@@ -469,42 +469,18 @@ func validateAfterStageTask(tasks []placementv1beta1.StageTask) error {
469469
470470// recordOverrideSnapshots finds all the override snapshots that are associated with each cluster and record them in the UpdateRun status.
471471func (r * Reconciler ) recordOverrideSnapshots (ctx context.Context , placementKey types.NamespacedName , updateRun placementv1beta1.UpdateRunObj ) error {
472- var resourceSnapshotObjs []placementv1beta1.ResourceSnapshotObj
473- var snapshotIndex int
474472 updateRunRef := klog .KObj (updateRun )
475473 updateRunSpec := updateRun .GetUpdateRunSpec ()
476474 placementName := placementKey .Name
477- if updateRunSpec .ResourceSnapshotIndex != "" {
478- snapshotIndex , err := strconv .Atoi (updateRunSpec .ResourceSnapshotIndex )
479- if err != nil || snapshotIndex < 0 {
480- err := controller .NewUserError (fmt .Errorf ("invalid resource snapshot index `%s` provided, expected an integer >= 0" , updateRunSpec .ResourceSnapshotIndex ))
481- klog .ErrorS (err , "Failed to parse the resource snapshot index" , "updateRun" , updateRunRef )
482- // no more retries here.
483- return fmt .Errorf ("%w: %s" , errInitializedFailed , err .Error ())
484- }
485475
486- resourceSnapshotList , err := controller .ListAllResourceSnapshotWithAnIndex (ctx , r .Client , updateRunSpec .ResourceSnapshotIndex , placementName , placementKey .Namespace )
487- if err != nil {
488- klog .ErrorS (err , "Failed to list the resourceSnapshots associated with the placement" ,
489- "placement" , placementKey , "resourceSnapshotIndex" , snapshotIndex , "updateRun" , updateRunRef )
490- // err can be retried.
491- return controller .NewAPIServerError (true , err )
492- }
493- resourceSnapshotObjs = resourceSnapshotList .GetResourceSnapshotObjs ()
494- } else {
495- latestResourceSnapshot , err := controller .ListLatestResourceSnapshots (ctx , r .Client , placementKey )
496- if err != nil {
497- klog .ErrorS (err , "Failed to list the latest resourceSnapshots associated with the placement" ,
498- "placement" , placementKey , "updateRun" , updateRunRef )
499- // err can be retried.
500- return controller .NewAPIServerError (true , err )
501- }
502- resourceSnapshotObjs = latestResourceSnapshot .GetResourceSnapshotObjs ()
476+ resourceSnapshotObjs , err := r .getResourceSnapshotObjs (ctx , updateRunSpec , placementName , placementKey , updateRunRef )
477+ if err != nil {
478+ return err
503479 }
504480
505481 if len (resourceSnapshotObjs ) == 0 {
506- err := controller .NewUserError (fmt .Errorf ("no resourceSnapshots with index `%d` found for placement `%s`" , snapshotIndex , placementKey ))
507- klog .ErrorS (err , "No specified resourceSnapshots found" , "updateRun" , updateRunRef )
482+ err := controller .NewUserError (fmt .Errorf ("no resourceSnapshots found for placement `%s`" , placementKey ))
483+ klog .ErrorS (err , "No resourceSnapshots found" , "updateRun" , updateRunRef )
508484 // no more retries here.
509485 return fmt .Errorf ("%w: %s" , errInitializedFailed , err .Error ())
510486 }
@@ -518,15 +494,23 @@ func (r *Reconciler) recordOverrideSnapshots(ctx context.Context, placementKey t
518494 break
519495 }
520496 }
521-
522497 // No masterResourceSnapshot found.
523498 if masterResourceSnapshot == nil {
524- err := controller .NewUnexpectedBehaviorError (fmt .Errorf ("no master resourceSnapshot found for placement `%s` with index `%d` " , placementKey , snapshotIndex ))
499+ err := controller .NewUnexpectedBehaviorError (fmt .Errorf ("no master resourceSnapshot found for placement %s " , placementKey ))
525500 klog .ErrorS (err , "Failed to find master resourceSnapshot" , "updateRun" , updateRunRef )
526501 // no more retries here.
527502 return fmt .Errorf ("%w: %s" , errInitializedFailed , err .Error ())
528503 }
529- klog .V (2 ).InfoS ("Found master resourceSnapshot" , "placement" , placementKey , "index" , snapshotIndex , "updateRun" , updateRunRef )
504+
505+ klog .InfoS ("Found master resourceSnapshot" , "placement" , placementKey , "index" , updateRun .GetUpdateRunSpec ().ResourceSnapshotIndex , "updateRun" , updateRunRef )
506+
507+ // Update the resource snapshot name in the UpdateRun status.
508+ updateRun .GetUpdateRunStatus ().ResourceSnapshotName = masterResourceSnapshot .GetName ()
509+ if updateErr := r .Client .Status ().Update (ctx , updateRun ); updateErr != nil {
510+ klog .ErrorS (updateErr , "Failed to update the UpdateRun status with resource snapshot name" , "updateRun" , klog .KObj (updateRun ), "resourceSnapshot" , klog .KObj (masterResourceSnapshot ))
511+ // updateErr can be retried.
512+ return controller .NewUpdateIgnoreConflictError (updateErr )
513+ }
530514
531515 resourceSnapshotRef := klog .KObj (masterResourceSnapshot )
532516 // Fetch all the matching overrides.
@@ -555,6 +539,34 @@ func (r *Reconciler) recordOverrideSnapshots(ctx context.Context, placementKey t
555539 return nil
556540}
557541
542+ // getResourceSnapshotObjs retrieves the resource snapshot objects either by index or latest snapshots.
543+ func (r * Reconciler ) getResourceSnapshotObjs (ctx context.Context , updateRunSpec * placementv1beta1.UpdateRunSpec , placementName string , placementKey types.NamespacedName , updateRunRef klog.ObjectRef ) ([]placementv1beta1.ResourceSnapshotObj , error ) {
544+ if updateRunSpec .ResourceSnapshotIndex != "" {
545+ snapshotIndex , err := strconv .Atoi (updateRunSpec .ResourceSnapshotIndex )
546+ if err != nil || snapshotIndex < 0 {
547+ err := controller .NewUserError (fmt .Errorf ("invalid resource snapshot index `%s` provided, expected an integer >= 0" , updateRunSpec .ResourceSnapshotIndex ))
548+ klog .ErrorS (err , "Failed to parse the resource snapshot index" , "updateRun" , updateRunRef )
549+ return nil , fmt .Errorf ("%w: %s" , errInitializedFailed , err .Error ())
550+ }
551+
552+ resourceSnapshotList , err := controller .ListAllResourceSnapshotWithAnIndex (ctx , r .Client , updateRunSpec .ResourceSnapshotIndex , placementName , placementKey .Namespace )
553+ if err != nil {
554+ klog .ErrorS (err , "Failed to list the resourceSnapshots associated with the placement" ,
555+ "placement" , placementKey , "resourceSnapshotIndex" , snapshotIndex , "updateRun" , updateRunRef )
556+ return nil , controller .NewAPIServerError (true , err )
557+ }
558+ return resourceSnapshotList .GetResourceSnapshotObjs (), nil
559+ }
560+
561+ latestResourceSnapshots , err := controller .ListLatestResourceSnapshots (ctx , r .Client , placementKey )
562+ if err != nil {
563+ klog .ErrorS (err , "Failed to list the latest resourceSnapshots associated with the placement" ,
564+ "placement" , placementKey , "updateRun" , updateRunRef )
565+ return nil , controller .NewAPIServerError (true , err )
566+ }
567+ return latestResourceSnapshots .GetResourceSnapshotObjs (), nil
568+ }
569+
558570// recordInitializationSucceeded records the successful initialization condition in the UpdateRun status.
559571func (r * Reconciler ) recordInitializationSucceeded (ctx context.Context , updateRun placementv1beta1.UpdateRunObj ) error {
560572 updateRunStatus := updateRun .GetUpdateRunStatus ()
0 commit comments