Skip to content

Commit

Permalink
Merge pull request #32 from mruoss/renovate/flame-0.x
Browse files Browse the repository at this point in the history
Update dependency flame to ~> 0.2.0
  • Loading branch information
mruoss authored Jun 19, 2024
2 parents 732c675 + 566bbba commit e895323
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 50 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

<!--------------------- Don't add new entries after this line --------------------->

- Support for FLAME >= 0.2.0 and livebook integraion (requires livebook >= 0.13.0) - [#32](https://github.com/mruoss/flame_k8s_backend/pull/32)

## [0.3.3] - 2024-04-29

### Changed
Expand Down
27 changes: 6 additions & 21 deletions lib/flame_k8s_backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,8 @@ defmodule FLAMEK8sBackend do

require Logger

defstruct env: %{},
runner_pod_manifest: nil,
defstruct runner_pod_manifest: nil,
parent_ref: nil,
runner_node_basename: nil,
runner_pod_ip: nil,
runner_pod_name: nil,
runner_node_name: nil,
runner_pod_tpl: nil,
boot_timeout: nil,
Expand All @@ -175,11 +171,10 @@ defmodule FLAMEK8sBackend do
@impl true
def init(opts) do
conf = Application.get_env(:flame, __MODULE__) || []
[node_base | _ip] = node() |> to_string() |> String.split("@")
[_node_base | _ip] = node() |> to_string() |> String.split("@")

default = %FLAMEK8sBackend{
boot_timeout: 30_000,
runner_node_basename: node_base
boot_timeout: 30_000
}

provided_opts =
Expand All @@ -197,11 +192,6 @@ defmodule FLAMEK8sBackend do

parent_ref = make_ref()

encoded_parent =
parent_ref
|> FLAME.Parent.new(self(), __MODULE__)
|> FLAME.Parent.encode()

req = K8sClient.connect()

case K8sClient.get_pod(req, System.get_env("POD_NAMESPACE"), System.get_env("POD_NAME")) do
Expand All @@ -214,7 +204,7 @@ defmodule FLAMEK8sBackend do
RunnerPodTemplate.manifest(
base_pod,
opts[:runner_pod_tpl],
encoded_parent,
parent_ref,
Keyword.take(provided_opts, [:app_container_name, :omit_owner_reference])
)
)
Expand Down Expand Up @@ -266,11 +256,7 @@ defmodule FLAMEK8sBackend do
case created_pod do
{:ok, pod} ->
log(state, "Runner pod created and scheduled", pod_ip: pod["status"]["podIP"])

struct!(state,
runner_pod_ip: pod["status"]["podIP"],
runner_pod_name: pod["metadata"]["name"]
)
state

:error ->
Logger.error("failed to schedule runner pod within #{state.boot_timeout}ms")
Expand All @@ -279,7 +265,6 @@ defmodule FLAMEK8sBackend do
end)

remaining_connect_window = state.boot_timeout - req_connect_time
runner_node_name = :"#{state.runner_node_basename}@#{new_state.runner_pod_ip}"

log(state, "Waiting for Remote UP.", remaining_connect_window: remaining_connect_window)

Expand All @@ -297,7 +282,7 @@ defmodule FLAMEK8sBackend do
new_state =
struct!(new_state,
remote_terminator_pid: remote_terminator_pid,
runner_node_name: runner_node_name
runner_node_name: node(remote_terminator_pid)
)

