k8s v0.3.1 K8s.Selector

Builds label selectors for K8s.Operations

Examples

Parse from a YAML map

iex> deployment = %{
...>   "kind" => "Deployment",
...>   "metadata" => %{
...>     "name" => "nginx",
...>     "labels" => %{
...>       "app" => "nginx",
...>       "tier" => "backend"
...>     }
...>   },
...>   "spec" => %{
...>     "selector" => %{
...>       "matchLabels" => %{
...>         "app" => "nginx"
...>       }
...>     },
...>     "template" => %{
...>       "metadata" => %{
...>         "labels" => %{
...>           "app" => "nginx",
...>           "tier" => "backend"
...>         }
...>       }
...>     }
...>   }
...> }
...> K8s.Selector.parse(deployment)
%K8s.Selector{match_labels: %{"app" => "nginx"}}

Provides a composable interface for building label selectors

iex> {"component", "redis"}
...> |> K8s.Selector.label()
...> |> K8s.Selector.label_in({"tier", "cache"})
...> |> K8s.Selector.label_not_in({"environment", "dev"})
...> |> K8s.Selector.label_exists("foo")
...> |> K8s.Selector.label_does_not_exist("bar")
%K8s.Selector{
  match_labels: %{"component" => "redis"},
  match_expressions: [
    %{"key" => "tier", "operator" => "In", "values" => ["cache"]},
    %{"key" => "environment", "operator" => "NotIn", "values" => ["dev"]},
    %{"key" => "foo", "operator" => "Exists"},
    %{"key" => "bar", "operator" => "DoesNotExist"}
  ]
}

Provides a composable interface for adding selectors to K8s.Operations.

iex> K8s.Client.get("v1", :pods)
...> |> K8s.Selector.label({"app", "nginx"})
...> |> K8s.Selector.label_in({"environment", ["qa", "prod"]})
%K8s.Operation{data: nil, api_version: "v1", label_selector: %K8s.Selector{match_expressions: [%{"key" => "environment", "operator" => "In", "values" => ["qa", "prod"]}], match_labels: %{"app" => "nginx"}}, method: :get, name: :pods, path_params: [], verb: :get}

Link to this section Summary

Functions

matchLabels helper that creates a composable K8s.Selector.

matchLabels helper that creates a composable K8s.Selector.

DoesNotExist expression helper that creates a composable K8s.Selector.

Exists expression helper that creates a composable K8s.Selector.

In expression helper that creates a composable K8s.Selector.

NotIn expression helper that creates a composable K8s.Selector.

Parses a "selector" map of "matchLabels" and "matchExpressions"

Returns a labelSelector query string value for a set of label expressions.

Returns a labelSelector query string value for a set of label matches.

Serializes a K8s.Selector to a labelSelector query string.

Link to this section Types

Link to this type

selector_or_operation_t()
selector_or_operation_t() :: t() | K8s.Operation.t()

Link to this type

t()
t() :: %K8s.Selector{match_expressions: [map()], match_labels: map()}

Link to this section Functions

Link to this function

label(arg)
label({binary() | atom(), binary()}) :: t()

matchLabels helper that creates a composable K8s.Selector.

Examples

iex> K8s.Selector.label({"component", "redis"})
%K8s.Selector{match_labels: %{"component" => "redis"}}
Link to this function

label(selector_or_operation, label)
label(selector_or_operation_t(), {binary() | atom(), binary()}) :: t()

matchLabels helper that creates a composable K8s.Selector.

Examples

iex> selector = K8s.Selector.label({"component", "redis"})
...> K8s.Selector.label(selector, {"environment", "dev"})
%K8s.Selector{match_labels: %{"component" => "redis", "environment" => "dev"}}
Link to this function

label_does_not_exist(key)
label_does_not_exist(binary()) :: t()

DoesNotExist expression helper that creates a composable K8s.Selector.

Examples

iex> K8s.Selector.label_does_not_exist("environment")
%K8s.Selector{match_expressions: [%{"key" => "environment", "operator" => "DoesNotExist"}]}
Link to this function

label_does_not_exist(selector_or_operation, key)
label_does_not_exist(selector_or_operation_t(), binary()) :: t()

Link to this function

label_exists(key)
label_exists(binary()) :: t()

Exists expression helper that creates a composable K8s.Selector.

Examples

