@@ -471,30 +471,10 @@ func validateAfterStageTask(tasks []placementv1beta1.StageTask) error {
471471func (r * Reconciler ) recordOverrideSnapshots (ctx context.Context , placementKey types.NamespacedName , updateRun placementv1beta1.UpdateRunObj ) error {
472472 updateRunRef := klog .KObj (updateRun )
473473 updateRunSpec := updateRun .GetUpdateRunSpec ()
474- placementName := placementKey .Name
475474
476- snapshotIndex , err := strconv .Atoi (updateRunSpec .ResourceSnapshotIndex )
477- if err != nil || snapshotIndex < 0 {
478- err := controller .NewUserError (fmt .Errorf ("invalid resource snapshot index `%s` provided, expected an integer >= 0" , updateRunSpec .ResourceSnapshotIndex ))
479- klog .ErrorS (err , "Failed to parse the resource snapshot index" , "updateRun" , updateRunRef )
480- // no more retries here.
481- return fmt .Errorf ("%w: %s" , errInitializedFailed , err .Error ())
482- }
483-
484- resourceSnapshotList , err := controller .ListAllResourceSnapshotWithAnIndex (ctx , r .Client , updateRunSpec .ResourceSnapshotIndex , placementName , placementKey .Namespace )
475+ resourceSnapshotObjs , err := r .getResourceSnapshotObjs (ctx , placementKey , updateRun )
485476 if err != nil {
486- klog .ErrorS (err , "Failed to list the resourceSnapshots associated with the placement" ,
487- "placement" , placementKey , "resourceSnapshotIndex" , snapshotIndex , "updateRun" , updateRunRef )
488- // err can be retried.
489- return controller .NewAPIServerError (true , err )
490- }
491-
492- resourceSnapshotObjs := resourceSnapshotList .GetResourceSnapshotObjs ()
493- if len (resourceSnapshotObjs ) == 0 {
494- err := controller .NewUserError (fmt .Errorf ("no resourceSnapshots with index `%d` found for placement `%s`" , snapshotIndex , placementKey ))
495- klog .ErrorS (err , "No specified resourceSnapshots found" , "updateRun" , updateRunRef )
496- // no more retries here.
497- return fmt .Errorf ("%w: %s" , errInitializedFailed , err .Error ())
477+ return err
498478 }
499479
500480 // Look for the master resourceSnapshot.
@@ -509,12 +489,18 @@ func (r *Reconciler) recordOverrideSnapshots(ctx context.Context, placementKey t
509489
510490 // No masterResourceSnapshot found.
511491 if masterResourceSnapshot == nil {
512- err := controller .NewUnexpectedBehaviorError (fmt .Errorf ("no master resourceSnapshot found for placement `%s` with index `%d` " , placementKey , snapshotIndex ))
492+ err := controller .NewUnexpectedBehaviorError (fmt .Errorf ("no master resourceSnapshot found for placement %s " , placementKey ))
513493 klog .ErrorS (err , "Failed to find master resourceSnapshot" , "updateRun" , updateRunRef )
514494 // no more retries here.
515495 return fmt .Errorf ("%w: %s" , errInitializedFailed , err .Error ())
516496 }
517- klog .V (2 ).InfoS ("Found master resourceSnapshot" , "placement" , placementKey , "index" , snapshotIndex , "updateRun" , updateRunRef )
497+
498+ klog .V (2 ).InfoS ("Found master resourceSnapshot" , "placement" , placementKey , "masterResourceSnapshot" , masterResourceSnapshot .GetName (), "updateRun" , updateRunRef )
499+
500+ // Record the resource snapshot index used.
501+ updateRunStatus := updateRun .GetUpdateRunStatus ()
502+ updateRunStatus .ResourceSnapshotIndexUsed = masterResourceSnapshot .GetLabels ()[placementv1beta1 .ResourceIndexLabel ]
503+ updateRun .SetUpdateRunStatus (* updateRunStatus )
518504
519505 resourceSnapshotRef := klog .KObj (masterResourceSnapshot )
520506 // Fetch all the matching overrides.
@@ -526,7 +512,6 @@ func (r *Reconciler) recordOverrideSnapshots(ctx context.Context, placementKey t
526512 }
527513
528514 // Pick the overrides associated with each target cluster.
529- updateRunStatus := updateRun .GetUpdateRunStatus ()
530515 for _ , stageStatus := range updateRunStatus .StagesStatus {
531516 for i := range stageStatus .Clusters {
532517 clusterStatus := & stageStatus .Clusters [i ]
@@ -543,6 +528,58 @@ func (r *Reconciler) recordOverrideSnapshots(ctx context.Context, placementKey t
543528 return nil
544529}
545530
531+ // getResourceSnapshotObjs retrieves the list of resource snapshot objects from the specified ResourceSnapshotIndex.
532+ // If ResourceSnapshotIndex is unspecified, it returns the list of latest resource snapshots.
533+ func (r * Reconciler ) getResourceSnapshotObjs (ctx context.Context , placementKey types.NamespacedName , updateRun placementv1beta1.UpdateRunObj ) ([]placementv1beta1.ResourceSnapshotObj , error ) {
534+ updateRunRef := klog .KObj (updateRun )
535+ updateRunSpec := updateRun .GetUpdateRunSpec ()
536+ var resourceSnapshotObjs []placementv1beta1.ResourceSnapshotObj
537+ if updateRunSpec .ResourceSnapshotIndex != "" {
538+ snapshotIndex , err := strconv .Atoi (updateRunSpec .ResourceSnapshotIndex )
539+ if err != nil || snapshotIndex < 0 {
540+ err := controller .NewUserError (fmt .Errorf ("invalid resource snapshot index `%s` provided, expected an integer >= 0" , updateRunSpec .ResourceSnapshotIndex ))
541+ klog .ErrorS (err , "Failed to parse the resource snapshot index" , "updateRun" , updateRunRef )
542+ // no more retries here.
543+ return nil , fmt .Errorf ("%w: %s" , errInitializedFailed , err .Error ())
544+ }
545+
546+ resourceSnapshotList , err := controller .ListAllResourceSnapshotWithAnIndex (ctx , r .Client , updateRunSpec .ResourceSnapshotIndex , placementKey .Name , placementKey .Namespace )
547+ if err != nil {
548+ klog .ErrorS (err , "Failed to list the resourceSnapshots associated with the placement" ,
549+ "placement" , placementKey , "resourceSnapshotIndex" , snapshotIndex , "updateRun" , updateRunRef )
550+ // list err can be retried.
551+ return nil , controller .NewAPIServerError (true , err )
552+ }
553+
554+ resourceSnapshotObjs = resourceSnapshotList .GetResourceSnapshotObjs ()
555+ if len (resourceSnapshotObjs ) == 0 {
556+ err := controller .NewUserError (fmt .Errorf ("no resourceSnapshots with index `%d` found for placement `%s`" , snapshotIndex , placementKey ))
557+ klog .ErrorS (err , "No specified resourceSnapshots found" , "updateRun" , updateRunRef )
558+ // no more retries here.
559+ return resourceSnapshotObjs , fmt .Errorf ("%w: %s" , errInitializedFailed , err .Error ())
560+ }
561+ return resourceSnapshotObjs , nil
562+ }
563+
564+ klog .V (2 ).InfoS ("No resource snapshot index specified, fetching latest resource snapshots" , "placement" , placementKey , "updateRun" , updateRunRef )
565+ latestResourceSnapshots , err := controller .ListLatestResourceSnapshots (ctx , r .Client , placementKey )
566+ if err != nil {
567+ klog .ErrorS (err , "Failed to list the latest resourceSnapshots associated with the placement" ,
568+ "placement" , placementKey , "updateRun" , updateRunRef )
569+ // list err can be retried.
570+ return nil , controller .NewAPIServerError (true , err )
571+ }
572+
573+ resourceSnapshotObjs = latestResourceSnapshots .GetResourceSnapshotObjs ()
574+ if len (resourceSnapshotObjs ) == 0 {
575+ err := fmt .Errorf ("no latest resourceSnapshots found for placement `%s`. This might be a transient state, need retry" , placementKey )
576+ klog .ErrorS (err , "No latest resourceSnapshots found for placement. This might be transient, need retry" , "placement" , placementKey , "updateRun" , updateRunRef )
577+ // retryable error.
578+ return resourceSnapshotObjs , err
579+ }
580+ return resourceSnapshotObjs , nil
581+ }
582+
546583// recordInitializationSucceeded records the successful initialization condition in the UpdateRun status.
547584func (r * Reconciler ) recordInitializationSucceeded (ctx context.Context , updateRun placementv1beta1.UpdateRunObj ) error {
548585 updateRunStatus := updateRun .GetUpdateRunStatus ()
0 commit comments