View Source K8s.Operation (k8s v2.4.2)

Encapsulates Kubernetes REST API operations.

Summary

Types

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

Acceptable patch types

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 parameters. Merged w/ params provided to any K8s.Client.Runner. K8s.Client.Runner options win.
  • header_params - Header parameters.

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

Gets a K8s.Selector on the operation.

Set the connection object on the operation

Add a query param to an operation

Puts a K8s.Selector on the operation.

Converts a K8s.Operation into a URL path.

Types

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

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

@type patch_type() :: :strategic_merge | :merge | :json_merge | :apply

Acceptable patch types

@type t() :: %K8s.Operation{
  api_version: binary(),
  conn: K8s.Conn.t() | nil,
  data: map() | nil,
  header_params: keyword(),
  method: atom(),
  name: name_t(),
  path_params: keyword(),
  query_params: keyword(),
  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 parameters. Merged w/ params provided to any K8s.Client.Runner. K8s.Client.Runner options win.
  • header_params - Header parameters.

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"],
  header_params: ["Content-Type": "application/json"]
}

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"],
  header_params: ["Content-Type": "application/json"]
}

Functions

Link to this function

build(verb, resource, opts \\ [])

View Source
@spec build(atom(), map(), keyword()) :: 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, opts \\ [])

View Source
@spec build(atom(), binary(), name_t(), keyword(), map() | nil, keyword()) :: 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_label_selector(operation)

View Source
This function is deprecated. Use get_selector/1.
@spec get_label_selector(t()) :: K8s.Selector.t()

See K8s.Operation.get_selector/1.

Link to this function

get_query_param(operation, key)

View Source
@spec get_query_param(t(), atom()) :: any()

Get a query param of an operation

Examples

Using a keyword list of params:

iex> operation = %K8s.Operation{query_params: [foo: "bar"]}
...> K8s.Operation.get_query_param(operation, :foo)
"bar"
@spec get_selector(t()) :: K8s.Selector.t()

Gets a K8s.Selector on the operation.

Examples

iex> operation = %K8s.Operation{query_params: [labelSelector: K8s.Selector.label({"component", "redis"})]}
...> K8s.Operation.get_selector(operation)
%K8s.Selector{
  match_expressions: [],
  match_labels: %{"component" => {"=", "redis"}}
}
Link to this function

put_conn(operation, conn)

View Source
@spec put_conn(t(), K8s.Conn.t()) :: t()

Set the connection object on the operation

Examples

iex> operation = %K8s.Operation{query_params: [foo: "bar"]}
...> conn = %K8s.Conn{}
...> operation = K8s.Operation.put_conn(operation, conn)
...> match?(%K8s.Operation{conn: %K8s.Conn{}}, operation)
true
Link to this function

put_label_selector(op, selector)

View Source
This function is deprecated. Use put_selector/2.
@spec put_label_selector(t(), K8s.Selector.t()) :: t()

See K8s.Operation.put_selector/2.

Link to this function

put_query_param(op, opts)

View Source
@spec put_query_param(t(), list() | K8s.Selector.t()) :: t()
Link to this function

put_query_param(op, key, value)

View Source
@spec put_query_param(t(), any(), String.t() | K8s.Selector.t()) :: t()

Add a query param to an operation

Examples

Using a keyword list of params:

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

put_selector(op, selector)

View Source
@spec put_selector(t(), K8s.Selector.t()) :: t()

Puts a K8s.Selector on the operation.

Examples

iex> operation = %K8s.Operation{}
...> selector = K8s.Selector.label({"component", "redis"})
...> K8s.Operation.put_selector(operation, selector)
%K8s.Operation{
  query_params: [
    labelSelector: %K8s.Selector{
      match_expressions: [],
      match_labels: %{"component" => {"=", "redis"}}
    }
  ]
}
@spec to_path(t()) :: {:ok, String.t()} | {:error, K8s.Operation.Error.t()}

Converts a K8s.Operation into a URL path.