Bonny.Resource (bonny v1.5.0)

Copy Markdown View Source

Helper functions for dealing with kubernetes resources.

Summary

Functions

Add an owner reference to the given resource.

Applies the given resource to the cluster.

Applies the given resource to the cluster.

Applies the status subresource of the given resource to the cluster. If the given resource doesn't contain a status object, nothing is done and :noop is returned.

Removes .metadata.managedFields from the resource.

Removes .metadata.resourceVersion from the resource.

Returns a tuple in the form

Get a reference to the given resource

Set a label on the given resource.

Sets .status.observedGeneration to .metadata.generation

Types

t()

@type t() :: map()

Functions

add_owner_reference(resource, owner, opts \\ [])

@spec add_owner_reference(t(), map(), keyword(boolean())) :: t()

Add an owner reference to the given resource.

Example

iex> owner = %{
...>   "apiVersion" => "example.com/v1",
...>   "kind" => "Orange",
...>   "metadata" => %{
...>     "name" => "annoying",
...>     "namespace" => "default",
...>     "uid" => "e19b6f40-3293-11ed-a261-0242ac120002"
...>   }
...> }
...> resource = %{
...>   "apiVersion" => "v1",
...>   "kind" => "Pod",
...>   "metadata" => %{"name" => "nginx", "namespace" => "default"}
...>   # spec
...> }
...> Bonny.Resource.add_owner_reference(resource, owner)
%{
  "apiVersion" => "v1",
  "kind" => "Pod",
  "metadata" => %{
    "name" => "nginx",
    "namespace" => "default",
    "ownerReferences" => [%{
      "apiVersion" => "example.com/v1",
      "blockOwnerDeletion" => false,
      "controller" => true,
      "kind" => "Orange",
      "name" => "annoying",
      "uid" => "e19b6f40-3293-11ed-a261-0242ac120002"
    }]
  }
}

apply(resource, conn, opts)

Applies the given resource to the cluster.

apply_async(resources, conn, opts \\ [])

@spec apply_async([t()], K8s.Conn.t(), Keyword.t()) :: [
  {t(), K8s.Client.Runner.Base.result_t()}
]

Applies the given resource to the cluster.

apply_status(resource, conn, opts \\ [])

@spec apply_status(t(), K8s.Conn.t(), Keyword.t()) ::
  K8s.Client.Runner.Base.result_t() | :noop

Applies the status subresource of the given resource to the cluster. If the given resource doesn't contain a status object, nothing is done and :noop is returned.

drop_managed_fields(resource)

@spec drop_managed_fields(t()) :: t()

Removes .metadata.managedFields from the resource.

drop_rv(resource)

@spec drop_rv(t()) :: t()

Removes .metadata.resourceVersion from the resource.

gvkn(resource)

@spec gvkn(t()) ::
  {namespace_name :: binary(), api_version :: binary(), kind :: binary()}

Returns a tuple in the form

  • {apiVersion, kind, namespace/name} for namespaced resources
  • {apiVersion, kind, name} for cluster scoped resources

resource_reference(resource)

Get a reference to the given resource

Example

iex> resource = %{
...>   "apiVersion" => "example.com/v1",
...>   "kind" => "Orange",
...>   "metadata" => %{
...>     "name" => "annoying",
...>     "namespace" => "default",
...>     "uid" => "e19b6f40-3293-11ed-a261-0242ac120002"
...>   }
...> }
...> Bonny.Resource.resource_reference(resource)
%{
  "apiVersion" => "example.com/v1",
  "kind" => "Orange",
  "name" => "annoying",
  "namespace" => "default",
  "uid" => "e19b6f40-3293-11ed-a261-0242ac120002"
}

set_label(resource, label, value)

@spec set_label(t(), binary(), binary()) :: t()

Set a label on the given resource.

Example

iex> resource = %{
...>   "apiVersion" => "v1",
...>   "kind" => "Pod",
...>   "metadata" => %{"name" => "nginx", "namespace" => "default"}
...>   # spec
...> }
...> Bonny.Resource.set_label(resource, "app.kubernetes.io/managed-by", "my-operator")
%{
  "apiVersion" => "v1",
  "kind" => "Pod",
  "metadata" => %{
    "name" => "nginx",
    "namespace" => "default",
    "labels" => %{
      "app.kubernetes.io/managed-by" => "my-operator"
    }
  }
}

set_observed_generation(resource)

@spec set_observed_generation(t()) :: t()

Sets .status.observedGeneration to .metadata.generation