<p>I'm writing a Kubernetes Operator in Go and I would like to generate events in the same way Pods do, i.e. at each point of the reconciliation I want to write an event which can be examined using <code>kubectl describe myresource</code>.</p>
<p>I found the package that would allow me to do that, but I don't understand how to use it: <a href="https://github.com/kubernetes/client-go/blob/master/tools/record/event.go" rel="nofollow noreferrer">https://github.com/kubernetes/client-go/blob/master/tools/record/event.go</a></p>
<p>Example skeleton code:</p>
<pre class="lang-golang prettyprint-override"><code>type MyResourceReconciler struct {
    client.Client
    Log    logr.Logger
    Scheme *runtime.Scheme
}

var logger logr.Logger

func (r *MyResourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
    return ctrl.NewControllerManagedBy(mgr).
        Named(&quot;MyResource-controller&quot;).
        For(&amp;v1.MyResource{}).
        Complete(r)
}

func (r *MyResourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    logger = r.Log.V(0).WithValues(&quot;MyResource&quot;, req.NamespacedName)
    logger.Info(&quot;reconcile called&quot;)

    // TODO: Record event for req.NamespacedName

    return reconcile.Result{}, nil
}
</code></pre>

Cris
<p>The Kubebuilder v1 book has a good example on how to <em>create</em> and <em>write</em> <code>Events</code> using an <a href="https://github.com/kubernetes/client-go/blob/master/tools/record/event.go#L88" rel="nofollow noreferrer">EventRecorder</a> from <code>client-go</code>.</p>
<p>See <a href="https://book-v1.book.kubebuilder.io/beyond_basics/creating_events.html" rel="nofollow noreferrer">Kubebuilder v1 book - Create Events and Write Events</a></p>
