Packages
k8s
0.5.0-rc.1
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/cluster.ex
defmodule K8s.Cluster do
@moduledoc """
Cluster configuration and API route store for `K8s.Client`
"""
alias K8s.{Cluster, Operation}
@doc """
Retrieve the URL for a `K8s.Operation`
## Examples
iex> conn = K8s.Conn.from_file("./test/support/kube-config.yaml")
...> K8s.Cluster.Registry.add(:test_cluster, conn)
...> operation = K8s.Operation.build(:get, "apps/v1", :deployments, [namespace: "default", name: "nginx"])
...> K8s.Cluster.url_for(operation, :test_cluster)
{:ok, "https://localhost:6443/apis/apps/v1/namespaces/default/deployments/nginx"}
"""
@spec url_for(Operation.t(), atom) :: {:ok, binary} | {:error, atom(), binary()}
def url_for(%Operation{api_version: api_version, name: name, verb: _verb} = operation, cluster) do
with {:ok, conn} <- Cluster.conn(cluster),
{:ok, name} <- Cluster.Group.resource_name_for_kind(cluster, api_version, name),
operation <- Map.put(operation, :name, name),
{:ok, path} <- Operation.to_path(operation) do
{:ok, Path.join(conn.url, path)}
end
end
@doc """
Retrieve the base URL for a cluster
## Examples
iex> conn = K8s.Conn.from_file("./test/support/kube-config.yaml")
...> K8s.Cluster.Registry.add(:test_cluster, conn)
...> K8s.Cluster.base_url(:test_cluster)
{:ok, "https://localhost:6443"}
"""
@spec base_url(atom) :: {:ok, binary()} | {:error, atom} | {:error, binary}
def base_url(cluster) do
with {:ok, conn} <- Cluster.conn(cluster) do
{:ok, conn.url}
end
end
@doc """
Retrieve a cluster's connection configuration.
## Example
iex> config_file = K8s.Conn.from_file("./test/support/kube-config.yaml", [user: "token-user"])
...> K8s.Cluster.Registry.add(:test_cluster, config_file)
...> {:ok, conn} = K8s.Cluster.conn(:test_cluster)
...> conn
%K8s.Conn{auth: %K8s.Conn.Auth.Token{token: "just-a-token-user-pun-intended"}, ca_cert: nil, cluster_name: "docker-for-desktop-cluster", insecure_skip_tls_verify: true, url: "https://localhost:6443",user_name: "token-user"}
"""
@spec conn(atom) :: {:ok, K8s.Conn.t()} | {:error, :cluster_not_registered}
def conn(cluster_name) do
case :ets.lookup(K8s.Conn, cluster_name) do
[] -> {:error, :cluster_not_registered}
[{_, conn}] -> {:ok, conn}
end
end
@doc """
List registered cluster names
"""
@spec list() :: list(atom)
def list() do
K8s.Conn
|> :ets.tab2list()
|> Keyword.keys()
end
end