Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Implement default values for job history limits #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions pkg/controller/cronjobber/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,13 @@ func (jm *TZCronJobController) syncAll() {
// cleanupFinishedJobs cleanups finished jobs created by a TZCronJob
func cleanupFinishedJobs(sj *cronjobberv1.TZCronJob, js []batchv1.Job, jc jobControlInterface,
sjc sjControlInterface, recorder record.EventRecorder, logger *zap.SugaredLogger) {
// If neither limits are active, there is no need to do anything.
if sj.Spec.FailedJobsHistoryLimit == nil && sj.Spec.SuccessfulJobsHistoryLimit == nil {
return
// According to the documentation and the behaviour of the original cronjob we set the default values:
if sj.Spec.FailedJobsHistoryLimit == nil {
sj.Spec.FailedJobsHistoryLimit = pointerInt32(1)
}

if sj.Spec.SuccessfulJobsHistoryLimit == nil {
sj.Spec.SuccessfulJobsHistoryLimit = pointerInt32(3)
}

failedJobs := []batchv1.Job{}
Expand All @@ -168,23 +172,19 @@ func cleanupFinishedJobs(sj *cronjobberv1.TZCronJob, js []batchv1.Job, jc jobCon
}
}

if sj.Spec.SuccessfulJobsHistoryLimit != nil {
removeOldestJobs(sj,
succesfulJobs,
jc,
*sj.Spec.SuccessfulJobsHistoryLimit,
recorder,
logger)
}

if sj.Spec.FailedJobsHistoryLimit != nil {
removeOldestJobs(sj,
failedJobs,
jc,
*sj.Spec.FailedJobsHistoryLimit,
recorder,
logger)
}
removeOldestJobs(sj,
succesfulJobs,
jc,
*sj.Spec.SuccessfulJobsHistoryLimit,
recorder,
logger)

removeOldestJobs(sj,
failedJobs,
jc,
*sj.Spec.FailedJobsHistoryLimit,
recorder,
logger)

// Update the TZCronJob, in case jobs were removed from the list.
if _, err := sjc.UpdateStatus(sj); err != nil {
Expand Down
13 changes: 7 additions & 6 deletions pkg/controller/cronjobber/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,34 +449,35 @@ func TestCleanupFinishedJobs_DeleteOrNot(t *testing.T) {
{"2016-05-19T09:00:00Z", T, F, F, F},
}, justBeforeTheHour(), &limitThree, &limitThree, 0},

// This test case should trigger the short-circuit
// If the limits are disabled, apply the defaults (3 success, 1 failed)
"limits disabled": {
[]CleanupJobSpec{
{"2016-05-19T04:00:00Z", T, T, F, F},
{"2016-05-19T05:00:00Z", T, F, F, F},
{"2016-05-19T05:00:00Z", T, F, T, F},
{"2016-05-19T06:00:00Z", T, T, F, F},
{"2016-05-19T07:00:00Z", T, T, F, F},
{"2016-05-19T08:00:00Z", T, F, F, F},
{"2016-05-19T08:00:00Z", T, F, T, F},
{"2016-05-19T09:00:00Z", T, F, F, F},
}, justBeforeTheHour(), nil, nil, 0},

"success limit disabled": {
[]CleanupJobSpec{
{"2016-05-19T04:00:00Z", T, T, F, F},
{"2016-05-19T04:00:00Z", T, T, T, F},
{"2016-05-19T05:00:00Z", T, F, F, F},
{"2016-05-19T06:00:00Z", T, T, F, F},
{"2016-05-19T07:00:00Z", T, T, F, F},
{"2016-05-19T08:00:00Z", T, F, F, F},
{"2016-05-19T09:00:00Z", T, F, F, F},
{"2016-05-19T10:00:00Z", T, T, F, F},
}, justBeforeTheHour(), nil, &limitThree, 0},

"failure limit disabled": {
[]CleanupJobSpec{
{"2016-05-19T04:00:00Z", T, T, F, F},
{"2016-05-19T05:00:00Z", T, F, F, F},
{"2016-05-19T05:00:00Z", T, F, T, F},
{"2016-05-19T06:00:00Z", T, T, F, F},
{"2016-05-19T07:00:00Z", T, T, F, F},
{"2016-05-19T08:00:00Z", T, F, F, F},
{"2016-05-19T08:00:00Z", T, F, T, F},
{"2016-05-19T09:00:00Z", T, F, F, F},
}, justBeforeTheHour(), &limitThree, nil, 0},

Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/cronjobber/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,7 @@ func getCurrentTimeInZone(sj *cronjobberv1.TZCronJob) (time.Time, error) {

return time.Now().In(loc), nil
}

func pointerInt32(i int32) *int32 {
return &i
}