Kuberator - Kubernetes Operator Framework
Kuberatoris a Kubernetes Operator Framework designed to simplify the process of
building Kubernetes Operators. It is still in its early stages and a work in progress.
Usage
It's best to follow an example to understand how to use kuberator in it's current form.
use Arc;
use async_trait;
use Action;
use Config;
use Api;
use Client;
use CustomResource;
use Result as KubeResult;
use Context;
use Finalize;
use Reconcile;
use JsonSchema;
use Deserialize;
use Serialize;
// First, we need to define a custom resource that the operator will manage.
// The core of the operator is the implementation of the [Reconcile] trait, which requires
// us to first implement the [Context] and [Finalize] traits for certain structs.
// The [Finalize] trait will be implemented on a Kubernetes repository-like structure
// and is responsible for handling the finalizer logic.
// The [Context] trait must be implemented on a struct that serves as the core of the
// operator. It contains the logic for handling the custom resource object, including
// creation, updates, and deletion.
// The final step is to implement the [Reconcile] trait on a struct that holds the context.
// The Reconciler is responsible for starting the controller runtime and managing the
// reconciliation loop.
// The [destruct] function is used to retrieve the Api, Config, and context.
// And that’s basically it!
// Now we can wire everything together in the main function and start the reconciler.
// It will continuously watch for custom resource objects and invoke the [handle_apply] and
// [handle_cleanup] functions as part of the reconciliation loop.
async
Error Handling
Kuberator provides a dedicated error type. When implementing [Reconcile::handle_apply] and [Reconcile::handle_cleanup], you must return this error in your Result, or use [kuberator::error::Result] directly.
To convert your custom error into a Kuberator error, implement From for your error
type and wrap it using Error::Anyhow.
Your error.rs file could look something like this:
use Debug;
use Error as KubeError;
use Error as ThisError;
pub type Result<T> = Result;
With this approach, you can conveniently handle your custom errors using the ? operator
and return them as Kuberator errors.
Status Object Handling
Kuberator provides helper methods to facilitate the Observed Generation Pattern. To use this pattern, you need to implement the ObserveGeneration trait for your status object.
Let's say this is your status.rs file:
use ObserveGeneration;
With this implementation, you can utilize the update_status() method provided by the
[Finalize] trait.
This allows you to:
(a) Keep your resource status up to date.
(b) Compare it against the current generation of the resource (object.meta().generation)
to determine whether you have already processed this version or if it is a new show in
the reconciliation cycle.
This pattern is particularly useful for ensuring idempotency in your reconciliation logic.