Packages
k8s
0.2.7
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
Retired package: Release invalid - crashes on start, bad stacktrace
Current section
Files
Jump to
Current section
Files
lib/k8s/client/runner/base.ex
defmodule K8s.Client.Runner.Base do
@moduledoc """
Base HTTP processor for `K8s.Client`
"""
@type result_t :: {:ok, map() | reference()} | {:error, atom} | {:error, binary()}
alias K8s.Cluster
alias K8s.Conf.RequestOptions
alias K8s.Operation
@doc """
Runs a `K8s.Operation`.
## Examples
*Note:* Examples assume a cluster was registered named "test-cluster", see `K8s.Cluster.register/2`.
Running a list pods operation:
```elixir
operation = K8s.Client.list("v1", "Pod", namespace: :all)
{:ok, %{"items" => pods}} = K8s.Client.run(operation, "test-cluster")
```
Running a dry-run of a create deployment operation:
```elixir
deployment = %{
"apiVersion" => "apps/v1",
"kind" => "Deployment",
"metadata" => %{
"labels" => %{
"app" => "nginx"
},
"name" => "nginx",
"namespace" => "test"
},
"spec" => %{
"replicas" => 2,
"selector" => %{
"matchLabels" => %{
"app" => "nginx"
}
},
"template" => %{
"metadata" => %{
"labels" => %{
"app" => "nginx"
}
},
"spec" => %{
"containers" => %{
"image" => "nginx",
"name" => "nginx"
}
}
}
}
}
operation = K8s.Client.create(deployment)
# opts is passed to HTTPoison as opts.
opts = [params: %{"dryRun" => "all"}]
:ok = K8s.Client.Runner.Base.run(operation, "test-cluster", opts)
```
"""
@spec run(Operation.t(), nil | binary | atom) :: result_t
def run(operation = %Operation{}, cluster_name \\ "default"),
do: run(operation, cluster_name, [])
@doc """
Run an operation and pass `opts` to HTTPoison.
See `run/2`
"""
@spec run(Operation.t(), binary | atom, keyword()) :: result_t
def run(operation = %Operation{}, cluster_name, opts) when is_list(opts) do
run(operation, cluster_name, operation.resource, opts)
end
@doc """
Run an operation with an alternative HTTP Body (map) and pass `opts` to HTTPoison.
See `run/2`
"""
@spec run(Operation.t(), binary | atom, map(), keyword()) :: result_t
def run(operation = %Operation{}, cluster_name, body, opts \\ []) do
with {:ok, url} <- Cluster.url_for(operation, cluster_name),
{:ok, conf} <- Cluster.conf(cluster_name),
{:ok, request_options} <- RequestOptions.generate(conf),
{:ok, http_body} <- encode(body, operation.method) do
http_headers = K8s.http_provider().headers(request_options)
http_opts = Keyword.merge([ssl: request_options.ssl_options], opts)
K8s.http_provider().request(
operation.method,
url,
http_body,
http_headers,
http_opts
)
else
error -> error
end
end
@spec encode(any(), atom()) :: {:ok, binary} | {:error, any}
def encode(body, http_method) when http_method in [:put, :patch, :post] do
Jason.encode(body)
end
def encode(_, _), do: {:ok, ""}
end