iex> K8s.Selector.label_exists("environment")
%K8s.Selector{match_expressions: [%{"key" => "environment", "operator" => "Exists"}]}
Link to this function

label_exists(selector_or_operation, key)
label_exists(selector_or_operation_t(), binary()) :: t()

Link to this function

label_in(arg)
label_in({binary(), binary() | [binary()]}) :: t()

In expression helper that creates a composable K8s.Selector.

Examples

iex> K8s.Selector.label_in({"component", "redis"})
%K8s.Selector{match_expressions: [%{"key" => "component", "operator" => "In", "values" => ["redis"]}]}
Link to this function

label_in(selector_or_operation, label)
label_in(selector_or_operation_t(), {binary(), binary() | [binary()]}) :: t()

Link to this function

label_not_in(arg)
label_not_in({binary(), binary() | [binary()]}) :: t()

NotIn expression helper that creates a composable K8s.Selector.

Examples

iex> K8s.Selector.label_not_in({"component", "redis"})
%K8s.Selector{match_expressions: [%{"key" => "component", "operator" => "NotIn", "values" => ["redis"]}]}
Link to this function

label_not_in(selector_or_operation, label)
label_not_in(selector_or_operation_t(), {binary(), binary() | [binary()]}) ::
  t()

Link to this function

parse(arg1)
parse(map()) :: t()

Parses a "selector" map of "matchLabels" and "matchExpressions"

Examples

iex> selector = %{ ...> "matchLabels" => %{"component" => "redis"}, ...> "matchExpressions" => [ ...> %{"operator" => "In", "key" => "tier", "values" => ["cache"]}, ...> %{"operator" => "NotIn", "key" => "environment", "values" => ["dev"]} ...> ] ...> } ...> K8s.Selector.parse(selector) %K8s.Selector{match_labels: %{"component" => "redis"}, match_expressions: [%{"operator" => "In", "key" => "tier", "values" => ["cache"]},%{"operator" => "NotIn", "key" => "environment", "values" => ["dev"]}]}

Link to this function

serialize_match_expressions(exps)
serialize_match_expressions([map()]) :: [binary()]

Returns a labelSelector query string value for a set of label expressions.

For != matches, use a NotIn set-based expression.

Examples

Builds a query string for In expressions (kubectl get pods -l 'environment in (production,qa),tier in (frontend)):

iex> expressions = [
...>   %{"operator" => "In", "key" => "environment", "values" => ["production", "qa"]},
...>   %{"operator" => "In", "key" => "tier", "values" => ["frontend"]},
...> ]
...> K8s.Selector.serialize_match_expressions(expressions)
["environment in (production,qa)", "tier in (frontend)"]

Builds a query string for NotIn expressions (kubectl get pods -l 'environment notin (frontend)):

iex> expressions = [
...>   %{"operator" => "NotIn", "key" => "environment", "values" => ["frontend"]}
...> ]
...> K8s.Selector.serialize_match_expressions(expressions)
["environment notin (frontend)"]

Builds a query string for Exists expressions (kubectl get pods -l 'environment'):

iex> expressions = [
...>   %{"operator" => "Exists", "key" => "environment"}
...> ]
...> K8s.Selector.serialize_match_expressions(expressions)
["environment"]

Builds a query string for DoesNotExist expressions (kubectl get pods -l '!environment'):

iex> expressions = [
...>   %{"operator" => "DoesNotExist", "key" => "environment"}
...> ]
...> K8s.Selector.serialize_match_expressions(expressions)
["!environment"]
Link to this function

serialize_match_labels(labels)
serialize_match_labels(map()) :: [binary()]

Returns a labelSelector query string value for a set of label matches.

Examples

Builds a query string for a single label (kubectl get pods -l environment=production):

iex> K8s.Selector.serialize_match_labels(%{"environment" => "prod"})
["environment=prod"]

Builds a query string for multiple labels (kubectl get pods -l environment=production,tier=frontend):

iex> K8s.Selector.serialize_match_labels(%{"environment" => "prod", "tier" => "frontend"})
["environment=prod", "tier=frontend"]
Link to this function

to_s(selector)
to_s(t()) :: binary()

Serializes a K8s.Selector to a labelSelector query string.

Examples

iex> selector = K8s.Selector.label({"component", "redis"}) ...> K8s.Selector.to_s(selector) "component=redis"