k8s v0.5.2 K8s.Operation

Encapsulates Kubernetes REST API operations.

Link to this section Summary

Types

K8s.Operation name. May be an atom, string, or tuple of {resource, subresource}.

t()
  • api_version - API groupVersion, AKA apiVersion
  • name - The name of the REST operation (Kubernets kind/resource/subresource). This is not always the same as the kind key in the data field. e.g: deployments when POSTing, GETting a deployment.
  • data - HTTP request body to submit when applicable. (POST, PUT, PATCH, etc)
  • method - HTTP Method
  • verb - Kubernetes REST API verb (deletecollection, update, create, watch, etc)
  • path_params - Parameters to interpolate into the Kubernetes REST URL
  • query_params - Query parameter (map). Merged w/ params provided to any K8s.Client.Runner. K8s.Client.Runner options win.

name would be deployments in the case of a deployment, but may be deployments/status or deployments/scale for Status and Scale subresources.

Functions

Builds an Operation given a verb and a k8s resource.

Builds an Operation given an verb and a k8s resource info.

Get a query param of an operation

Add a query param to an operation

Converts a K8s.Operation into a URL path.

Link to this section Types

Link to this type

name_t()

name_t() :: binary() | atom() | {binary(), binary()}

K8s.Operation name. May be an atom, string, or tuple of {resource, subresource}.

Link to this type

t()

t() :: %K8s.Operation{
  api_version: binary(),
  data: map() | nil,
  label_selector: K8s.Selector.t() | nil,
  method: atom(),
  name: name_t(),
  path_params: keyword(atom()),
  query_params: map(),
  verb: atom()
}
  • api_version - API groupVersion, AKA apiVersion
  • name - The name of the REST operation (Kubernets kind/resource/subresource). This is not always the same as the kind key in the data field. e.g: deployments when POSTing, GETting a deployment.
  • data - HTTP request body to submit when applicable. (POST, PUT, PATCH, etc)
  • method - HTTP Method
  • verb - Kubernetes REST API verb (deletecollection, update, create, watch, etc)
  • path_params - Parameters to interpolate into the Kubernetes REST URL
  • query_params - Query parameter (map). Merged w/ params provided to any K8s.Client.Runner. K8s.Client.Runner options win.

name would be deployments in the case of a deployment, but may be deployments/status or deployments/scale for Status and Scale subresources.

name and data field examples

The following example would update the nginx deployment's Scale. Note the deployments/scale operation will have a Scale data payload:

%K8s.Operation{
  method: :put,
  verb: :update,
  api_version: "v1", # api version of the "Scale" kind
  name: "deployments/scale",
  data: %{"apiVersion" => "v1", "kind" => "Scale"}, # `data` is of kind "Scale"
  path_params: [name: "nginx", namespace: "default"]
}

The following example would update the nginx deployment's Status. Note the deployments/status operation will have a Deployment data payload:

%K8s.Operation{
  method: :put,
  verb: :update,
  api_version: "apps/v1", # api version of the "Deployment" kind
  name: "deployments/status",
  data: %{"apiVersion" => "apps/v1", "kind" => "Deployment"}, # `data` is of kind "Deployment"
  path_params: [name: "nginx", namespace: "default"]
}

Link to this section Functions

Link to this function

build(verb, resource)

build(atom(), map()) :: t()

Builds an Operation given a verb and a k8s resource.

Examples

iex> deploy = %{"apiVersion" => "apps/v1", "kind" => "Deployment", "metadata" => %{"namespace" => "default", "name" => "nginx"}}
...> K8s.Operation.build(:update, deploy)
%K8s.Operation{
  method: :put,
  verb: :update,
  data: %{"apiVersion" => "apps/v1", "kind" => "Deployment", "metadata" => %{"namespace" => "default", "name" => "nginx"}},
  path_params: [namespace: "default", name: "nginx"],
  api_version: "apps/v1",
  name: "Deployment"
}
Link to this function

build(verb, api_version, name_or_kind, path_params, data \\ nil)

build(atom(), binary(), name_t(), keyword(), map() | nil) :: t()

Builds an Operation given an verb and a k8s resource info.

Note: The name here may be a Kind and not a REST resource name in the event that the operation was built using a map. Use K8s.Discovery.ResourceFinder.resource_name_for_kind/3 to get the correct REST resource name, given a kind.

Examples

Building a GET deployment operation:

iex> K8s.Operation.build(:get, "apps/v1", :deployment, [namespace: "default", name: "nginx"])
%K8s.Operation{
  method: :get,
  verb: :get,
  data: nil,
  path_params: [namespace: "default", name: "nginx"],
  api_version: "apps/v1",
  name: :deployment
}

Building a GET deployments/status operation:

iex> K8s.Operation.build(:get, "apps/v1", "deployments/status", [namespace: "default", name: "nginx"])
%K8s.Operation{
  method: :get,
  verb: :get,
  data: nil,
  path_params: [namespace: "default", name: "nginx"],
  api_version: "apps/v1",
  name: "deployments/status"
}
Link to this function

get_query_param(operation, key)

get_query_param(t(), atom()) :: any()

Get a query param of an operation

Examples

iex> operation = %K8s.Operation{query_params: %{foo: "bar"}}
...> K8s.Operation.get_query_param(operation, :foo)
"bar"
Link to this function

put_query_param(op, key, value)

put_query_param(t(), atom(), String.t() | K8s.Selector.t()) :: t()

Add a query param to an operation

Examples

iex> operation = %K8s.Operation{}
...> K8s.Operation.put_query_param(operation, "foo", "bar")
%K8s.Operation{query_params: %{"foo" => "bar"}}
Link to this function

to_path(operation)

to_path(t()) :: {:ok, String.t()} | {:error, :missing_required_param, [atom()]}

Converts a K8s.Operation into a URL path.