From aab62685e3fdc3ee93e68192e0a8c61aff708a14 Mon Sep 17 00:00:00 2001 From: Kensei Nakada Date: Wed, 4 Oct 2023 10:32:14 +0900 Subject: [PATCH] fix: emits ReconcileError events correctly --- controllers/tortoise_controller.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/controllers/tortoise_controller.go b/controllers/tortoise_controller.go index 452f55e6..d114c80b 100644 --- a/controllers/tortoise_controller.go +++ b/controllers/tortoise_controller.go @@ -69,12 +69,6 @@ type TortoiseReconciler struct { //+kubebuilder:rbac:groups=autoscaling,resources=horizontalpodautoscalers,verbs=get;list;watch;create;update;patch;delete func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) { - defer func() { - if reterr != nil { - r.EventRecorder.Event(&autoscalingv1beta1.Tortoise{}, "Warning", "ReconcileError", reterr.Error()) - } - }() - logger := log.FromContext(ctx) now := time.Now() @@ -89,6 +83,11 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ logger.Error(err, "failed to get tortoise", "tortoise", req.NamespacedName) return ctrl.Result{}, err } + defer func() { + if reterr != nil { + r.EventRecorder.Event(tortoise, "Warning", "ReconcileError", reterr.Error()) + } + }() if !tortoise.ObjectMeta.DeletionTimestamp.IsZero() { // Tortoise is deleted by user and waiting for finalizer.