diff --git a/lib/strategy/kubernetes.ex b/lib/strategy/kubernetes.ex index 19fb4a8..ae83791 100644 --- a/lib/strategy/kubernetes.ex +++ b/lib/strategy/kubernetes.ex @@ -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 `` @@ -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 @@ -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") diff --git a/test/kubernetes_test.exs b/test/kubernetes_test.exs index fc37019..512ad6d 100644 --- a/test/kubernetes_test.exs +++ b/test/kubernetes_test.exs @@ -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, @@ -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: @@ -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, @@ -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"]) ],