From c91ad314d6083c4b0718d6c5fbc5f72a5ec5d4ca Mon Sep 17 00:00:00 2001 From: Ilyes Ben Dlala Date: Tue, 18 Feb 2025 12:19:06 +0100 Subject: [PATCH] Handle conflict errors during ScheduledScan status updates with retry logic Signed-off-by: Ilyes Ben Dlala --- .../execution/scheduledscan_controller.go | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/operator/controllers/execution/scheduledscan_controller.go b/operator/controllers/execution/scheduledscan_controller.go index 51fcc014aa..8206d97a6c 100644 --- a/operator/controllers/execution/scheduledscan_controller.go +++ b/operator/controllers/execution/scheduledscan_controller.go @@ -14,6 +14,7 @@ import ( "github.com/go-logr/logr" "github.com/robfig/cron" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" @@ -76,8 +77,17 @@ func (r *ScheduledScanReconciler) Reconcile(ctx context.Context, req ctrl.Reques log.V(4).Info("Updating ScheduledScans Findings as they appear to have changed") scheduledScan.Status.Findings = *lastFindings.DeepCopy() if err := r.Status().Update(ctx, &scheduledScan); err != nil { - log.Error(err, "unable to update ScheduledScan status") - return ctrl.Result{}, err + if apierrors.IsConflict(err) { + r.Log.V(4).Info( + "Conflict while updating ScheduledScan status, retrying", + "scheduledScan", scheduledScan.Name, + "namespace", scheduledScan.Namespace, + ) + return ctrl.Result{RequeueAfter: 10 * time.Second}, nil + } else { + log.Error(err, "unable to update ScheduledScan status") + return ctrl.Result{}, err + } } } } @@ -178,8 +188,17 @@ func (r *ScheduledScanReconciler) Reconcile(ctx context.Context, req ctrl.Reques var now metav1.Time = metav1.Now() scheduledScan.Status.LastScheduleTime = &now if err := r.Status().Update(ctx, &scheduledScan); err != nil { - log.Error(err, "Unable to update ScheduledScan status") - return ctrl.Result{}, err + if apierrors.IsConflict(err) { + r.Log.V(4).Info( + "Conflict while updating ScheduledScan status, retrying", + "scheduledScan", scheduledScan.Name, + "namespace", scheduledScan.Namespace, + ) + return ctrl.Result{RequeueAfter: 10 * time.Second}, nil + } else { + log.Error(err, "Unable to update ScheduledScan status") + return ctrl.Result{}, err + } } // Recalculate next schedule