Skip to content

Commit 79cec87

Browse files
authored
📖 Update cronjob_controller.go: simplify setting mostRecentTime logic (#3700)
simplify setting mostRecentTime logic The change consolidates conditions under which mostRecentTime can be set to scheduledTimeForJob for better readability
1 parent 5e81ad8 commit 79cec87

File tree

3 files changed

+5
-9
lines changed

3 files changed

+5
-9
lines changed

‎docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
217217
continue
218218
}
219219
if scheduledTimeForJob != nil {
220-
if mostRecentTime == nil {
221-
mostRecentTime = scheduledTimeForJob
222-
} else if mostRecentTime.Before(*scheduledTimeForJob) {
220+
if mostRecentTime == nil || mostRecentTime.Before(*scheduledTimeForJob) {
223221
mostRecentTime = scheduledTimeForJob
224222
}
225223
}
@@ -304,7 +302,7 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
304302
if int32(i) >= int32(len(successfulJobs))-*cronJob.Spec.SuccessfulJobsHistoryLimit {
305303
break
306304
}
307-
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); (err) != nil {
305+
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); err != nil {
308306
log.Error(err, "unable to delete old successful job", "job", job)
309307
} else {
310308
log.V(0).Info("deleted old successful job", "job", job)

‎hack/docs/internal/cronjob-tutorial/controller_implementation.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ const ControllerReconcileLogic = `log := log.FromContext(ctx)
204204
continue
205205
}
206206
if scheduledTimeForJob != nil {
207-
if mostRecentTime == nil {
208-
mostRecentTime = scheduledTimeForJob
209-
} else if mostRecentTime.Before(*scheduledTimeForJob) {
207+
if mostRecentTime == nil || mostRecentTime.Before(*scheduledTimeForJob) {
210208
mostRecentTime = scheduledTimeForJob
211209
}
212210
}
@@ -291,7 +289,7 @@ const ControllerReconcileLogic = `log := log.FromContext(ctx)
291289
if int32(i) >= int32(len(successfulJobs))-*cronJob.Spec.SuccessfulJobsHistoryLimit {
292290
break
293291
}
294-
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); (err) != nil {
292+
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); err != nil {
295293
log.Error(err, "unable to delete old successful job", "job", job)
296294
} else {
297295
log.V(0).Info("deleted old successful job", "job", job)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
exampleTargetexampleCodeexampleCodeexampleCodeexampleCodeexampleCodeexampleCode
1+
exampleTargetexampleCodeexampleCodeexampleCodeexampleCodeexampleCodeexampleCodeexampleCode

0 commit comments

Comments
 (0)