Skip to content

Commit

Permalink
Query DNS before finalizing cloud instances
Browse files Browse the repository at this point in the history
There's some lag to externaldns registering for an ingress that could cause user-facing goofiness, just query dns until it resolves to wait for that to dissipate.
  • Loading branch information
michaeljguarino committed Aug 30, 2024
1 parent baf07c9 commit 05805bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
4 changes: 4 additions & 0 deletions apps/core/lib/core/schema/console_instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ defmodule Core.Schema.ConsoleInstance do
)
end

def provisioned(query \\ __MODULE__) do
from(c in query, where: c.status == ^:provisioned)
end

def ordered(query \\ __MODULE__, order \\ [asc: :name]) do
from(c in query, order_by: ^order)
end
Expand Down
24 changes: 17 additions & 7 deletions apps/core/lib/core/services/cloud/workflow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Core.Services.Cloud.Workflow do

Enum.reduce_while(0..10, instance, fn _, acc ->
case up(acc) do
{:ok, %ConsoleInstance{status: :deployment_created} = inst} -> {:halt, inst}
{:ok, %ConsoleInstance{status: :provisioned} = inst} -> {:halt, inst}
{:ok, inst} -> {:cont, inst}
err ->
:timer.sleep(:timer.seconds(1))
Expand All @@ -35,20 +35,28 @@ defmodule Core.Services.Cloud.Workflow do
def deprovision(%ConsoleInstance{} = instance) do
instance = Repo.preload(instance, [:postgres, :cluster])

Enum.reduce_while(0..10, instance, fn _, acc ->
Enum.reduce_while(0..20, instance, fn _, acc ->
case down(acc) do
{:ok, %ConsoleInstance{status: :pending} = inst} -> {:halt, inst}
{:ok, %ConsoleInstance{status: :database_deleted} = inst} -> {:halt, inst}
{:ok, inst} -> {:cont, inst}
err ->
:timer.sleep(:timer.seconds(1))
:timer.sleep(:timer.seconds(10))
Logger.error "failed to transition deprovisioning console: #{inspect(err)}"
{:cont, acc}
end
end)
|> finalize(:down)
end

defp up(%ConsoleInstance{status: :deployment_created, url: url} = inst) do
case {DNS.resolve(url), DNS.resolve(url, :cname)} do
{{:ok, [_ | _]}, _} -> mark_provisioned(inst)
{_, {:ok, [_ | _]}} -> mark_provisioned(inst)
{{:error, err}, _} -> {:error, "cannot resolve #{url}: #{inspect(err)}"}
end
end

defp up(%ConsoleInstance{status: :pending, postgres: pg, configuration: conf} = inst) do
with {:ok, pid} <- connect(pg),
{:ok, _} <- Postgrex.query(pid, "CREATE DATABASE #{conf.database}", []),
Expand Down Expand Up @@ -115,10 +123,7 @@ defmodule Core.Services.Cloud.Workflow do

defp down(inst), do: {:ok, inst}

defp finalize(%ConsoleInstance{status: :deployment_created} = inst, :up) do
ConsoleInstance.changeset(inst, %{status: :provisioned})
|> Repo.update()
end
defp finalize(%ConsoleInstance{status: :provisioned} = inst, :up), do: {:ok, inst}

defp finalize(%ConsoleInstance{status: :database_deleted, cluster: cluster, postgres: pg} = inst, :down) do
start_transaction()
Expand Down Expand Up @@ -161,5 +166,10 @@ defmodule Core.Services.Cloud.Workflow do
end
defp userinfo(_), do: %{}

defp mark_provisioned(inst) do
ConsoleInstance.changeset(inst, %{status: :provisioned})
|> Repo.update()
end

defp console(), do: Console.new(Core.conf(:console_url), Core.conf(:console_token))
end

0 comments on commit 05805bb

Please sign in to comment.