k8s v0.3.0 K8s.Resource
Manifest attribute helpers
Link to this section Summary
Functions
Create a list of resource Map
s from a YAML file with multi object annotation.
Returns the value of a k8s resource's annotation.
Returns the annotations of k8s resource.
Returns the apiVersion of k8s resource.
Helper for building a kubernetes' resource Map
Deserializes CPU quantity
Create a resource Map
from a YAML file.
Check if an annotation is present.
Check if a label is present.
Returns the kind of k8s resource.
Returns the value of a k8s resource's label.
Returns the labels of k8s resource.
Deserializes memory quantity
Returns the metadata of k8s resource.
Returns the name of k8s resource.
Returns the namespace of k8s resource.
Link to this section Functions
all_from_file!(path, assigns)
Create a list of resource Map
s from a YAML file with multi object annotation.
Raises File.Error
when the file does not exist.
Examples
iex> opts = [namespace: "default", name: "nginx", image: "nginx:nginx:1.7.9"]
...> K8s.Resource.all_from_file!("test/support/helm-chart.yaml", opts)
[
%{
"apiVersion" => "v1",
"kind" => "Namespace",
"metadata" => %{"name" => "default"}
},
%{
"apiVersion" => "apps/v1",
"kind" => "Deployment",
"metadata" => %{
"labels" => %{"app" => "nginx"},
"name" => "nginx-deployment",
"namespace" => "default"
},
"spec" => %{
"replicas" => 3,
"selector" => %{"matchLabels" => %{"app" => "nginx"}},
"template" => %{
"metadata" => %{"labels" => %{"app" => "nginx"}},
"spec" => %{
"containers" => [
%{
"image" => "nginx:nginx:1.7.9",
"name" => "nginx",
"ports" => [%{"containerPort" => 80}]
}
]
}
}
}
}
]
annotation(resource, name)
Returns the value of a k8s resource's annotation.
Examples
iex> K8s.Resource.annotation(%{"metadata" => %{"annotations" => %{"env" => "test"}}}, "env")
"test"
annotations(resource)
Returns the annotations of k8s resource.
Examples
iex> K8s.Resource.annotations(%{"metadata" => %{"annotations" => %{"env" => "test"}}})
%{"env" => "test"}
api_version(resource)
Returns the apiVersion of k8s resource.
Examples
iex> K8s.Resource.api_version(%{"apiVersion" => "apps/v1"})
"apps/v1"
build(api_version, kind)
Helper for building a kubernetes' resource Map
Examples
iex> K8s.Resource.build("v1", "Pod")
%{"apiVersion" => "v1", "kind" => "Pod", "metadata" => %{}}
iex> K8s.Resource.build("v1", "Namespace", "foo")
%{"apiVersion" => "v1", "kind" => "Namespace", "metadata" => %{"name" => "foo"}}
iex> K8s.Resource.build("v1", "Pod", "default", "foo")
%{"apiVersion" => "v1", "kind" => "Pod", "metadata" => %{"namespace" => "default", "name" => "foo"}}
build(api_version, kind, name)
build(api_version, kind, namespace, name)
cpu(str)
Deserializes CPU quantity
Examples
Parses whole values
iex> K8s.Resource.cpu("3")
3
Parses millicpu values
iex> K8s.Resource.cpu("500m")
0.5
Parses decimal values
iex> K8s.Resource.cpu("1.5")
1.5
from_file!(path, assigns)
Create a resource Map
from a YAML file.
Raises File.Error
when the file does not exist.
Examples
iex> opts = [namespace: "default", name: "nginx", image: "nginx:nginx:1.7.9"]
...> K8s.Resource.from_file!("test/support/deployment.yaml", opts)
%{
"apiVersion" => "apps/v1",
"kind" => "Deployment",
"metadata" => %{
"labels" => %{"app" => "nginx"},
"name" => "nginx-deployment",
"namespace" => "default"
},
"spec" => %{
"replicas" => 3,
"selector" => %{"matchLabels" => %{"app" => "nginx"}},
"template" => %{
"metadata" => %{"labels" => %{"app" => "nginx"}},
"spec" => %{
"containers" => [
%{
"image" => "nginx:nginx:1.7.9",
"name" => "nginx",
"ports" => [%{"containerPort" => 80}]
}
]
}
}
}
}
has_annotation?(resource, name)
Check if an annotation is present.
Examples
iex> K8s.Resource.has_annotation?(%{"metadata" => %{"annotations" => %{"env" => "test"}}}, "env")
true
iex> K8s.Resource.has_annotation?(%{"metadata" => %{"annotations" => %{"env" => "test"}}}, "foo")
false
has_label?(resource, name)
Check if a label is present.
Examples
iex> K8s.Resource.has_label?(%{"metadata" => %{"labels" => %{"env" => "test"}}}, "env")
true
iex> K8s.Resource.has_label?(%{"metadata" => %{"labels" => %{"env" => "test"}}}, "foo")
false
kind(resource)
Returns the kind of k8s resource.
Examples
iex> K8s.Resource.kind(%{"kind" => "Deployment"})
"Deployment"
label(resource, name)
Returns the value of a k8s resource's label.
Examples
iex> K8s.Resource.label(%{"metadata" => %{"labels" => %{"env" => "test"}}}, "env")
"test"
labels(resource)
Returns the labels of k8s resource.
Examples
iex> K8s.Resource.labels(%{"metadata" => %{"labels" => %{"env" => "test"}}})
%{"env" => "test"}
memory(str)
Deserializes memory quantity
Examples
Parses whole values
iex> K8s.Resource.memory("1000000")
1000000
Parses decimal values
iex> K8s.Resource.memory("10.75")
10.75
Parses decimalSI values
iex> K8s.Resource.memory("10M")
10000000
Parses binarySI suffixes
iex> K8s.Resource.memory("50Mi")
52428800
Returns the numeric value when the suffix is unrecognized
iex> K8s.Resource.memory("50Foo")
50
metadata(resource)
Returns the metadata of k8s resource.
Examples
iex> K8s.Resource.metadata(%{"metadata" => %{"name" => "nginx", "namespace" => "foo"}})
%{"name" => "nginx", "namespace" => "foo"}
name(resource)
Returns the name of k8s resource.
Examples
iex> K8s.Resource.name(%{"metadata" => %{"name" => "nginx", "namespace" => "foo"}})
"nginx"
namespace(resource)
Returns the namespace of k8s resource.
Examples
iex> K8s.Resource.namespace(%{"metadata" => %{"name" => "nginx", "namespace" => "foo"}})
"foo"