@@ -26,9 +26,7 @@ import (
2626 apierrors "k8s.io/apimachinery/pkg/api/errors"
2727 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828 "k8s.io/apimachinery/pkg/labels"
29- "k8s.io/apimachinery/pkg/runtime"
3029 kerrors "k8s.io/apimachinery/pkg/util/errors"
31- kuberecorder "k8s.io/client-go/tools/record"
3230 "k8s.io/client-go/util/workqueue"
3331 ctrl "sigs.k8s.io/controller-runtime"
3432 "sigs.k8s.io/controller-runtime/pkg/builder"
@@ -41,14 +39,15 @@ import (
4139
4240 reflectorv1 "github.com/fluxcd/image-reflector-controller/api/v1"
4341 aclapi "github.com/fluxcd/pkg/apis/acl"
44- eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1 "
42+ eventv1 "github.com/fluxcd/pkg/apis/event/v1 "
4543 "github.com/fluxcd/pkg/apis/meta"
4644 "github.com/fluxcd/pkg/auth"
4745 "github.com/fluxcd/pkg/cache"
4846 "github.com/fluxcd/pkg/git"
4947 "github.com/fluxcd/pkg/runtime/acl"
5048 "github.com/fluxcd/pkg/runtime/conditions"
5149 helper "github.com/fluxcd/pkg/runtime/controller"
50+ "github.com/fluxcd/pkg/runtime/events"
5251 "github.com/fluxcd/pkg/runtime/patch"
5352 "github.com/fluxcd/pkg/runtime/predicates"
5453 runtimereconcile "github.com/fluxcd/pkg/runtime/reconcile"
@@ -102,7 +101,7 @@ func getPatchOptions(ownedConditions []string, controllerName string) []patch.Op
102101// ImageUpdateAutomationReconciler reconciles a ImageUpdateAutomation object
103102type ImageUpdateAutomationReconciler struct {
104103 client.Client
105- kuberecorder .EventRecorder
104+ events .EventRecorder
106105 helper.Metrics
107106
108107 ControllerName string
@@ -608,6 +607,7 @@ func observedPoliciesChanged(previous, current imagev1.ObservedPolicies) bool {
608607// case of any failure, the failure message is read from the Ready condition and
609608// included in the event.
610609func (r * ImageUpdateAutomationReconciler ) notify (ctx context.Context , oldObj , newObj conditions.Setter , result * source.PushResult , syncNeeded bool ) {
610+ related := sourceRefObject (newObj )
611611 // Use the Ready message as the notification message by default.
612612 ready := conditions .Get (newObj , meta .ReadyCondition )
613613 msg := ready .Message
@@ -619,20 +619,20 @@ func (r *ImageUpdateAutomationReconciler) notify(ctx context.Context, oldObj, ne
619619
620620 // Was ready before and is ready now, with new push result,
621621 if conditions .IsReady (oldObj ) && conditions .IsReady (newObj ) && result != nil {
622- eventLogf ( ctx , r . EventRecorder , newObj , corev1 .EventTypeNormal , ready .Reason , "%s" , msg )
622+ r . Eventf ( newObj , related , corev1 .EventTypeNormal , ready .Reason , "" , "%s" , msg )
623623 return
624624 }
625625
626626 // Emit events when reconciliation fails or recovers from failure.
627627
628628 // Became ready from not ready.
629629 if ! conditions .IsReady (oldObj ) && conditions .IsReady (newObj ) {
630- eventLogf ( ctx , r . EventRecorder , newObj , corev1 .EventTypeNormal , ready .Reason , "%s" , msg )
630+ r . Eventf ( newObj , related , corev1 .EventTypeNormal , ready .Reason , "" , "%s" , msg )
631631 return
632632 }
633633 // Not ready, failed. Use the failure message from ready condition.
634634 if ! conditions .IsReady (newObj ) {
635- eventLogf ( ctx , r . EventRecorder , newObj , corev1 .EventTypeWarning , ready .Reason , "%s" , ready .Message )
635+ r . Eventf ( newObj , related , corev1 .EventTypeWarning , ready .Reason , "" , "%s" , ready .Message )
636636 return
637637 }
638638
@@ -642,21 +642,30 @@ func (r *ImageUpdateAutomationReconciler) notify(ctx context.Context, oldObj, ne
642642 // Full reconciliation skipped.
643643 msg = "no change since last reconciliation"
644644 }
645- eventLogf ( ctx , r . EventRecorder , newObj , eventv1 .EventTypeTrace , meta .SucceededReason , "%s" , msg )
645+ r . Eventf ( newObj , related , eventv1 .EventTypeTrace , meta .SucceededReason , "" , "%s" , msg )
646646}
647647
648- // eventLogf records events, and logs at the same time.
649- //
650- // This log is different from the debug log in the EventRecorder, in the sense
651- // that this is a simple log. While the debug log contains complete details
652- // about the event.
653- func eventLogf (ctx context.Context , r kuberecorder.EventRecorder , obj runtime.Object , eventType string , reason string , messageFmt string , args ... interface {}) {
654- msg := fmt .Sprintf (messageFmt , args ... )
655- // Log and emit event.
656- if eventType == corev1 .EventTypeWarning {
657- ctrl .LoggerFrom (ctx ).Error (errors .New (reason ), msg )
658- } else {
659- ctrl .LoggerFrom (ctx ).Info (msg )
648+ // sourceRefObject returns a minimal GitRepository object for use as the
649+ // related object in events. It extracts the source reference from the
650+ // ImageUpdateAutomation spec.
651+ func sourceRefObject (obj conditions.Setter ) * sourcev1.GitRepository {
652+ auto , ok := obj .(* imagev1.ImageUpdateAutomation )
653+ if ! ok {
654+ return nil
655+ }
656+ ref := auto .Spec .SourceRef
657+ ns := ref .Namespace
658+ if ns == "" {
659+ ns = auto .Namespace
660+ }
661+ return & sourcev1.GitRepository {
662+ TypeMeta : metav1.TypeMeta {
663+ Kind : sourcev1 .GitRepositoryKind ,
664+ APIVersion : sourcev1 .GroupVersion .String (),
665+ },
666+ ObjectMeta : metav1.ObjectMeta {
667+ Name : ref .Name ,
668+ Namespace : ns ,
669+ },
660670 }
661- r .Eventf (obj , eventType , reason , msg )
662671}
0 commit comments