Skip to content

Commit c92e2a3

Browse files
authored
Wait until startAfter before skipping upgrade job (#90)
1 parent 20dfb4c commit c92e2a3

2 files changed

Lines changed: 42 additions & 15 deletions

File tree

controllers/upgradejob_controller.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,15 @@ func (r *UpgradeJobReconciler) Reconcile(ctx context.Context, req ctrl.Request)
121121

122122
now := r.Clock.Now()
123123

124-
window, err := r.matchingUpgradeSuspensionWindow(ctx, uj, now)
125-
if err != nil {
126-
return ctrl.Result{}, fmt.Errorf("failed to search for matching upgrade suspension window: %w", err)
127-
}
128-
if window != nil {
129-
l.Info("Upgrade job skipped by UpgradeSuspensionWindow", "window", window.Name, "reason", window.Spec.Reason, "start", window.Spec.Start.Time, "end", window.Spec.End.Time)
130-
r.setStatusCondition(&uj.Status.Conditions, metav1.Condition{
131-
Type: managedupgradev1beta1.UpgradeJobConditionSucceeded,
132-
Status: metav1.ConditionTrue,
133-
Reason: managedupgradev1beta1.UpgradeJobReasonSkipped,
134-
Message: fmt.Sprintf("Upgrade job skipped by UpgradeSuspensionWindow %q, reason: %q", window.Name, window.Spec.Reason),
135-
})
136-
return ctrl.Result{}, r.Status().Update(ctx, &uj)
137-
}
138-
139124
if now.After(uj.Spec.StartBefore.Time) {
125+
skipped, err := r.checkAndMarkSkipped(ctx, uj, now)
126+
if err != nil {
127+
return ctrl.Result{}, err
128+
}
129+
if skipped {
130+
return ctrl.Result{}, nil
131+
}
132+
140133
r.setStatusCondition(&uj.Status.Conditions, metav1.Condition{
141134
Type: managedupgradev1beta1.UpgradeJobConditionFailed,
142135
Status: metav1.ConditionTrue,
@@ -148,6 +141,14 @@ func (r *UpgradeJobReconciler) Reconcile(ctx context.Context, req ctrl.Request)
148141
}
149142

150143
if !now.Before(uj.Spec.StartAfter.Time) {
144+
skipped, err := r.checkAndMarkSkipped(ctx, uj, now)
145+
if err != nil {
146+
return ctrl.Result{}, err
147+
}
148+
if skipped {
149+
return ctrl.Result{}, nil
150+
}
151+
151152
r.setStatusCondition(&uj.Status.Conditions, metav1.Condition{
152153
Type: managedupgradev1beta1.UpgradeJobConditionStarted,
153154
Status: metav1.ConditionTrue,
@@ -1010,3 +1011,22 @@ func (r *UpgradeJobReconciler) matchingUpgradeSuspensionWindow(ctx context.Conte
10101011

10111012
return nil, nil
10121013
}
1014+
1015+
// checkAndMarkSkipped checks if the upgrade job should be skipped due to an UpgradeSuspensionWindow and marks it as skipped if necessary.
1016+
func (r *UpgradeJobReconciler) checkAndMarkSkipped(ctx context.Context, uj managedupgradev1beta1.UpgradeJob, now time.Time) (skipped bool, err error) {
1017+
window, err := r.matchingUpgradeSuspensionWindow(ctx, uj, now)
1018+
if err != nil {
1019+
return true, fmt.Errorf("failed to search for matching upgrade suspension window: %w", err)
1020+
}
1021+
if window != nil {
1022+
log.FromContext(ctx).Info("Upgrade job skipped by UpgradeSuspensionWindow", "window", window.Name, "reason", window.Spec.Reason, "start", window.Spec.Start.Time, "end", window.Spec.End.Time)
1023+
r.setStatusCondition(&uj.Status.Conditions, metav1.Condition{
1024+
Type: managedupgradev1beta1.UpgradeJobConditionSucceeded,
1025+
Status: metav1.ConditionTrue,
1026+
Reason: managedupgradev1beta1.UpgradeJobReasonSkipped,
1027+
Message: fmt.Sprintf("Upgrade job skipped by UpgradeSuspensionWindow %q, reason: %q", window.Name, window.Spec.Reason),
1028+
})
1029+
return true, r.Status().Update(ctx, &uj)
1030+
}
1031+
return false, nil
1032+
}

controllers/upgradejob_controller_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,14 @@ func Test_UpgradeJobReconciler_Reconcile_Skipped_Job(t *testing.T) {
406406
ManagedUpstreamClusterVersionName: "version",
407407
}
408408

409+
step(t, "startAfter not yet reached, should not skip job yet", func(t *testing.T) {
410+
reconcileNTimes(t, subject, ctx, requestForObject(upgradeJob), 10)
411+
require.Nil(t, apimeta.FindStatusCondition(upgradeJob.Status.Conditions, managedupgradev1beta1.UpgradeJobConditionSucceeded))
412+
})
413+
409414
step(t, "Upgrade Skipped because of window", func(t *testing.T) {
415+
// Move to start after time
416+
clock.Advance(time.Hour)
410417
reconcileNTimes(t, subject, ctx, requestForObject(upgradeJob), 10)
411418
require.NoError(t, c.Get(ctx, requestForObject(upgradeJob).NamespacedName, upgradeJob))
412419
sc := apimeta.FindStatusCondition(upgradeJob.Status.Conditions, managedupgradev1beta1.UpgradeJobConditionSucceeded)

0 commit comments

Comments
 (0)