@@ -34,6 +34,7 @@ import (
3434 "k8s.io/apimachinery/pkg/labels"
3535 "k8s.io/apimachinery/pkg/runtime"
3636 "k8s.io/apimachinery/pkg/types"
37+ "k8s.io/client-go/util/retry"
3738 ctrl "sigs.k8s.io/controller-runtime"
3839 "sigs.k8s.io/controller-runtime/pkg/client"
3940 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -194,7 +195,7 @@ func (r *NonAdminBackupReconciler) setStatusAndConditionForDeletionAndCallDelete
194195 },
195196 )
196197 if updatedPhase || updatedCondition {
197- if err := r .Status (). Update ( ctx , nab ); err != nil {
198+ if err := r .updateStatusWithRetry ( ctx , logger , nab ); err != nil {
198199 logger .Error (err , statusUpdateError )
199200 return false , err
200201 }
@@ -238,7 +239,7 @@ func (r *NonAdminBackupReconciler) setStatusForDirectKubernetesAPIDeletion(ctx c
238239 },
239240 )
240241 if updatedPhase || updatedCondition {
241- if err := r .Status (). Update ( ctx , nab ); err != nil {
242+ if err := r .updateStatusWithRetry ( ctx , logger , nab ); err != nil {
242243 logger .Error (err , statusUpdateError )
243244 return false , err
244245 }
@@ -490,7 +491,7 @@ func (r *NonAdminBackupReconciler) initNabCreate(ctx context.Context, logger log
490491
491492 // Set phase to New
492493 if updated := updateNonAdminPhase (& nab .Status .Phase , nacv1alpha1 .NonAdminPhaseNew ); updated {
493- if err := r .Status (). Update ( ctx , nab ); err != nil {
494+ if err := r .updateStatusWithRetry ( ctx , logger , nab ); err != nil {
494495 logger .Error (err , statusUpdateError )
495496 return false , err
496497 }
@@ -548,7 +549,7 @@ func (r *NonAdminBackupReconciler) validateSpec(ctx context.Context, logger logr
548549 },
549550 )
550551 if updated {
551- if err := r .Status (). Update ( ctx , nab ); err != nil {
552+ if err := r .updateStatusWithRetry ( ctx , logger , nab ); err != nil {
552553 logger .Error (err , statusUpdateError )
553554 return false , err
554555 }
@@ -590,7 +591,7 @@ func (r *NonAdminBackupReconciler) setBackupUUIDInStatus(ctx context.Context, lo
590591 Namespace : r .OADPNamespace ,
591592 Name : veleroBackupNACUUID ,
592593 }
593- if err := r .Status (). Update ( ctx , nab ); err != nil {
594+ if err := r .updateStatusWithRetry ( ctx , logger , nab ); err != nil {
594595 logger .Error (err , statusUpdateError )
595596 return false , err
596597 }
@@ -663,7 +664,7 @@ func (r *NonAdminBackupReconciler) createVeleroBackupAndSyncWithNonAdminBackup(c
663664 },
664665 )
665666 if updatedPhase || updatedCondition {
666- if updateErr := r .Status (). Update ( ctx , nab ); updateErr != nil {
667+ if updateErr := r .updateStatusWithRetry ( ctx , logger , nab ); updateErr != nil {
667668 logger .Error (updateErr , nonAdminRestoreStatusUpdateFailureMessage )
668669 return false , updateErr
669670 }
@@ -798,7 +799,7 @@ func (r *NonAdminBackupReconciler) createVeleroBackupAndSyncWithNonAdminBackup(c
798799 updatedDataUploadStatus := updateNonAdminBackupDataUploadStatus (& nab .Status , dataUploads )
799800
800801 if updated || updatedPhase || updatedCondition || updatedQueueInfo || updatedPodVolumeBackupStatus || updatedDataUploadStatus {
801- if err := r .Status (). Update ( ctx , nab ); err != nil {
802+ if err := r .updateStatusWithRetry ( ctx , logger , nab ); err != nil {
802803 logger .Error (err , statusUpdateError )
803804 return false , err
804805 }
@@ -810,6 +811,29 @@ func (r *NonAdminBackupReconciler) createVeleroBackupAndSyncWithNonAdminBackup(c
810811 return false , nil
811812}
812813
814+ // updateStatusWithRetry updates the NonAdminBackup status with retry logic to handle resource version conflicts
815+ func (r * NonAdminBackupReconciler ) updateStatusWithRetry (ctx context.Context , logger logr.Logger , nab * nacv1alpha1.NonAdminBackup ) error {
816+ return retry .RetryOnConflict (retry .DefaultBackoff , func () error {
817+ // Always fetch the latest version before updating status
818+ current := & nacv1alpha1.NonAdminBackup {}
819+ if err := r .Get (ctx , client .ObjectKeyFromObject (nab ), current ); err != nil {
820+ return err
821+ }
822+
823+ // Copy the status we want to update to the latest version
824+ current .Status = nab .Status
825+
826+ // Attempt status update on fresh resource version
827+ if err := r .Status ().Update (ctx , current ); err != nil {
828+ logger .V (1 ).Info ("Status update conflict, retrying..." , "error" , err .Error ())
829+ return err
830+ }
831+
832+ logger .V (1 ).Info ("Status update successful" )
833+ return nil
834+ })
835+ }
836+
813837// SetupWithManager sets up the controller with the Manager.
814838func (r * NonAdminBackupReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
815839 return ctrl .NewControllerManagedBy (mgr ).
0 commit comments