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

12/30/2021

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?

-- Fabrice Jammes
kubebuilder
kubernetes
operator-sdk

2 Answers

1/9/2022

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
Source: StackOverflow

12/30/2021

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
Source: StackOverflow