{:ok, remote_terminator_pid, new_state}
Expand Down
28 changes: 19 additions & 9 deletions lib/flame_k8s_backend/runner_pod_template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,22 @@ defmodule FLAMEK8sBackend.RunnerPodTemplate do
"""
@spec manifest(parent_pod_manifest(), t() | callback(), Keyword.t()) ::
runner_pod_template :: map()
def manifest(parent_pod_manifest, template_args_or_callback, encoded_parent, opts \\ [])
def manifest(parent_pod_manifest, template_args_or_callback, parent_ref, opts \\ [])

def manifest(parent_pod_manifest, template_callback, encoded_parent, opts)
def manifest(parent_pod_manifest, template_callback, parent_ref, opts)
when is_function(template_callback) do
app_container = app_container(parent_pod_manifest, opts)

parent_pod_manifest
|> template_callback.()
|> apply_defaults(parent_pod_manifest, app_container, encoded_parent, opts)
|> apply_defaults(parent_pod_manifest, app_container, parent_ref, opts)
end

def manifest(parent_pod_manifest, nil, encoded_parent, opts) do
manifest(parent_pod_manifest, %RunnerPodTemplate{}, encoded_parent, opts)
def manifest(parent_pod_manifest, nil, parent_ref, opts) do
manifest(parent_pod_manifest, %RunnerPodTemplate{}, parent_ref, opts)
end

def manifest(parent_pod_manifest, %RunnerPodTemplate{} = template_opts, encoded_parent, opts) do
def manifest(parent_pod_manifest, %RunnerPodTemplate{} = template_opts, parent_ref, opts) do
app_container = app_container(parent_pod_manifest, opts)
env = template_opts.env || []

Expand All @@ -159,25 +159,35 @@ defmodule FLAMEK8sBackend.RunnerPodTemplate do
}
}

apply_defaults(runner_pod_template, parent_pod_manifest, app_container, encoded_parent, opts)
apply_defaults(runner_pod_template, parent_pod_manifest, app_container, parent_ref, opts)
end

defp apply_defaults(
runner_pod_template,
parent_pod_manifest,
app_container,
encoded_parent,
parent_ref,
opts
) do
parent_pod_manifest_name = parent_pod_manifest["metadata"]["name"]
pod_name_sliced = String.slice(parent_pod_manifest_name, 0..40)
runner_pod_name = pod_name_sliced <> rand_id(20)
runner_pod_name = "#{pod_name_sliced}-#{rand_id(20)}"

object_references =
if opts[:omit_owner_reference],
do: [],
else: object_references(parent_pod_manifest)

parent = FLAME.Parent.new(parent_ref, self(), FLAMEK8sBackend, runner_pod_name, "POD_IP")

parent =
case System.get_env("FLAME_K8S_BACKEND_GIT_REF") do
nil -> parent
git_ref -> struct(parent, backend_vsn: [github: "mruoss/flame_k8s_backend", ref: git_ref])
end

encoded_parent = FLAME.Parent.encode(parent)

runner_pod_template
|> Map.merge(%{"apiVersion" => "v1", "kind" => "Pod"})
|> put_in(~w(metadata name), runner_pod_name)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule FlameK8sBackend.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:flame, "~> 0.1.8"},
{:flame, "~> 0.2.0"},
{:req, "~> 0.4.5"},
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
{:mix_test_watch, "~> 1.0", only: [:dev, :test], runtime: false},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"ex_doc": {:hex, :ex_doc, "0.34.0", "ab95e0775db3df71d30cf8d78728dd9261c355c81382bcd4cefdc74610bef13e", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "60734fb4c1353f270c3286df4a0d51e65a2c1d9fba66af3940847cc65a8066d7"},
"file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"},
"finch": {:hex, :finch, "0.18.0", "944ac7d34d0bd2ac8998f79f7a811b21d87d911e77a786bc5810adb75632ada4", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "69f5045b042e531e53edc2574f15e25e735b522c37e2ddb766e15b979e03aa65"},
"flame": {:hex, :flame, "0.1.12", "4d46b706d35d6eb22505d0e060fe41174052eaa38f778a4762fc74dd2c9df301", [:mix], [{:req, "~> 0.4.13", [hex: :req, repo: "hexpm", optional: false]}], "hexpm", "aa25de6614455ac01e33409c08db4560fa54dd837ad116aae16c8f7c011ccd76"},
"flame": {:hex, :flame, "0.2.0", "6a87cfc9fde4d51899a90db209254479b1b9ee14e9e6080027497662187bcd83", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, ">= 0.0.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "eba9c68d9804fb6f437a76b077455886f33c34cc2ccb880af61ba7facfafd093"},
"hpax": {:hex, :hpax, "0.2.0", "5a58219adcb75977b2edce5eb22051de9362f08236220c9e859a47111c194ff5", [:mix], [], "hexpm", "bea06558cdae85bed075e6c036993d43cd54d447f76d8190a8db0dc5893fa2f1"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"k8s": {:hex, :k8s, "2.5.0", "16ceef480cf1503ad561529ef93c87d921b4baa29d73ee16cebae13d37e218f4", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: false]}, {:mint_web_socket, "~> 1.0", [hex: :mint_web_socket, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:yaml_elixir, "~> 2.8", [hex: :yaml_elixir, repo: "hexpm", optional: false]}], "hexpm", "c46032a4eef273000d32608efbc2edbdfde480d5f24df69e0938daa01026d1eb"},
Expand Down
38 changes: 21 additions & 17 deletions test/flame_k8s_backend/runner_pod_template_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ defmodule FLAMEK8sBackend.RunnerPodTemplateTest do

import YamlElixir.Sigil

defp flame_parent(pod_manifest) do
pod_manifest
|> get_in(env_var_access("FLAME_PARENT"))
|> List.first()
|> Base.decode64!()
|> :erlang.binary_to_term()
end

defp env_var_access(name) do
app_container_access(["env", Access.filter(&(&1["name"] == name)), "value"])
end
Expand Down Expand Up @@ -40,7 +48,7 @@ defmodule FLAMEK8sBackend.RunnerPodTemplateTest do
)
end

MUT.manifest(parent_pod_manifest, callback, "ENCODED_PARNET_REF")
MUT.manifest(parent_pod_manifest, callback, make_ref())
end

test "should return pod manifest with data form callback", %{
Expand All @@ -63,7 +71,7 @@ defmodule FLAMEK8sBackend.RunnerPodTemplateTest do
)
end

pod_manifest = MUT.manifest(parent_pod_manifest, callback, "ENCODED_PARNET_REF")
pod_manifest = MUT.manifest(parent_pod_manifest, callback, make_ref())

assert get_in(pod_manifest, app_container_access(~w(resources requests memory))) == "100Mi"
assert get_in(pod_manifest, app_container_access(~w(resources limits memory))) == "500Mi"
Expand All @@ -85,7 +93,7 @@ defmodule FLAMEK8sBackend.RunnerPodTemplateTest do
"""
end

