Skip to content

Commit

Permalink
Apply CR
Browse files Browse the repository at this point in the history
  • Loading branch information
Argonus committed Jan 3, 2025
1 parent d214e14 commit 84e542b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
15 changes: 8 additions & 7 deletions lib/strategy/kubernetes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Cluster.Strategy.Kubernetes do
- `:kubernetes_selector`
- `:kubernetes_service_name`
- `:kubernetes_ip_lookup_mode`
- `:kubernetes_resource_version`
- `:kubernetes_use_cached_resources`
- `:mode`
## Getting `<basename>`
Expand Down Expand Up @@ -71,12 +71,11 @@ defmodule Cluster.Strategy.Kubernetes do
Then, this strategy will fetch the IP of all pods with that label and attempt to connect.
### `kubernetes_resource_version` option
### `kubernetes_use_cached_resources` option
When setting this value, this strategy will use given resource version value to fetch k8s resources,
where each modification of the resource increments the resource version.
If set to `0` kubernetes will use cached version, that may be outdated.
When setting this value, this strategy will use cached resource version value to fetch k8s resources.
In k8s resources are incremented by 1 on every change, this version will set requested resourceVersion
to 0, that will use cached versions of resources, take in mind that this may be outdated or unavailable.
### `:mode` option
Expand Down Expand Up @@ -368,7 +367,9 @@ defmodule Cluster.Strategy.Kubernetes do
service_name = Keyword.get(config, :kubernetes_service_name)
selector = Keyword.fetch!(config, :kubernetes_selector)
ip_lookup_mode = Keyword.get(config, :kubernetes_ip_lookup_mode, :endpoints)
resource_version = Keyword.get(config, :kubernetes_resource_version, nil)

use_cache = Keyword.get(config, :kubernetes_use_cached_resources, false)
resource_version = if use_cache, do: 0, else: nil

master_name = Keyword.get(config, :kubernetes_master, @kubernetes_master)
cluster_domain = System.get_env("CLUSTER_DOMAIN", "#{cluster_name}.local")
Expand Down
35 changes: 31 additions & 4 deletions test/kubernetes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ defmodule Cluster.Strategy.KubernetesTest do
end
end

test "works with resource version" do
test "works with cached resources" do
use_cassette "kubernetes", custom: true do
capture_log(fn ->
start_supervised!({Kubernetes,
Expand All @@ -91,7 +91,34 @@ defmodule Cluster.Strategy.KubernetesTest do
config: [
kubernetes_node_basename: "test_basename",
kubernetes_selector: "app=test_selector",
kubernetes_resource_version: 0,
kubernetes_use_cached_resources: true,
# If you want to run the test freshly, you'll need to create a DNS Entry
kubernetes_master: "cluster.localhost.",
kubernetes_service_account_path:
Path.join([__DIR__, "fixtures", "kubernetes", "service_account"])
],
connect: {Nodes, :connect, [self()]},
disconnect: {Nodes, :disconnect, [self()]},
list_nodes: {Nodes, :list_nodes, [[]]}
}
]})

assert_receive {:connect, _}, 5_000
end)
end
end

test "works with no cached resources" do
use_cassette "kubernetes", custom: true do
capture_log(fn ->
start_supervised!({Kubernetes,
[
%Cluster.Strategy.State{
topology: :name,
config: [
kubernetes_node_basename: "test_basename",
kubernetes_selector: "app=test_selector",
kubernetes_use_cached_resources: false,
# If you want to run the test freshly, you'll need to create a DNS Entry
kubernetes_master: "cluster.localhost.",
kubernetes_service_account_path:
Expand Down Expand Up @@ -228,7 +255,7 @@ defmodule Cluster.Strategy.KubernetesTest do
end
end

test "works with pods and resource version" do
test "works with pods and cached resources" do
use_cassette "kubernetes_pods", custom: true do
capture_log(fn ->
start_supervised!({Kubernetes,
Expand All @@ -241,7 +268,7 @@ defmodule Cluster.Strategy.KubernetesTest do
# If you want to run the test freshly, you'll need to create a DNS Entry
kubernetes_master: "cluster.localhost.",
kubernetes_ip_lookup_mode: :pods,
kubernetes_resource_version: 0,
kubernetes_use_cached_resources: true,
kubernetes_service_account_path:
Path.join([__DIR__, "fixtures", "kubernetes", "service_account"])
],
Expand Down

0 comments on commit 84e542b

Please sign in to comment.