Skip to content

Commit 1b86e43

Browse files
committed
address comments
Signed-off-by: Britania Rodriguez Reyes <britaniar@microsoft.com>
1 parent 734d82c commit 1b86e43

File tree

6 files changed

+104
-17
lines changed

6 files changed

+104
-17
lines changed

apis/placement/v1beta1/stageupdate_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ type UpdateRunStatus struct {
374374
// +kubebuilder:validation:Optional
375375
PolicyObservedClusterCount int `json:"policyObservedClusterCount,omitempty"`
376376

377-
// ResourceSnapshotName records the name of the resource snapshot that the update run is based on.
377+
// ResourceSnapshotName records the name of the master resource snapshot that the update run is based on.
378378
// +kubebuilder:validation:Optional
379379
ResourceSnapshotName string `json:"resourceSnapshotName,omitempty"`
380380

apis/placement/v1beta1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/placement.kubernetes-fleet.io_clusterstagedupdateruns.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,7 @@ spec:
18951895
to the current policy.
18961896
type: string
18971897
resourceSnapshotName:
1898-
description: ResourceSnapshotName records the name of the resource
1898+
description: ResourceSnapshotName records the name of the master resource
18991899
snapshot that the update run is based on.
19001900
type: string
19011901
stagedUpdateStrategySnapshot:

config/crd/bases/placement.kubernetes-fleet.io_stagedupdateruns.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ spec:
815815
to the current policy.
816816
type: string
817817
resourceSnapshotName:
818-
description: ResourceSnapshotName records the name of the resource
818+
description: ResourceSnapshotName records the name of the master resource
819819
snapshot that the update run is based on.
820820
type: string
821821
stagedUpdateStrategySnapshot:

pkg/controllers/updaterun/initialization.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,13 @@ func (r *Reconciler) recordOverrideSnapshots(ctx context.Context, placementKey t
478478
return err
479479
}
480480

481-
if len(resourceSnapshotObjs) == 0 {
482-
err := controller.NewUserError(fmt.Errorf("no resourceSnapshots found for placement `%s`", placementKey))
483-
klog.ErrorS(err, "No resourceSnapshots found", "updateRun", updateRunRef)
484-
// no more retries here.
485-
return fmt.Errorf("%w: %s", errInitializedFailed, err.Error())
486-
}
487-
488481
// Look for the master resourceSnapshot.
489482
var masterResourceSnapshot placementv1beta1.ResourceSnapshotObj
490483
for _, resourceSnapshot := range resourceSnapshotObjs {
491484
// only master has this annotation.
492485
if len(resourceSnapshot.GetAnnotations()[placementv1beta1.ResourceGroupHashAnnotation]) != 0 {
493486
masterResourceSnapshot = resourceSnapshot
487+
494488
break
495489
}
496490
}
@@ -502,10 +496,12 @@ func (r *Reconciler) recordOverrideSnapshots(ctx context.Context, placementKey t
502496
return fmt.Errorf("%w: %s", errInitializedFailed, err.Error())
503497
}
504498

505-
klog.InfoS("Found master resourceSnapshot", "placement", placementKey, "index", updateRun.GetUpdateRunSpec().ResourceSnapshotIndex, "updateRun", updateRunRef)
499+
klog.InfoS("Found master resourceSnapshot", "placement", placementKey, "masterResourceSnapshot", masterResourceSnapshot.GetName(), "updateRun", updateRunRef)
506500

507501
// Update the resource snapshot name in the UpdateRun status.
508-
updateRun.GetUpdateRunStatus().ResourceSnapshotName = masterResourceSnapshot.GetName()
502+
updateRunStatus := updateRun.GetUpdateRunStatus()
503+
updateRunStatus.ResourceSnapshotName = masterResourceSnapshot.GetName()
504+
updateRun.SetUpdateRunStatus(*updateRunStatus)
509505
if updateErr := r.Client.Status().Update(ctx, updateRun); updateErr != nil {
510506
klog.ErrorS(updateErr, "Failed to update the UpdateRun status with resource snapshot name", "updateRun", klog.KObj(updateRun), "resourceSnapshot", klog.KObj(masterResourceSnapshot))
511507
// updateErr can be retried.
@@ -522,7 +518,6 @@ func (r *Reconciler) recordOverrideSnapshots(ctx context.Context, placementKey t
522518
}
523519

524520
// Pick the overrides associated with each target cluster.
525-
updateRunStatus := updateRun.GetUpdateRunStatus()
526521
for _, stageStatus := range updateRunStatus.StagesStatus {
527522
for i := range stageStatus.Clusters {
528523
clusterStatus := &stageStatus.Clusters[i]
@@ -539,8 +534,10 @@ func (r *Reconciler) recordOverrideSnapshots(ctx context.Context, placementKey t
539534
return nil
540535
}
541536

542-
// getResourceSnapshotObjs retrieves the resource snapshot objects either by index or latest snapshots.
537+
// getResourceSnapshotObjs retrieves the list of resource snapshot objects from the specified ResourceSnapshotIndex.
538+
// If ResourceSnapshotIndex is unspecified, it returns the list of latest resource snapshots.
543539
func (r *Reconciler) getResourceSnapshotObjs(ctx context.Context, updateRunSpec *placementv1beta1.UpdateRunSpec, placementName string, placementKey types.NamespacedName, updateRunRef klog.ObjectRef) ([]placementv1beta1.ResourceSnapshotObj, error) {
540+
var resourceSnapshotObjs []placementv1beta1.ResourceSnapshotObj
544541
if updateRunSpec.ResourceSnapshotIndex != "" {
545542
snapshotIndex, err := strconv.Atoi(updateRunSpec.ResourceSnapshotIndex)
546543
if err != nil || snapshotIndex < 0 {
@@ -555,7 +552,15 @@ func (r *Reconciler) getResourceSnapshotObjs(ctx context.Context, updateRunSpec
555552
"placement", placementKey, "resourceSnapshotIndex", snapshotIndex, "updateRun", updateRunRef)
556553
return nil, controller.NewAPIServerError(true, err)
557554
}
558-
return resourceSnapshotList.GetResourceSnapshotObjs(), nil
555+
556+
resourceSnapshotObjs = resourceSnapshotList.GetResourceSnapshotObjs()
557+
if len(resourceSnapshotObjs) == 0 {
558+
err := controller.NewUserError(fmt.Errorf("no resourceSnapshots with index `%d` found for placement `%s`", snapshotIndex, placementKey))
559+
klog.ErrorS(err, "No specified resourceSnapshots found", "updateRun", updateRunRef)
560+
// no more retries here.
561+
return resourceSnapshotObjs, fmt.Errorf("%w: %s", errInitializedFailed, err.Error())
562+
}
563+
return resourceSnapshotObjs, nil
559564
}
560565

561566
latestResourceSnapshots, err := controller.ListLatestResourceSnapshots(ctx, r.Client, placementKey)
@@ -564,7 +569,15 @@ func (r *Reconciler) getResourceSnapshotObjs(ctx context.Context, updateRunSpec
564569
"placement", placementKey, "updateRun", updateRunRef)
565570
return nil, controller.NewAPIServerError(true, err)
566571
}
567-
return latestResourceSnapshots.GetResourceSnapshotObjs(), nil
572+
573+
resourceSnapshotObjs = latestResourceSnapshots.GetResourceSnapshotObjs()
574+
if len(resourceSnapshotObjs) == 0 {
575+
err := controller.NewUserError(fmt.Errorf("no resourceSnapshots found for placement `%s`", placementKey))
576+
klog.ErrorS(err, "No resourceSnapshots found", "updateRun", updateRunRef)
577+
// no more retries here.
578+
return resourceSnapshotObjs, fmt.Errorf("%w: %s", errInitializedFailed, err.Error())
579+
}
580+
return resourceSnapshotObjs, nil
568581
}
569582

570583
// recordInitializationSucceeded records the successful initialization condition in the UpdateRun status.

pkg/controllers/updaterun/initialization_integration_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,80 @@ var _ = Describe("Updaterun initialization tests", func() {
877877
validateUpdateRunMetricsEmitted(generateProgressingMetric(updateRun))
878878
})
879879
})
880+
881+
Context("Test multiple resource snapshots", func() {
882+
var resourceSnapshot2, resourceSnapshot3 *placementv1beta1.ClusterResourceSnapshot
883+
BeforeEach(func() {
884+
By("Creating a new clusterResourcePlacement")
885+
Expect(k8sClient.Create(ctx, crp)).To(Succeed())
886+
887+
By("Creating scheduling policy snapshot")
888+
Expect(k8sClient.Create(ctx, policySnapshot)).To(Succeed())
889+
890+
By("Setting the latest policy snapshot condition as fully scheduled")
891+
meta.SetStatusCondition(&policySnapshot.Status.Conditions, metav1.Condition{
892+
Type: string(placementv1beta1.PolicySnapshotScheduled),
893+
Status: metav1.ConditionTrue,
894+
ObservedGeneration: policySnapshot.Generation,
895+
Reason: "scheduled",
896+
})
897+
Expect(k8sClient.Status().Update(ctx, policySnapshot)).Should(Succeed(), "failed to update the policy snapshot condition")
898+
899+
By("Creating a new resource snapshot")
900+
resourceSnapshot.Labels[placementv1beta1.IsLatestSnapshotLabel] = "false"
901+
Expect(k8sClient.Create(ctx, resourceSnapshot)).To(Succeed())
902+
903+
By("Creating a another new resource snapshot")
904+
resourceSnapshot2 = generateTestClusterResourceSnapshot()
905+
resourceSnapshot2.Name = testCRPName + "-1-snapshot"
906+
resourceSnapshot2.Labels[placementv1beta1.IsLatestSnapshotLabel] = "false"
907+
resourceSnapshot2.Labels[placementv1beta1.ResourceIndexLabel] = "1"
908+
Expect(k8sClient.Create(ctx, resourceSnapshot2)).To(Succeed())
909+
910+
By("Creating a latest master resource snapshot")
911+
resourceSnapshot3 = generateTestClusterResourceSnapshot()
912+
resourceSnapshot3.Name = testCRPName + "-2-snapshot"
913+
resourceSnapshot3.Labels[placementv1beta1.ResourceIndexLabel] = "2"
914+
Expect(k8sClient.Create(ctx, resourceSnapshot3)).To(Succeed())
915+
916+
By("Creating the member clusters")
917+
for _, cluster := range targetClusters {
918+
Expect(k8sClient.Create(ctx, cluster)).To(Succeed())
919+
}
920+
for _, cluster := range unscheduledClusters {
921+
Expect(k8sClient.Create(ctx, cluster)).To(Succeed())
922+
}
923+
924+
By("Creating a bunch of ClusterResourceBindings")
925+
for _, binding := range resourceBindings {
926+
Expect(k8sClient.Create(ctx, binding)).To(Succeed())
927+
}
928+
929+
By("Creating a clusterStagedUpdateStrategy")
930+
Expect(k8sClient.Create(ctx, updateStrategy)).To(Succeed())
931+
})
932+
933+
It("Should pick latest master resource snapshot", func() {
934+
By("Creating a new cluster resource override")
935+
Expect(k8sClient.Create(ctx, clusterResourceOverride)).To(Succeed())
936+
937+
By("Creating a new clusterStagedUpdateRun")
938+
updateRun.Spec.ResourceSnapshotIndex = ""
939+
Expect(k8sClient.Create(ctx, updateRun)).To(Succeed())
940+
941+
By("Validating the clusterStagedUpdateRun stats")
942+
initialized := generateSucceededInitializationStatus(crp, updateRun, policySnapshot, updateStrategy, clusterResourceOverride)
943+
initialized.ResourceSnapshotName = resourceSnapshot3.Name
944+
want := generateExecutionStartedStatus(updateRun, initialized)
945+
validateClusterStagedUpdateRunStatus(ctx, updateRun, want, "")
946+
947+
By("Validating the clusterStagedUpdateRun initialized consistently")
948+
validateClusterStagedUpdateRunStatusConsistently(ctx, updateRun, want, "")
949+
950+
By("Checking update run status metrics are emitted")
951+
validateUpdateRunMetricsEmitted(generateProgressingMetric(updateRun))
952+
})
953+
})
880954
})
881955

882956
func validateFailedInitCondition(ctx context.Context, updateRun *placementv1beta1.ClusterStagedUpdateRun, message string) {

0 commit comments

Comments
 (0)