2

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?

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
Fabrice Jammes
  • 2,275
  • 1
  • 26
  • 39

3 Answers3

2

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.

Mikołaj Głodziak
  • 4,775
  • 7
  • 28
  • Thanks for your answer, but it does not apply to Kubebuilder V3 (the link you provide goes to obsolete documentation). In addition, on my side, I'd like to send events, not to manage received events. – Fabrice Jammes Dec 31 '21 at 13:31
  • 1
    Then it looks like something is actually missing from the official documentation for kubebuilder v3. Also, I do not see any part from the doc that relates to this. – Mikołaj Głodziak Jan 05 '22 at 08:33
1

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.

0

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!

Fabrice Jammes
  • 2,275
  • 1
  • 26
  • 39