Packages
k8s
0.4.0
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
Current section
Files
Jump to
Current section
Files
lib/k8s/resource/field_accessors.ex
defmodule K8s.Resource.FieldAccessors do
@moduledoc "Helper functions for accessing common fields"
@doc """
Returns the kind of k8s resource.
## Examples
iex> K8s.Resource.FieldAccessors.kind(%{"kind" => "Deployment"})
"Deployment"
"""
@spec kind(map()) :: binary() | nil
def kind(%{} = resource), do: resource["kind"]
@doc """
Returns the apiVersion of k8s resource.
## Examples
iex> K8s.Resource.FieldAccessors.api_version(%{"apiVersion" => "apps/v1"})
"apps/v1"
"""
@spec api_version(map()) :: binary() | nil
def api_version(%{} = resource), do: resource["apiVersion"]
@doc """
Returns the metadata of k8s resource.
## Examples
iex> K8s.Resource.FieldAccessors.metadata(%{"metadata" => %{"name" => "nginx", "namespace" => "foo"}})
%{"name" => "nginx", "namespace" => "foo"}
"""
@spec metadata(map()) :: map() | nil
def metadata(%{} = resource), do: resource["metadata"]
@doc """
Returns the name of k8s resource.
## Examples
iex> K8s.Resource.FieldAccessors.name(%{"metadata" => %{"name" => "nginx", "namespace" => "foo"}})
"nginx"
"""
@spec name(map()) :: binary() | nil
def name(%{} = resource), do: get_in(resource, ~w(metadata name))
@doc """
Returns the namespace of k8s resource.
## Examples
iex> K8s.Resource.FieldAccessors.namespace(%{"metadata" => %{"name" => "nginx", "namespace" => "foo"}})
"foo"
"""
@spec namespace(map()) :: binary()
def namespace(%{} = resource), do: get_in(resource, ~w(metadata namespace))
@doc """
Returns the labels of k8s resource.
## Examples
iex> K8s.Resource.FieldAccessors.labels(%{"metadata" => %{"labels" => %{"env" => "test"}}})
%{"env" => "test"}
"""
@spec labels(map()) :: map()
def labels(%{} = resource), do: get_in(resource, ~w(metadata labels)) || %{}
@doc """
Returns the value of a k8s resource's label.
## Examples
iex> K8s.Resource.FieldAccessors.label(%{"metadata" => %{"labels" => %{"env" => "test"}}}, "env")
"test"
"""
@spec label(map(), binary) :: binary() | nil
def label(%{} = resource, name), do: get_in(resource, ["metadata", "labels", name])
@doc """
Returns the annotations of k8s resource.
## Examples
iex> K8s.Resource.FieldAccessors.annotations(%{"metadata" => %{"annotations" => %{"env" => "test"}}})
%{"env" => "test"}
"""
@spec annotations(map()) :: map()
def annotations(%{} = resource), do: get_in(resource, ~w(metadata annotations)) || %{}
@doc """
Returns the value of a k8s resource's annotation.
## Examples
iex> K8s.Resource.FieldAccessors.annotation(%{"metadata" => %{"annotations" => %{"env" => "test"}}}, "env")
"test"
"""
@spec annotation(map(), binary) :: binary() | nil
def annotation(%{} = resource, name), do: get_in(resource, ["metadata", "annotations", name])
@doc """
Check if a label is present.
## Examples
iex> K8s.Resource.FieldAccessors.has_label?(%{"metadata" => %{"labels" => %{"env" => "test"}}}, "env")
true
iex> K8s.Resource.FieldAccessors.has_label?(%{"metadata" => %{"labels" => %{"env" => "test"}}}, "foo")
false
"""
@spec has_label?(map(), binary()) :: boolean()
def has_label?(%{} = resource, name), do: resource |> labels() |> Map.has_key?(name)
@doc """
Check if an annotation is present.
## Examples
iex> K8s.Resource.FieldAccessors.has_annotation?(%{"metadata" => %{"annotations" => %{"env" => "test"}}}, "env")
true
iex> K8s.Resource.FieldAccessors.has_annotation?(%{"metadata" => %{"annotations" => %{"env" => "test"}}}, "foo")
false
"""
@spec has_annotation?(map(), binary()) :: boolean()
def has_annotation?(%{} = resource, name), do: resource |> annotations() |> Map.has_key?(name)
end