Kubebuilder: No way to get event recorder instance

Created on 3 Jan 2019  路  4Comments  路  Source: kubernetes-sigs/kubebuilder

I could see Recorder instance set for sigs.k8s.io/controller-runtime/pkg/internal/controller in "sigs.k8s.io/controller-runtime/pkg/controller" 'New' method,
But there is no way to use that recorder instance in my controller because 'sigs.k8s.io/controller-runtime/pkg/internal/controller' is internal package which I could not import in my controller

Even though the recorder set for controller through New method, I have to call manager.GetRecorder() again from my controller to get the recorder instance

lifecyclstale

Most helpful comment

this link is dead: https://book.kubebuilder.io/beyond_basics/creating_events.html

but here's an example of creating events:

first in your project directory, go to /controllers/yourKind_controller.go and add the recorder to your Reconciler struct:

type AssessmentReconciler struct {
    client.Client
    Log      logr.Logger
    Scheme   *runtime.Scheme
    recorder record.EventRecorder
}

in the SetupWithManager method, make sure to set the recorder

// SetupWithManager acts of the types we control/monitor
func (r *AssessmentReconciler) SetupWithManager(mgr ctrl.Manager) error {

        // add this line
    r.recorder = mgr.GetEventRecorderFor("Assessment")

    return ctrl.NewControllerManagedBy(mgr).
        For(&auditv1alpha1.Assessment{}).
        Complete(r)
}

then, to create events use:

    r.recorder.Event(&assessment, core.EventTypeNormal, "Updated", "Updated assessment")

where core and record are:

import (
core "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"

)

make sure to replace Assessment with YourKind

All 4 comments

This might be relevant - https://book.kubebuilder.io/beyond_basics/creating_events.html

Basically, you add a recorder field to your Reconcile struct and when you instantiate it, you call manager.GetRecorder("my-controller"). After that you can write events using the recorder

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

closing as per the above answer

this link is dead: https://book.kubebuilder.io/beyond_basics/creating_events.html

but here's an example of creating events:

first in your project directory, go to /controllers/yourKind_controller.go and add the recorder to your Reconciler struct:

type AssessmentReconciler struct {
    client.Client
    Log      logr.Logger
    Scheme   *runtime.Scheme
    recorder record.EventRecorder
}

in the SetupWithManager method, make sure to set the recorder

// SetupWithManager acts of the types we control/monitor
func (r *AssessmentReconciler) SetupWithManager(mgr ctrl.Manager) error {

        // add this line
    r.recorder = mgr.GetEventRecorderFor("Assessment")

    return ctrl.NewControllerManagedBy(mgr).
        For(&auditv1alpha1.Assessment{}).
        Complete(r)
}

then, to create events use:

    r.recorder.Event(&assessment, core.EventTypeNormal, "Updated", "Updated assessment")

where core and record are:

import (
core "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"

)

make sure to replace Assessment with YourKind

Was this page helpful?
0 / 5 - 0 ratings