Skip to content

Commit

Permalink
Feature/ret ep update db+domain multi cluster (#367)
Browse files Browse the repository at this point in the history
- [dashboard] set ret client to use the http port 4001
- [dashboard] added a migration to add 2 columns in hubs table, region and domain.
- [dashboard] added the region and domain field in hubs class
- [dashboard] send create hub instances req to orch with region, catch domain and region in orch's response, store them in db (hubs table)
- [dashboard] ret_host_url to use orch provided multi-cluster domain and public api to check liveness
- [dashboardclient] add domain and region field to hubT
- [dashboardclient] use the newly provided domain value from dashboard's /api/v1/hubs for HubLink and HubCard
  • Loading branch information
tanfarming authored Jun 20, 2023
1 parent 304f7c0 commit 9f385f5
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 17 deletions.
4 changes: 2 additions & 2 deletions client/components/modules/hubs/HubCard/HubCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type HubCardPropsT = {

const HubCard = ({ hub, refreshHubData, classProp = '' }: HubCardPropsT) => {
const storeContext = useContext(StoreContext);
const { hubId, status, subdomain, lastError } = hub;
const { hubId, status, subdomain, domain, lastError } = hub;
const [showRevertError, setShowRevertError] = useState<boolean>(
lastError === LastErrorE.SUBDOMAIN_REVERTED
);
Expand Down Expand Up @@ -159,7 +159,7 @@ const HubCard = ({ hub, refreshHubData, classProp = '' }: HubCardPropsT) => {
)}

{/* Subdomain is ready and available */}
{status === StatusE.READY && <HubLink subdomain={subdomain} />}
{status === StatusE.READY && <HubLink subdomain={subdomain} domain={domain} />}
</div>

{/* FOOTER */}
Expand Down
6 changes: 4 additions & 2 deletions client/components/modules/hubs/HubCard/HubLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { HUB_ROOT_DOMAIN } from 'config';

type HubLinkPropsT = {
subdomain: string;
domain: string;
classProp?: string;
};

const HubLink = ({ subdomain, classProp = '' }: HubLinkPropsT) => {
const subdomainRootdomain = `https://${subdomain}.${HUB_ROOT_DOMAIN}`;
const HubLink = ({ subdomain, domain=HUB_ROOT_DOMAIN, classProp = '' }: HubLinkPropsT) => {

const subdomainRootdomain = `https://${subdomain}.${domain || HUB_ROOT_DOMAIN}`;

return (
<div className={`flex ${classProp}`}>
Expand Down
4 changes: 4 additions & 0 deletions client/pages/dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const creatingHub: HubT = {
storageLimitMb: 0,
subdomain: '',
tier: 'premium',
domain: "myhubs.net",
region: "us",
};

/**
Expand All @@ -47,6 +49,8 @@ const ErroringHub: HubT = {
storageLimitMb: 0,
subdomain: '',
tier: 'premium',
domain: "myhubs.net",
region: "us",
};

const Dashboard = ({ subscription }: DashboardPropsT) => {
Expand Down
2 changes: 2 additions & 0 deletions client/types/General.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export type HubT = {
storageLimitMb: number;
subdomain: string;
tier: TierT;
domain: string;
region: string;
};

// TODO Do we still need this?
Expand Down
4 changes: 4 additions & 0 deletions lib/dash/hub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ defmodule Dash.Hub do
field :status, Ecto.Enum, values: [:creating, :updating, :ready, :subdomain_error, :error]
field :storage_limit_mb, :integer
field :subdomain, :string
field :domain, :string
field :region, :string
field :tier, Ecto.Enum, values: [:mvp, :p0, :p1]
belongs_to :account, Dash.Account, references: :account_id

Expand All @@ -29,6 +31,7 @@ defmodule Dash.Hub do
:storage_limit_mb,
:tier,
:subdomain,
:domain,
:status
])
|> validate_required([
Expand All @@ -37,6 +40,7 @@ defmodule Dash.Hub do
:storage_limit_mb,
:tier,
:subdomain,
:domain,
:status
])
|> unique_constraint(:subdomain)
Expand Down
3 changes: 3 additions & 0 deletions lib/dash/orch_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Dash.OrchClient do
subdomain: hub.subdomain,
tier: hub.tier,
ccu_limit: hub.ccu_limit |> to_string(),
region: hub.region,
storage_limit: (hub.storage_limit_mb / 1024) |> to_string()
}

Expand Down Expand Up @@ -52,6 +53,7 @@ defmodule Dash.OrchClient do
storage_limit: Float.to_string(hub.storage_limit_mb / 1024),
subdomain: hub.subdomain,
tier: hub.tier,
domain: hub.domain,
useremail: fxa_email
}),
[],
Expand All @@ -64,6 +66,7 @@ defmodule Dash.OrchClient do
orch_hub_endpoint(),
Jason.encode!(%{
hub_id: hub.hub_id |> to_string,
domain: hub.domain,
subdomain: hub.subdomain
}),
[],
Expand Down
25 changes: 20 additions & 5 deletions lib/dash/plan_state_machine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,18 @@ defmodule Dash.PlanStateMachine do
status: :creating,
storage_limit_mb: @starter_storage_limit_mb,
subdomain: rand_string(10),
domain: "",
region: "us",
tier: :p0
})

{:ok, %{status_code: 200}} =
{:ok, response} =
OrchClient.create_hub(account.email, hub, disable_branding?: true)

if response.status_code == 200 do
response_body = Jason.decode!(response.body)
domain = response_body["domain"]
changeset = Ecto.Changeset.change(hub, domain: domain)
Repo.update!(changeset)
end
:ok
end

Expand All @@ -117,10 +123,19 @@ defmodule Dash.PlanStateMachine do
status: :creating,
storage_limit_mb: @standard_storage_limit_mb,
subdomain: rand_string(10),
domain: "",
region: "us",
tier: :p1
})

{:ok, %{status_code: 200}} = OrchClient.create_hub(account.email, hub)
{:ok, response} =
OrchClient.create_hub(account.email, hub)
if response.status_code == 200 do
response_body = Jason.decode!(response.body)
domain = response_body["domain"]
changeset = Ecto.Changeset.change(hub, domain: domain)
Repo.update!(changeset)
end

:ok
end

Expand Down
21 changes: 13 additions & 8 deletions lib/dash/ret_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ defmodule Dash.RetClient do

@ret_host_prefix "ret.hc-"
@ret_host_postfix ".svc.cluster.local"
@ret_internal_port "4000"
defp ret_host_url(%Dash.Hub{} = hub), do: ret_host_url(hub.hub_id)
@ret_internal_port "4001"
defp ret_host_url(%Dash.Hub{} = hub) do
if hub.domain != nil and hub.domain != "" do
foreignRetHost="https://#{hub.subdomain}.#{hub.domain}"
Logger.info("foreign ret -- #{foreignRetHost}")
foreignRetHost
else
ret_host_url(hub.hub_id)
end
end

defp ret_host_url(hub_id) when is_integer(hub_id) do
"https://#{@ret_host_prefix}#{hub_id}#{@ret_host_postfix}:#{@ret_internal_port}"
"http://#{@ret_host_prefix}#{hub_id}#{@ret_host_postfix}:#{@ret_internal_port}"
end

@ret_internal_scope "/api-internal/v1/"
Expand All @@ -37,8 +45,7 @@ defmodule Dash.RetClient do
[
{"x-ret-dashboard-access-key", get_ret_access_key()},
{"content-type", "application/json"}
],
hackney: [:insecure]
]
)
end

Expand Down Expand Up @@ -160,7 +167,6 @@ defmodule Dash.RetClient do
Logger.error(
"Failed to rewrite assets from: #{old_domain} to #{new_domain}. Error: #{inspect(err)}"
)

{:error, :rewrite_assets_failed}
end
end
Expand All @@ -174,8 +180,7 @@ defmodule Dash.RetClient do
[
{"x-ret-dashboard-access-key", get_ret_access_key()},
{"content-type", "application/json"}
],
hackney: [:insecure]
]
)

case response do
Expand Down
2 changes: 2 additions & 0 deletions lib/dash_web/views/api/v1/hub_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ defmodule DashWeb.Api.V1.HubView do
storageLimitMb: hub.storage_limit_mb,
tier: hub.tier,
subdomain: hub.subdomain,
domain: hub.domain,
region: hub.region,
status: hub.status
}
|> Map.merge(maybe_include_usage_stats)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Dash.Repo.Migrations.AddDomainAndRegionColumnsToHubs do
use Ecto.Migration

def up do
execute("""
DO $$
BEGIN
IF NOT EXISTS(
SELECT * FROM information_schema.columns
WHERE table_name = 'hubs' AND column_name = 'domain'
) THEN
ALTER TABLE hubs ADD COLUMN domain VARCHAR(255);
END IF;
END
$$;
""")
execute("""
DO $$
BEGIN
IF NOT EXISTS(
SELECT * FROM information_schema.columns
WHERE table_name = 'hubs' AND column_name = 'region'
) THEN
ALTER TABLE hubs ADD COLUMN region VARCHAR(255);
END IF;
END
$$;
""")
end

def down do
execute("ALTER TABLE hubs DROP COLUMN IF EXISTS domain")
execute("ALTER TABLE hubs DROP COLUMN IF EXISTS region")
end

end

0 comments on commit 9f385f5

Please sign in to comment.