Fabrice Jammes
Fabrice Jammes

Reputation: 3205

How to send Events with Kubebuilder-v3 / operator-sdk

The Kubebuilder V3 documentation explains that it talks about "How to batch multiple events into a single reconciliation call". However, I could not find any information about event management in this documentation.

Could you please provide information/code sample about how to send Events with Kubebuilder-v3/operator-sdk?

Upvotes: 2

Views: 1520

Answers (3)

Lewis Macdonald
Lewis Macdonald

Reputation: 11

Since you ask about sending events, you should use an EventRecorder.

The approach to get an event recorder as documented in book-v1 used to be

mgr.GetRecorder("containerset-controller")

However, an equivalent doc doesn't seem to exist in the updated v3 documentation any more. The new alternative is:

mgr.GetEventRecorderFor("containerset-controller")

e.g.

func newReconciler(mgr manager.Manager) reconcile.Reconciler {
  return &ReconcileContainerSet{
    Client:   mgr.GetClient(),
    scheme:   mgr.GetScheme(),
    recorder: mgr.GetEventRecorderFor("containerset-controller"),
  }
}

the resulting record.EventRecorder can be used in your reconciler like before:

recorder.Event(&obj, apiv1.EventTypeNormal, "Created", "something was created")

However, the quoted extract

"How to batch multiple events into a single reconciliation call".

Is referring to something quite different; namely this:

reconcile.Requests for the same Name / Namespace are batched and deduplicated when they are enqueued. This allows Controllers to gracefully handle a high volume of events for a single object. Multiplexing multiple event Sources to a single object Type will batch requests across events for different object types.

Upvotes: 1

Fabrice Jammes
Fabrice Jammes

Reputation: 3205

It seems this page might help in understanding how to send events: https://book-v1.book.kubebuilder.io/beyond_basics/creating_events.html using the standard client-go EventRecorder

However, it is not up to date for Kubebuilder v3.

Thanks @coderanger for your help on this topic, on the k8s stack channel!

Upvotes: 0

Mikołaj Głodziak
Mikołaj Głodziak

Reputation: 5277

This part from the official documentation should answer your question:

This business logic of the Controller is implemented in the Reconcile function. This function takes the Namespace and Name of a ContainerSet, allowing multiple Events to be batched together into a single Reconcile call. The function shown here creates or updates a Deployment using the replicas and image specified in ContainerSet.Spec. Note that it sets an OwnerReference for the Deployment to enable garbage collection on the Deployment once the ContainerSet is deleted.

  1. Read the ContainerSet using the NamespacedName
  2. If there is an error or it has been deleted, return
  3. Create the new desired DeploymentSpec from the ContainerSetSpec
  4. Read the Deployment and compare the Deployment.Spec to the ContainerSet.Spec
  5. If the observed Deployment.Spec does not match the desired spec - Deployment was not found: create a new Deployment - Deployment was found and changes are needed: update the Deployment

There you can also find example with the code.

Upvotes: 2

Related Questions