Skip to content

Commit c4f5c0a

Browse files
committed
reformat and add label to distinguish between approval object tasks
Signed-off-by: Britania Rodriguez Reyes <britaniar@microsoft.com>
1 parent 2b1e9a7 commit c4f5c0a

File tree

9 files changed

+120
-84
lines changed

9 files changed

+120
-84
lines changed

apis/placement/v1beta1/commons.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ const (
158158
// TargetUpdateRunLabel indicates the target update run on a staged run related object.
159159
TargetUpdateRunLabel = FleetPrefix + "targetupdaterun"
160160

161+
// TargetStageTaskLabel indicates the target stage task on a staged run related object.
162+
TargetStageTaskLabel = FleetPrefix + "targetstagetask"
163+
161164
// UpdateRunDeleteStageName is the name of delete stage in the staged update run.
162165
UpdateRunDeleteStageName = FleetPrefix + "deleteStage"
163166

@@ -167,6 +170,12 @@ const (
167170
// TargetUpdatingStageNameLabel indicates the updating stage name on a staged run related object.
168171
TargetUpdatingStageNameLabel = FleetPrefix + "targetUpdatingStage"
169172

173+
// BeforeStageTaskLabelValue is the before stage task label value.
174+
BeforeStageTaskLabelValue = "beforeStage"
175+
176+
// AfterStageTaskLabelValue is the after stage task label value.
177+
AfterStageTaskLabelValue = "afterStage"
178+
170179
// BeforeStageApprovalTaskNameFmt is the format of the before stage approval task name.
171180
BeforeStageApprovalTaskNameFmt = "%s-before-%s"
172181

apis/placement/v1beta1/stageupdate_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ type ApprovalRequestObjList interface {
679679
// - `TargetUpdateRun`: Points to the cluster staged update run that this approval request is for.
680680
// - `TargetStage`: The name of the stage that this approval request is for.
681681
// - `IsLatestUpdateRunApproval`: Indicates whether this approval request is the latest one related to this update run.
682+
// - `TargetStageTask`: Indicates whether this approval request is for the before or after stage task.
682683
type ClusterApprovalRequest struct {
683684
metav1.TypeMeta `json:",inline"`
684685
metav1.ObjectMeta `json:"metadata,omitempty"`
@@ -925,6 +926,7 @@ func (s *StagedUpdateStrategyList) GetUpdateStrategyObjs() []UpdateStrategyObj {
925926
// - `TargetUpdateRun`: Points to the staged update run that this approval request is for.
926927
// - `TargetStage`: The name of the stage that this approval request is for.
927928
// - `IsLatestUpdateRunApproval`: Indicates whether this approval request is the latest one related to this update run.
929+
// - `TargetStageTask`: Indicates whether this approval request is for the before or after stage task.
928930
type ApprovalRequest struct {
929931
metav1.TypeMeta `json:",inline"`
930932
metav1.ObjectMeta `json:"metadata,omitempty"`

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ spec:
4141
- `TargetUpdateRun`: Points to the staged update run that this approval request is for.
4242
- `TargetStage`: The name of the stage that this approval request is for.
4343
- `IsLatestUpdateRunApproval`: Indicates whether this approval request is the latest one related to this update run.
44+
- `TargetStageTask`: Indicates whether this approval request is for the before or after stage task.
4445
properties:
4546
apiVersion:
4647
description: |-

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ spec:
172172
- `TargetUpdateRun`: Points to the cluster staged update run that this approval request is for.
173173
- `TargetStage`: The name of the stage that this approval request is for.
174174
- `IsLatestUpdateRunApproval`: Indicates whether this approval request is the latest one related to this update run.
175+
- `TargetStageTask`: Indicates whether this approval request is for the before or after stage task.
175176
properties:
176177
apiVersion:
177178
description: |-

pkg/controllers/updaterun/execution.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ func (r *Reconciler) execute(
8383
// which would update the lastTransitionTime even though the status hasn't effectively changed.
8484
markUpdateRunProgressingIfNotWaitingOrStuck(updateRun)
8585
if updatingStageIndex < len(updateRunStatus.StagesStatus) {
86-
maxConcurrency, err := calculateMaxConcurrencyValue(updateRunStatus, updatingStageIndex)
87-
if err != nil {
88-
return false, 0, err
89-
}
9086
updatingStageStatus = &updateRunStatus.StagesStatus[updatingStageIndex]
9187
approved, err := r.checkBeforeStageTasksStatus(ctx, updatingStageIndex, updateRun)
9288
if err != nil {
@@ -97,6 +93,10 @@ func (r *Reconciler) execute(
9793
markUpdateRunWaiting(updateRun, fmt.Sprintf(condition.UpdateRunWaitingMessageFmt, "before-stage", updatingStageStatus.StageName))
9894
return false, stageUpdatingWaitTime, nil
9995
}
96+
maxConcurrency, err := calculateMaxConcurrencyValue(updateRunStatus, updatingStageIndex)
97+
if err != nil {
98+
return false, 0, err
99+
}
100100
waitTime, err = r.executeUpdatingStage(ctx, updateRun, updatingStageIndex, toBeUpdatedBindings, maxConcurrency)
101101
// The execution has not finished yet.
102102
return false, waitTime, err
@@ -111,17 +111,17 @@ func (r *Reconciler) execute(
111111
func (r *Reconciler) checkBeforeStageTasksStatus(ctx context.Context, updatingStageIndex int, updateRun placementv1beta1.UpdateRunObj) (bool, error) {
112112
updateRunRef := klog.KObj(updateRun)
113113
updateRunStatus := updateRun.GetUpdateRunStatus()
114-
updatingStageStatus := &updateRunStatus.StagesStatus[updatingStageIndex]
115114
updatingStage := &updateRunStatus.UpdateStrategySnapshot.Stages[updatingStageIndex]
116115
if updatingStage.BeforeStageTasks == nil {
117116
klog.V(2).InfoS("There is no before stage task for this stage", "stage", updatingStage.Name, "updateRun", updateRunRef)
118117
return true, nil
119118
}
120119

120+
updatingStageStatus := &updateRunStatus.StagesStatus[updatingStageIndex]
121121
for i, task := range updatingStage.BeforeStageTasks {
122122
switch task.Type {
123123
case placementv1beta1.StageTaskTypeApproval:
124-
approved, err := r.handleStageApprovalTask(ctx, &updatingStageStatus.BeforeStageTaskStatus[i], updatingStage, updateRun)
124+
approved, err := r.handleStageApprovalTask(ctx, &updatingStageStatus.BeforeStageTaskStatus[i], updatingStage, updateRun, placementv1beta1.BeforeStageTaskLabelValue)
125125
if err != nil {
126126
return false, err
127127
}
@@ -418,7 +418,7 @@ func (r *Reconciler) checkAfterStageTasksStatus(ctx context.Context, updatingSta
418418
klog.V(2).InfoS("The after stage wait task has completed", "stage", updatingStage.Name, "updateRun", updateRunRef)
419419
}
420420
case placementv1beta1.StageTaskTypeApproval:
421-
approved, err := r.handleStageApprovalTask(ctx, &updatingStageStatus.AfterStageTaskStatus[i], updatingStage, updateRun)
421+
approved, err := r.handleStageApprovalTask(ctx, &updatingStageStatus.AfterStageTaskStatus[i], updatingStage, updateRun, placementv1beta1.AfterStageTaskLabelValue)
422422
if err != nil {
423423
return false, -1, err
424424
}
@@ -440,6 +440,7 @@ func (r *Reconciler) handleStageApprovalTask(
440440
stageTaskStatus *placementv1beta1.StageTaskStatus,
441441
updatingStage *placementv1beta1.StageConfig,
442442
updateRun placementv1beta1.UpdateRunObj,
443+
stageTaskType string,
443444
) (bool, error) {
444445
updateRunRef := klog.KObj(updateRun)
445446

@@ -450,7 +451,7 @@ func (r *Reconciler) handleStageApprovalTask(
450451
}
451452

452453
// Check if the approval request has been created.
453-
approvalRequest := buildApprovalRequestObject(types.NamespacedName{Name: stageTaskStatus.ApprovalRequestName, Namespace: updateRun.GetNamespace()}, updatingStage.Name, updateRun.GetName())
454+
approvalRequest := buildApprovalRequestObject(types.NamespacedName{Name: stageTaskStatus.ApprovalRequestName, Namespace: updateRun.GetNamespace()}, updatingStage.Name, updateRun.GetName(), stageTaskType)
454455
requestRef := klog.KObj(approvalRequest)
455456
if err := r.Client.Create(ctx, approvalRequest); err != nil {
456457
if apierrors.IsAlreadyExists(err) {
@@ -618,9 +619,9 @@ func checkClusterUpdateResult(
618619
return false, nil
619620
}
620621

621-
// buildApprovalRequestObject creates an approval request object for after-stage tasks.
622+
// buildApprovalRequestObject creates an approval request object for before-stage or after-stage tasks.
622623
// It returns a ClusterApprovalRequest if namespace is empty, otherwise returns an ApprovalRequest.
623-
func buildApprovalRequestObject(namespacedName types.NamespacedName, stageName, updateRunName string) placementv1beta1.ApprovalRequestObj {
624+
func buildApprovalRequestObject(namespacedName types.NamespacedName, stageName, updateRunName, stageTaskType string) placementv1beta1.ApprovalRequestObj {
624625
var approvalRequest placementv1beta1.ApprovalRequestObj
625626
if namespacedName.Namespace == "" {
626627
approvalRequest = &placementv1beta1.ClusterApprovalRequest{
@@ -629,6 +630,7 @@ func buildApprovalRequestObject(namespacedName types.NamespacedName, stageName,
629630
Labels: map[string]string{
630631
placementv1beta1.TargetUpdatingStageNameLabel: stageName,
631632
placementv1beta1.TargetUpdateRunLabel: updateRunName,
633+
placementv1beta1.TargetStageTaskLabel: stageTaskType,
632634
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
633635
},
634636
},
@@ -645,6 +647,7 @@ func buildApprovalRequestObject(namespacedName types.NamespacedName, stageName,
645647
Labels: map[string]string{
646648
placementv1beta1.TargetUpdatingStageNameLabel: stageName,
647649
placementv1beta1.TargetUpdateRunLabel: updateRunName,
650+
placementv1beta1.TargetStageTaskLabel: stageTaskType,
648651
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
649652
},
650653
},

pkg/controllers/updaterun/execution_integration_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ var _ = Describe("UpdateRun execution tests - double stages", func() {
173173
Labels: map[string]string{
174174
placementv1beta1.TargetUpdatingStageNameLabel: updateRun.Status.StagesStatus[0].StageName,
175175
placementv1beta1.TargetUpdateRunLabel: updateRun.Name,
176+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.BeforeStageTaskLabelValue,
176177
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
177178
},
178179
},
@@ -324,6 +325,7 @@ var _ = Describe("UpdateRun execution tests - double stages", func() {
324325
Labels: map[string]string{
325326
placementv1beta1.TargetUpdatingStageNameLabel: updateRun.Status.StagesStatus[0].StageName,
326327
placementv1beta1.TargetUpdateRunLabel: updateRun.Name,
328+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.AfterStageTaskLabelValue,
327329
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
328330
},
329331
},
@@ -379,6 +381,7 @@ var _ = Describe("UpdateRun execution tests - double stages", func() {
379381
Labels: map[string]string{
380382
placementv1beta1.TargetUpdatingStageNameLabel: updateRun.Status.StagesStatus[1].StageName,
381383
placementv1beta1.TargetUpdateRunLabel: updateRun.Name,
384+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.BeforeStageTaskLabelValue,
382385
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
383386
},
384387
},
@@ -531,6 +534,7 @@ var _ = Describe("UpdateRun execution tests - double stages", func() {
531534
Labels: map[string]string{
532535
placementv1beta1.TargetUpdatingStageNameLabel: updateRun.Status.StagesStatus[1].StageName,
533536
placementv1beta1.TargetUpdateRunLabel: updateRun.Name,
537+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.AfterStageTaskLabelValue,
534538
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
535539
},
536540
},
@@ -1085,6 +1089,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
10851089
Labels: map[string]string{
10861090
placementv1beta1.TargetUpdatingStageNameLabel: updateRun.Status.StagesStatus[0].StageName,
10871091
placementv1beta1.TargetUpdateRunLabel: updateRun.Name,
1092+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.AfterStageTaskLabelValue,
10881093
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
10891094
},
10901095
},
@@ -1306,6 +1311,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
13061311
Labels: map[string]string{
13071312
placementv1beta1.TargetUpdatingStageNameLabel: updateRun.Status.StagesStatus[0].StageName,
13081313
placementv1beta1.TargetUpdateRunLabel: updateRun.Name,
1314+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.BeforeStageTaskLabelValue,
13091315
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
13101316
},
13111317
},
@@ -1339,6 +1345,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
13391345
Labels: map[string]string{
13401346
placementv1beta1.TargetUpdatingStageNameLabel: updateRun.Status.StagesStatus[0].StageName,
13411347
placementv1beta1.TargetUpdateRunLabel: updateRun.Name,
1348+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.BeforeStageTaskLabelValue,
13421349
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
13431350
},
13441351
},
@@ -1441,6 +1448,7 @@ var _ = Describe("UpdateRun execution tests - single stage", func() {
14411448
Labels: map[string]string{
14421449
placementv1beta1.TargetUpdatingStageNameLabel: updateRun.Status.StagesStatus[0].StageName,
14431450
placementv1beta1.TargetUpdateRunLabel: updateRun.Name,
1451+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.AfterStageTaskLabelValue,
14441452
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
14451453
},
14461454
},

pkg/controllers/updaterun/execution_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,22 +346,25 @@ func TestBuildApprovalRequestObject(t *testing.T) {
346346
namespacedName types.NamespacedName
347347
stageName string
348348
updateRunName string
349+
stageTaskType string
349350
want placementv1beta1.ApprovalRequestObj
350351
}{
351352
{
352353
name: "should create ClusterApprovalRequest when namespace is empty",
353354
namespacedName: types.NamespacedName{
354-
Name: "test-approval-request",
355+
Name: fmt.Sprintf(placementv1beta1.BeforeStageApprovalTaskNameFmt, "test-update-run", "test-stage"),
355356
Namespace: "",
356357
},
357358
stageName: "test-stage",
358359
updateRunName: "test-update-run",
360+
stageTaskType: placementv1beta1.BeforeStageTaskLabelValue,
359361
want: &placementv1beta1.ClusterApprovalRequest{
360362
ObjectMeta: metav1.ObjectMeta{
361-
Name: "test-approval-request",
363+
Name: fmt.Sprintf(placementv1beta1.BeforeStageApprovalTaskNameFmt, "test-update-run", "test-stage"),
362364
Labels: map[string]string{
363365
placementv1beta1.TargetUpdatingStageNameLabel: "test-stage",
364366
placementv1beta1.TargetUpdateRunLabel: "test-update-run",
367+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.BeforeStageTaskLabelValue,
365368
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
366369
},
367370
},
@@ -374,18 +377,20 @@ func TestBuildApprovalRequestObject(t *testing.T) {
374377
{
375378
name: "should create namespaced ApprovalRequest when namespace is provided",
376379
namespacedName: types.NamespacedName{
377-
Name: "test-approval-request",
380+
Name: fmt.Sprintf(placementv1beta1.AfterStageApprovalTaskNameFmt, "test-update-run", "test-stage"),
378381
Namespace: "test-namespace",
379382
},
380383
stageName: "test-stage",
381384
updateRunName: "test-update-run",
385+
stageTaskType: placementv1beta1.AfterStageTaskLabelValue,
382386
want: &placementv1beta1.ApprovalRequest{
383387
ObjectMeta: metav1.ObjectMeta{
384-
Name: "test-approval-request",
388+
Name: fmt.Sprintf(placementv1beta1.AfterStageApprovalTaskNameFmt, "test-update-run", "test-stage"),
385389
Namespace: "test-namespace",
386390
Labels: map[string]string{
387391
placementv1beta1.TargetUpdatingStageNameLabel: "test-stage",
388392
placementv1beta1.TargetUpdateRunLabel: "test-update-run",
393+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.AfterStageTaskLabelValue,
389394
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
390395
},
391396
},
@@ -399,7 +404,7 @@ func TestBuildApprovalRequestObject(t *testing.T) {
399404

400405
for _, test := range tests {
401406
t.Run(test.name, func(t *testing.T) {
402-
got := buildApprovalRequestObject(test.namespacedName, test.stageName, test.updateRunName)
407+
got := buildApprovalRequestObject(test.namespacedName, test.stageName, test.updateRunName, test.stageTaskType)
403408

404409
// Compare the whole objects using cmp.Diff with ignore options
405410
if diff := cmp.Diff(test.want, got); diff != "" {
@@ -1035,6 +1040,7 @@ func TestCheckBeforeStageTasksStatus_NegativeCases(t *testing.T) {
10351040
Labels: map[string]string{
10361041
placementv1beta1.TargetUpdatingStageNameLabel: stageName,
10371042
placementv1beta1.TargetUpdateRunLabel: testUpdateRunName,
1043+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.BeforeStageTaskLabelValue,
10381044
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
10391045
},
10401046
},
@@ -1091,6 +1097,7 @@ func TestCheckBeforeStageTasksStatus_NegativeCases(t *testing.T) {
10911097
Labels: map[string]string{
10921098
placementv1beta1.TargetUpdatingStageNameLabel: stageName,
10931099
placementv1beta1.TargetUpdateRunLabel: testUpdateRunName,
1100+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.BeforeStageTaskLabelValue,
10941101
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
10951102
},
10961103
},
@@ -1147,6 +1154,7 @@ func TestCheckBeforeStageTasksStatus_NegativeCases(t *testing.T) {
11471154
Labels: map[string]string{
11481155
placementv1beta1.TargetUpdatingStageNameLabel: stageName,
11491156
placementv1beta1.TargetUpdateRunLabel: testUpdateRunName,
1157+
placementv1beta1.TargetStageTaskLabel: placementv1beta1.BeforeStageTaskLabelValue,
11501158
placementv1beta1.IsLatestUpdateRunApprovalLabel: "true",
11511159
},
11521160
},

0 commit comments

Comments
 (0)