Skip to content

Commit 558607b

Browse files
committed
update stagedUpdateRun to use latest resource snapshot if resourceIndex is unset
Signed-off-by: Britania Rodriguez Reyes <britaniar@microsoft.com>
1 parent 2fd4460 commit 558607b

7 files changed

+56
-21
lines changed

apis/placement/v1beta1/stageupdate_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ type UpdateRunSpec struct {
159159

160160
// The resource snapshot index of the selected resources to be updated across clusters.
161161
// The index represents a group of resource snapshots that includes all the resources a ResourcePlacement selected.
162-
// +kubebuilder:validation:Required
162+
// +kubebuilder:validation:optional
163163
ResourceSnapshotIndex string `json:"resourceSnapshotIndex"`
164164

165165
// The name of the update strategy that specifies the stages and the sequence

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ spec:
19981998
description: |-
19991999
MaxConcurrency specifies the maximum number of clusters that can be updated concurrently within this stage.
20002000
Value can be an absolute number (ex: 5) or a percentage of the total clusters in the stage (ex: 50%).
2001-
Absolute number is calculated from percentage by rounding up.
2001+
Fractional results are rounded down. A minimum of 1 update is enforced.
20022002
If not specified, all clusters in the stage are updated sequentially (effectively maxConcurrency = 1).
20032003
Defaults to 1.
20042004
pattern: ^((100|[0-9]{1,2})%|[0-9]+)$

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ spec:
312312
description: |-
313313
MaxConcurrency specifies the maximum number of clusters that can be updated concurrently within this stage.
314314
Value can be an absolute number (ex: 5) or a percentage of the total clusters in the stage (ex: 50%).
315-
Absolute number is calculated from percentage by rounding up.
315+
Fractional results are rounded down. A minimum of 1 update is enforced.
316316
If not specified, all clusters in the stage are updated sequentially (effectively maxConcurrency = 1).
317317
Defaults to 1.
318318
pattern: ^((100|[0-9]{1,2})%|[0-9]+)$

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ spec:
917917
description: |-
918918
MaxConcurrency specifies the maximum number of clusters that can be updated concurrently within this stage.
919919
Value can be an absolute number (ex: 5) or a percentage of the total clusters in the stage (ex: 50%).
920-
Absolute number is calculated from percentage by rounding up.
920+
Fractional results are rounded down. A minimum of 1 update is enforced.
921921
If not specified, all clusters in the stage are updated sequentially (effectively maxConcurrency = 1).
922922
Defaults to 1.
923923
pattern: ^((100|[0-9]{1,2})%|[0-9]+)$

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ spec:
174174
description: |-
175175
MaxConcurrency specifies the maximum number of clusters that can be updated concurrently within this stage.
176176
Value can be an absolute number (ex: 5) or a percentage of the total clusters in the stage (ex: 50%).
177-
Absolute number is calculated from percentage by rounding up.
177+
Fractional results are rounded down. A minimum of 1 update is enforced.
178178
If not specified, all clusters in the stage are updated sequentially (effectively maxConcurrency = 1).
179179
Defaults to 1.
180180
pattern: ^((100|[0-9]{1,2})%|[0-9]+)$

pkg/controllers/updaterun/initialization.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -469,27 +469,39 @@ 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.
471471
func (r *Reconciler) recordOverrideSnapshots(ctx context.Context, placementKey types.NamespacedName, updateRun placementv1beta1.UpdateRunObj) error {
472+
var resourceSnapshotObjs []placementv1beta1.ResourceSnapshotObj
473+
var snapshotIndex int
472474
updateRunRef := klog.KObj(updateRun)
473475
updateRunSpec := updateRun.GetUpdateRunSpec()
474476
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+
}
475485

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)
485-
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)
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()
490503
}
491504

492-
resourceSnapshotObjs := resourceSnapshotList.GetResourceSnapshotObjs()
493505
if len(resourceSnapshotObjs) == 0 {
494506
err := controller.NewUserError(fmt.Errorf("no resourceSnapshots with index `%d` found for placement `%s`", snapshotIndex, placementKey))
495507
klog.ErrorS(err, "No specified resourceSnapshots found", "updateRun", updateRunRef)

pkg/controllers/updaterun/initialization_integration_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ var _ = Describe("Updaterun initialization tests", func() {
748748
Expect(k8sClient.Create(ctx, updateStrategy)).To(Succeed())
749749
})
750750

751-
It("Should fail to initialize if the specified resource snapshot index is invalid - not integer", func() {
751+
FIt("Should fail to initialize if the specified resource snapshot index is invalid - not integer", func() {
752752
By("Creating a new clusterStagedUpdateRun with invalid resource snapshot index")
753753
updateRun.Spec.ResourceSnapshotIndex = "invalid-index"
754754
Expect(k8sClient.Create(ctx, updateRun)).To(Succeed())
@@ -828,6 +828,29 @@ var _ = Describe("Updaterun initialization tests", func() {
828828
validateUpdateRunMetricsEmitted(generateInitializationFailedMetric(updateRun))
829829
})
830830

831+
It("Should put related ClusterResourceOverrides in the status with no resource index defined", func() {
832+
By("Creating a new resource snapshot")
833+
Expect(k8sClient.Create(ctx, resourceSnapshot)).To(Succeed())
834+
835+
By("Creating a new cluster resource override")
836+
Expect(k8sClient.Create(ctx, clusterResourceOverride)).To(Succeed())
837+
838+
By("Creating a new clusterStagedUpdateRun")
839+
updateRun.Spec.ResourceSnapshotIndex = ""
840+
Expect(k8sClient.Create(ctx, updateRun)).To(Succeed())
841+
842+
By("Validating the clusterStagedUpdateRun stats")
843+
initialized := generateSucceededInitializationStatus(crp, updateRun, policySnapshot, updateStrategy, clusterResourceOverride)
844+
want := generateExecutionStartedStatus(updateRun, initialized)
845+
validateClusterStagedUpdateRunStatus(ctx, updateRun, want, "")
846+
847+
By("Validating the clusterStagedUpdateRun initialized consistently")
848+
validateClusterStagedUpdateRunStatusConsistently(ctx, updateRun, want, "")
849+
850+
By("Checking update run status metrics are emitted")
851+
validateUpdateRunMetricsEmitted(generateProgressingMetric(updateRun))
852+
})
853+
831854
It("Should put related ClusterResourceOverrides in the status", func() {
832855
By("Creating a new resource snapshot")
833856
Expect(k8sClient.Create(ctx, resourceSnapshot)).To(Succeed())

0 commit comments

Comments
 (0)