pod_manifest = MUT.manifest(parent_pod_manifest, callback, "ENCODED_PARNET_REF")
pod_manifest = MUT.manifest(parent_pod_manifest, callback, make_ref())
assert get_in(pod_manifest, app_container_access() ++ ["image"]) == "flame-test-image:0.1.0"

owner_references = get_in(pod_manifest, ~w(metadata ownerReferences))
Expand All @@ -112,7 +120,7 @@ defmodule FLAMEK8sBackend.RunnerPodTemplateTest do
end

pod_manifest =
MUT.manifest(parent_pod_manifest, callback, "ENCODED_PARNET_REF",
MUT.manifest(parent_pod_manifest, callback, make_ref(),
app_container_name: "other-container"
)

Expand All @@ -136,9 +144,7 @@ defmodule FLAMEK8sBackend.RunnerPodTemplateTest do
end

pod_manifest =
MUT.manifest(parent_pod_manifest, callback, "ENCODED_PARNET_REF",
omit_owner_reference: true
)
MUT.manifest(parent_pod_manifest, callback, make_ref(), omit_owner_reference: true)

assert [] == get_in(pod_manifest, ~w(metadata ownerReferences))
end
Expand All @@ -149,17 +155,17 @@ defmodule FLAMEK8sBackend.RunnerPodTemplateTest do
parent_pod_manifest_full: parent_pod_manifest
} do
template_opts = %MUT{}
pod_manifest = MUT.manifest(parent_pod_manifest, template_opts, "ENCODED_PARNET_REF")
pod_manifest = MUT.manifest(parent_pod_manifest, template_opts, make_ref())

