Skip to content

Kubernetes Events Using Kubebuilder's Recorder

By default, the scaffolded operator will not have events configured.

Adding Recorder To The Reconciler

In the cmd/main.go file, you'll find each reconciler loop inited. Simple add the recorder to along side the client and scheme to the call like so:

if err := (&triggercontroller.TriggerOperatorReconciler{
    Client:   mgr.GetClient(),
    Scheme:   mgr.GetScheme(),
    Recorder: mgr.GetEventRecorderFor("trigger-operator-controller"),
}).SetupWithManager(mgr); err != nil {
    setupLog.Error(err, "unable to create controller", "controller", "TriggerOperator")
    os.Exit(1)
}

and add it to the reconciler found in controller/internal/controller/<group>/<controller name>_controller.go like so:

// TriggerOperatorReconciler reconciles a TriggerOperator object
type TriggerOperatorReconciler struct {
	client.Client
	Scheme   *runtime.Scheme
	Recorder record.EventRecorder
}

Using Events

Creating an event is an important part of reconciling resources. Events are are a single point in time event and not a declarative state. Events will have one of two states, normal and warning. Essentially boiling down to expected and unexpected events.

r.Recorder.Event(&cell, events.EventTypeNormal, "Deleted", "Deleted the TriggerOperator related resources")
Powered by Vyra, Sweat and Tears™