bonny v0.4.1 Bonny.Server.Reconciler behaviour

Continuously reconciles a set of kubernetes resources.

reconcile/1 will be executed asynchronously with each result returned from reconcilable_resources/0.

reconcilable_resources/0 has a default implementation of running K8s.Client.stream/2 with reconcile_operation/0.

For a working example of the Reconciler see Bonny.Server.Scheduler

Examples

Print every pod. Not very useful, but a simple copy-paste example.

defmodule PodPrinterReconciler do
  use Bonny.Server.Reconciler, frequency: 15

  @impl true
  def reconcile(pod) do
    IO.inspect(pod)
    :ok
  end

  @impl true
  def reconcile_operation(), do: K8s.Client.list("v1", :pods, namespace: "default")

  @impl true
  def reconcilable_resources() do
    operation = reconcile_operation()
    cluster = Bonny.Config.cluster_name()
    K8s.Client.stream(operation, cluster)
  end
end

PodPrinterReconciler.start_link()

A quick and dirty chaos monkey for pods. 20% chance of eviction every 15 seconds.

defmodule ChaosMonkeyReconciler do
  use Bonny.Server.Reconciler, frequency: 15
  @percent_chance_evicted 20

  @impl true
  def reconcile(pod) do
    chance = :rand.uniform(100)

    if chance < @percent_chance_evicted do
      my_function_to_evict_pod(pod)
    end
    :ok
  end

  @impl true
  def reconcile_operation(), do: K8s.Client.list("v1", :pods, namespace: :all)

  @impl true
  def reconcilable_resources() do
    operation = reconcile_operation()
    cluster = Bonny.Config.cluster_name()
    K8s.Client.stream(operation, cluster)
  end
end

ChaosMonkeyReconciler.start_link()

Reconcile a CRD's resources every 15 seconds

defmodule MyCustomResourceReconciler do
  use Bonny.Server.Reconciler, frequency: 15

  @impl true
  def reconcile(resource) do
    # You should do something much cooler than inspect here...
    IO.inspect(resource)
    :ok
  end

  @impl true
  def reconcile_operation() do
    K8s.Client.list("example.com/v1", "MyCustomResourceDef", namespace: :all)
  end

  @impl true
  def reconcilable_resources() do
    operation = reconcile_operation()
    cluster = Bonny.Config.cluster_name()
    K8s.Client.stream(operation, cluster)
  end
end

MyCustomResourceReconciler.start_link()

Link to this section Summary

Functions

Runs a Reconcilers reconcile/1 for each resource return by reconcilable_resources/0

Schedules a run of a started Reconciler

Callbacks

(Optional) List of resources to be reconciled.

Reconciles a resource. This will receive a list of resources from reconcilable_resources/0.

Link to this section Functions

Link to this function

run(module)
run(module()) :: no_return()

Runs a Reconcilers reconcile/1 for each resource return by reconcilable_resources/0

Link to this function

schedule(pid, frequency)
schedule(pid(), pos_integer()) :: reference()

Schedules a run of a started Reconciler

Link to this section Callbacks

Link to this callback

reconcilable_resources()
reconcilable_resources() :: {:ok, [map()]} | {:error, any()}

(Optional) List of resources to be reconciled.

Default implementation is to stream all resources (reconcile_operation/0) from the cluster (Bonny.Config.cluster_name/0).

Link to this callback

reconcile(map)
reconcile(map()) :: :ok | {:ok, any()} | {:error, any()}

Reconciles a resource. This will receive a list of resources from reconcilable_resources/0.

Link to this callback

reconcile_operation()
reconcile_operation() :: K8s.Operation.t()

K8s.Operation to reconcile.

Examples

  def reconcile_operation() do
    K8s.Client.list("v1", :pods, namespace: :all)
  end