Packages
k8s
0.2.6
2.8.0
2.7.0
2.6.2
2.6.1
2.6.0
2.5.0
2.4.2
2.4.1
2.4.0
2.3.0
2.2.0
2.1.1
2.1.0
2.0.3
2.0.2
2.0.1
2.0.0
2.0.0-rc.6
2.0.0-rc.5
2.0.0-rc.4
2.0.0-rc.3
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
1.2.1
1.2.0
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
1.0.0-rc1
0.5.2
0.5.1
0.5.0
0.5.0-rc.2
0.5.0-rc.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
retired
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.3
Kubernetes API Client for Elixir
Current section
Files
Jump to
Current section
Files
lib/k8s/config.ex
defmodule K8s.Config do
# credo:disable-for-this-file
@moduledoc """
K8s runtime configuration
"""
@env_var_prefix "K8S_CLUSTER_CONF_"
@env_var_sa_prefix "K8S_CLUSTER_CONF_SA_"
@env_var_path_prefix "K8S_CLUSTER_CONF_PATH_"
@env_var_context_prefix "K8S_CLUSTER_CONF_CONTEXT_"
def all() do
compile_time_clusters_config = Application.get_env(:k8s, :clusters)
clusters = runtime_clusters_config(compile_time_clusters_config)
end
@doc """
Cluster configuration read from env variables.
To be merged over `Application.get_env(:k8s, :clusters)`.
"""
@spec runtime_clusters_config(map) :: map
def runtime_clusters_config(config) do
Enum.reduce(env(), config, fn {k, v}, acc ->
cluster_name = k |> cluster_name() |> String.to_atom()
acc_cluster_config = Map.get(acc, cluster_name, %{})
{new_key, new_value} = get_config_kv(k, v)
updated_cluster_conf = Map.put(acc_cluster_config, new_key, new_value)
Map.put(acc, cluster_name, updated_cluster_conf)
end)
end
def get_config_kv(@env_var_context_prefix <> _cluster_name, conf_opts_context),
do: {:conf_opts, [context: conf_opts_context]}
def get_config_kv(@env_var_path_prefix <> _cluster_name, conf_path), do: {:conf, conf_path}
def get_config_kv(@env_var_sa_prefix <> _cluster_name, "true"), do: {:use_sa, true}
def get_config_kv(@env_var_sa_prefix <> _cluster_name, "false"), do: {:use_sa, false}
def cluster_name(@env_var_context_prefix <> cluster_name), do: cluster_name
def cluster_name(@env_var_path_prefix <> cluster_name), do: cluster_name
def cluster_name(@env_var_sa_prefix <> cluster_name), do: cluster_name
def env(), do: Map.take(System.get_env(), env_keys())
@doc """
export K8S_CLUSTER_CONF_SA_us_central=true
export K8S_CLUSTER_CONF_PATH_us_east="east.yaml"
export K8S_CLUSTER_CONF_CONTEXT_us_east="east"
export K8S_CLUSTER_CONF_PATH_us_west="west.yaml"
export K8S_CLUSTER_CONF_CONTEXT_dev="context-name"
iex -S mix
"""
def env_keys() do
System.get_env()
|> Map.keys()
|> Enum.filter(&String.starts_with?(&1, @env_var_prefix))
end
end