View Source K8s.Selector (k8s v1.1.1)
Builds label selectors and field selectors for K8s.Operation
s
examples
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.Operation
s.
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", query_params: [labelSelector: %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
.
Checks whether a resource matches a single selector matchExpressions
Checks if a K8s.Resource
matches all matchExpressions
using a logical AND
Checks if a K8s.Resource
matches a single label
Checks if a K8s.Resource
matches all matchLabels
using a logcal AND
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
Specs
selector_or_operation_t() :: t() | K8s.Operation.t()
Specs
Link to this section Functions
Specs
matchLabels
helper that creates a composable K8s.Selector
.
examples
Examples
iex> K8s.Selector.label({"component", "redis"})
%K8s.Selector{match_labels: %{"component" => "redis"}}
iex> K8s.Selector.label(%{"component" => "redis", "env" => "prod"})
%K8s.Selector{match_labels: %{"component" => "redis", "env" => "prod"}}
Specs
label( selector_or_operation_t(), {binary() | atom(), binary()} ) :: selector_or_operation_t()
matchLabels
helper that creates a composable K8s.Selector
.
examples
Examples
iex> selector = K8s.Selector.label({"component", "redis"})
...> K8s.Selector.label(selector, {"environment", "dev"})
%K8s.Selector{match_labels: %{"component" => "redis", "environment" => "dev"}}
Specs
DoesNotExist
expression helper that creates a composable K8s.Selector
.
examples
Examples
iex> K8s.Selector.label_does_not_exist("environment")
%K8s.Selector{match_expressions: [%{"key" => "environment", "operator" => "DoesNotExist"}]}
Specs
label_does_not_exist(selector_or_operation_t(), binary()) :: selector_or_operation_t()
Specs
Exists
expression helper that creates a composable K8s.Selector
.
examples
Examples
iex> K8s.Selector.label_exists("environment")
%K8s.Selector{match_expressions: [%{"key" => "environment", "operator" => "Exists"}]}
Specs
label_exists(selector_or_operation_t(), binary()) :: selector_or_operation_t()
Specs
In
expression helper that creates a composable K8s.Selector
.
examples
Examples
iex> K8s.Selector.label_in({"component", "redis"})
%K8s.Selector{match_expressions: [%{"key" => "component", "operator" => "In", "values" => ["redis"]}]}
Specs
label_in( selector_or_operation_t(), {binary(), binary() | [binary()]} ) :: selector_or_operation_t()
Specs
NotIn
expression helper that creates a composable K8s.Selector
.
examples
Examples
iex> K8s.Selector.label_not_in({"component", "redis"})
%K8s.Selector{match_expressions: [%{"key" => "component", "operator" => "NotIn", "values" => ["redis"]}]}
Specs
label_not_in( selector_or_operation_t(), {binary(), binary() | [binary()]} ) :: selector_or_operation_t()
Specs
Checks whether a resource matches a single selector matchExpressions
examples
Examples
When an In
expression matches
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "prod"}}}
...> expr = %{"operator" => "In", "key" => "env", "values" => ["prod", "qa"]}
...> K8s.Selector.match_expression?(resource, expr)
true
When an In
expression doesnt match
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "dev"}}}
...> expr = %{"operator" => "In", "key" => "env", "values" => ["prod", "qa"]}
...> K8s.Selector.match_expression?(resource, expr)
false
When an NotIn
expression matches
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "dev"}}}
...> expr = %{"operator" => "NotIn", "key" => "env", "values" => ["prod"]}
...> K8s.Selector.match_expression?(resource, expr)
true
When an NotIn
expression doesnt match
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "dev"}}}
...> expr = %{"operator" => "NotIn", "key" => "env", "values" => ["dev"]}
...> K8s.Selector.match_expression?(resource, expr)
false
When an Exists
expression matches
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "dev"}}}
...> expr = %{"operator" => "Exists", "key" => "env"}
...> K8s.Selector.match_expression?(resource, expr)
true
When an Exists
expression doesnt match
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "dev"}}}
...> expr = %{"operator" => "Exists", "key" => "tier"}
...> K8s.Selector.match_expression?(resource, expr)
false
When an DoesNotExist
expression matches
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "dev"}}}
...> expr = %{"operator" => "DoesNotExist", "key" => "tier"}
...> K8s.Selector.match_expression?(resource, expr)
true
When an DoesNotExist
expression doesnt match
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "dev"}}}
...> expr = %{"operator" => "DoesNotExist", "key" => "env"}
...> K8s.Selector.match_expression?(resource, expr)
false
Specs
Checks if a K8s.Resource
matches all matchExpressions
using a logical AND
examples
Examples
Accepts K8s.Selector
s:
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "prod", "tier" => "frontend"}}}
...> expr1 = %{"operator" => "In", "key" => "env", "values" => ["prod", "qa"]}
...> expr2 = %{"operator" => "Exists", "key" => "tier"}
...> selector = %K8s.Selector{match_expressions: [expr1, expr2]}
...> K8s.Selector.match_expressions?(resource, selector)
true
Accepts map
s:
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "prod", "tier" => "frontend"}}}
...> expr1 = %{"operator" => "In", "key" => "env", "values" => ["prod", "qa"]}
...> expr2 = %{"operator" => "Exists", "key" => "tier"}
...> K8s.Selector.match_expressions?(resource, [expr1, expr2])
true
Returns false
when not matching all expressions:
iex> resource = %{"kind" => "Node", "metadata" => %{"labels" => %{"env" => "prod", "tier" => "frontend"}}}
...> expr1 = %{"operator" => "In", "key" => "env", "values" => ["prod", "qa"]}
...> expr2 = %{"operator" => "Exists", "key" => "foo"}
...> K8s.Selector.match_expressions?(resource, [expr1, expr2])
false
Specs
Checks if a K8s.Resource
matches a single label
Specs
Checks if a K8s.Resource
matches all matchLabels
using a logcal AND
examples
Examples
Accepts K8s.Selector
s:
iex> labels = %{"env" => "prod", "tier" => "frontend"}
...> selector = %K8s.Selector{match_labels: labels}
...> resource = K8s.Resource.build("v1", "Pod", "default", "test", labels)
...> K8s.Selector.match_labels?(resource, selector)
true
Accepts maps:
iex> labels = %{"env" => "prod", "tier" => "frontend"}
...> resource = K8s.Resource.build("v1", "Pod", "default", "test", labels)
...> K8s.Selector.match_labels?(resource, labels)
true
Returns false
when not matching all labels:
iex> not_a_match = %{"env" => "prod", "tier" => "frontend", "nope" => "not-a-match"}
...> resource = K8s.Resource.build("v1", "Pod", "default", "test", %{"env" => "prod", "tier" => "frontend"})
...> K8s.Selector.match_labels?(resource, not_a_match)
false
Specs
Parses a "selector"
map of "matchLabels"
and "matchExpressions"
examples
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"]}]}
Specs
Returns a labelSelector
query string value for a set of label expressions.
For !=
matches, use a NotIn
set-based expression.
examples
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"]
Specs
Returns a labelSelector
query string value for a set of label matches.
examples
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"]
Specs
Serializes a K8s.Selector
to a labelSelector
query string.
examples
Examples
iex> selector = K8s.Selector.label({"component", "redis"}) ...> K8s.Selector.to_s(selector) "component=redis"