View Source K8sWebhoox.ResourceConversion.Handler behaviour (k8s_webhoox v0.1.0)

A Helper module for resource conversion request handling.

When used, it turns the using module into a Pluggable step which can be used with K8sWebhoox.Plug.

post "/k8s-webhooks/resource-conversion",
  to: K8sWebhoox.Plug,
  init_opts: [webhook_handler: MyOperator.ResourceConversionHandler]

usage

Usage

Declare convert to convert a given resource to a desired_api_version.

defmodule MyOperator.ResourceConversionHandler do
  use K8sWebhoox.AdmissionControl.Handler

  def convert(
         %{"apiVersion" => "example.com/v1beta1", "kind" => "MyResource"} = resource,
         "example.com/v1"
       ) do

    # return {:ok, mutated_resource}
    {:ok, put_in(resource, ~w(metadata labels), %{"foo" => "bar"})}
  end

  def convert(
         %{"apiVersion" => "example.com/v1alpha1", "kind" => "MyResource"} = resource,
         "example.com/v1"
       ) do

    # return {:error, message}
    {:error, "V1Alpha1 cannot be converted to V1."}
  end
end

Link to this section Summary

Callbacks

Defines a handler for a converstion request of the given resource to the given desired_api_version. See moduledoc for an example.

Link to this section Callbacks

Link to this callback

convert(resource, desired_api_version)

View Source
@callback convert(resource :: map(), desired_api_version :: binary()) ::
  {:ok, [{:resource, map()}]} | {:error, message :: binary()}

Defines a handler for a converstion request of the given resource to the given desired_api_version. See moduledoc for an example.