assert get_in(pod_manifest, env_var_access("RELEASE_NODE")) == ["flame_test@$(POD_IP)"]
assert get_in(pod_manifest, env_var_access("FLAME_PARENT")) == ["ENCODED_PARNET_REF"]
end

test "Only default envs if add_parent_env is set to false", %{
parent_pod_manifest_full: parent_pod_manifest
} do
ref = make_ref()
template_opts = %MUT{add_parent_env: false}
pod_manifest = MUT.manifest(parent_pod_manifest, template_opts, "ENCODED_PARNET_REF")
pod_manifest = MUT.manifest(parent_pod_manifest, template_opts, ref)

assert get_in(pod_manifest, app_container_access(~w(resources requests memory))) == "100Mi"
assert get_in(pod_manifest, env_var_access("POD_NAMESPACE")) == ["test-namespace"]
Expand All @@ -168,7 +174,8 @@ defmodule FLAMEK8sBackend.RunnerPodTemplateTest do
get_in(pod_manifest, env_var_access("POD_NAME"))

assert get_in(pod_manifest, env_var_access("PHX_SERVER")) == ["false"]
assert get_in(pod_manifest, env_var_access("FLAME_PARENT")) == ["ENCODED_PARNET_REF"]
parent = flame_parent(pod_manifest)
assert parent.ref == ref
end
end

Expand All @@ -177,35 +184,32 @@ defmodule FLAMEK8sBackend.RunnerPodTemplateTest do
parent_pod_manifest_full: parent_pod_manifest
} do
template_opts = %MUT{env: [%{"name" => "FOO", "value" => "bar"}]}
pod_manifest = MUT.manifest(parent_pod_manifest, template_opts, "ENCODED_PARNET_REF")
pod_manifest = MUT.manifest(parent_pod_manifest, template_opts, make_ref())

assert get_in(pod_manifest, env_var_access("RELEASE_NODE")) == ["flame_test@$(POD_IP)"]
assert get_in(pod_manifest, env_var_access("FOO")) == ["bar"]
assert get_in(pod_manifest, env_var_access("FLAME_PARENT")) == ["ENCODED_PARNET_REF"]
end

test "No parent envs if add_parent_env is set to false", %{
parent_pod_manifest_full: parent_pod_manifest
} do
template_opts = %MUT{env: [%{"name" => "FOO", "value" => "bar"}], add_parent_env: false}
pod_manifest = MUT.manifest(parent_pod_manifest, template_opts, "ENCODED_PARNET_REF")
pod_manifest = MUT.manifest(parent_pod_manifest, template_opts, make_ref())

assert get_in(pod_manifest, env_var_access("RELEASE_NODE")) == []
assert get_in(pod_manifest, env_var_access("FOO")) == ["bar"]
assert get_in(pod_manifest, env_var_access("FLAME_PARENT")) == ["ENCODED_PARNET_REF"]
end
end

describe "manifest/2 with nil as template opts" do
test "Uses parent pod's values for empty template opts", %{
parent_pod_manifest_full: parent_pod_manifest
} do
pod_manifest = MUT.manifest(parent_pod_manifest, nil, "ENCODED_PARNET_REF")
pod_manifest = MUT.manifest(parent_pod_manifest, nil, make_ref())

assert get_in(pod_manifest, app_container_access(~w(resources requests memory))) == "100Mi"

assert get_in(pod_manifest, env_var_access("RELEASE_NODE")) == ["flame_test@$(POD_IP)"]
assert get_in(pod_manifest, env_var_access("FLAME_PARENT")) == ["ENCODED_PARNET_REF"]
end
end
end
2 changes: 1 addition & 1 deletion test/integration/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

FROM hexpm/elixir:1.15.7-erlang-26.1.2-debian-buster-20231009
FROM hexpm/elixir:1.17.1-erlang-27.0-debian-bullseye-20240612-slim

ENV MIX_ENV=test \
MIX_HOME=/opt/mix \
Expand Down

0 comments on commit e895323

Please sign in to comment.