Skip to content

Commit 8d4a235

Browse files
Use deployment group specific LIVEBOOK_NODE for K8s clustering (#3127)
Co-authored-by: José Valim <[email protected]>
1 parent 99b0118 commit 8d4a235

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

lib/livebook_web/live/hub/teams/deployment_group_agent_component.ex

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ defmodule LivebookWeb.Hub.Teams.DeploymentGroupAgentComponent do
207207
%{
208208
docker_instructions: docker_instructions(image, env),
209209
fly_instructions: fly_instructions(image, env, hub.hub_name, deployment_group.name),
210-
k8s_instructions: k8s_instructions(image, env)
210+
k8s_instructions: k8s_instructions(image, env, deployment_group.name)
211211
}
212212
end
213213

@@ -244,7 +244,7 @@ defmodule LivebookWeb.Hub.Teams.DeploymentGroupAgentComponent do
244244
}
245245
end
246246

247-
defp k8s_instructions(image, env) do
247+
defp k8s_instructions(image, env, deployment_group_name) do
248248
{secrets, envs} =
249249
Map.split(
250250
Map.new(env),
@@ -265,7 +265,9 @@ defmodule LivebookWeb.Hub.Teams.DeploymentGroupAgentComponent do
265265
"dns:livebook-headless.$(POD_NAMESPACE).svc.cluster.local"
266266
)
267267

268-
k8s_instructions_template(image, envs, secrets, replicas)
268+
dg_suffix = sanitize_for_node_name(deployment_group_name)
269+
270+
k8s_instructions_template(image, envs, secrets, replicas, dg_suffix)
269271
end
270272

271273
require EEx
@@ -328,7 +330,7 @@ defmodule LivebookWeb.Hub.Teams.DeploymentGroupAgentComponent do
328330
fieldRef:
329331
fieldPath: metadata.namespace
330332
- name: LIVEBOOK_NODE
331-
value: "livebook@$(POD_IP)"<%= for {k, v} <- envs, k != "LIVEBOOK_NODE" do %>
333+
value: "livebook-<%= dg_suffix %>@$(POD_IP)"<%= for {k, v} <- envs, k != "LIVEBOOK_NODE" do %>
332334
- name: <%= k %>
333335
value: <%= inspect(v) %><% end %><%= for {k, _} <- secrets do %>
334336
- name: <%= k %>
@@ -350,6 +352,25 @@ defmodule LivebookWeb.Hub.Teams.DeploymentGroupAgentComponent do
350352
# LIVEBOOK_PASSWORD: <base64_encoded_password><%= for {k, v} <- secrets do %>
351353
<%= k %>: <%= Base.encode64(v) %><% end %>
352354
""",
353-
[:image, :envs, :secrets, :replicas]
355+
[:image, :envs, :secrets, :replicas, :dg_suffix]
354356
)
357+
358+
def sanitize_for_node_name(string) do
359+
sanitized =
360+
string
361+
|> String.downcase()
362+
|> String.replace(~r/[^a-z0-9]/, "_")
363+
|> String.replace(~r/_+/, "_")
364+
|> String.trim("_")
365+
|> String.slice(0, 40)
366+
|> String.trim("_")
367+
368+
if sanitized == "" do
369+
string
370+
|> Base.encode32(padding: false, case: :lower)
371+
|> String.slice(0, 20)
372+
else
373+
sanitized
374+
end
375+
end
355376
end

0 commit comments

Comments
 (0)