Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -847,14 +847,26 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
// sort jobs so that latest job is first
// with preference for non-failed jobs
sort.Slice(jobs, func(i, j int) bool {
if jobs[i] == nil || jobs[j] == nil {
return jobs[i] != nil
}
condI, failedI := getCondition(jobs[i], batchv1.JobFailed)
condJ, failedJ := getCondition(jobs[j], batchv1.JobFailed)
if failedI != failedJ {
return !failedI // non-failed job goes first
}
return condI.LastTransitionTime.After(condJ.LastTransitionTime.Time)
})
if jobs[0] == nil {
// all nil jobs
return
}
latest = jobs[0]
nilJobsIndex := len(jobs) - 1
for ; nilJobsIndex >= 0 && jobs[nilJobsIndex] == nil; nilJobsIndex-- {
}

jobs = jobs[:nilJobsIndex+1] // exclude nil jobs from list of jobs to delete
if len(jobs) <= maxRetainedJobs {
return
}
Expand All @@ -864,7 +876,7 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
}

// cleanup old failed jobs, n-1 recent jobs and the oldest job
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs); i++ {
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs)-1; i++ {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ankitathomas, one concern here, if we use the < len(jobs)-1 here, it never returns the last child, which means will retain 6 jobs instead of 5. Is it as expected? Thanks!

maxRetainedJobs := 5                                  // TODO: make this configurable

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question! The intention there is to preserve the oldest failing unpack job for debugging purposes when this issue happens again, even when it allows for retrying the unpack.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I see now, thanks!

toDelete = append(toDelete, jobs[maxRetainedJobs+i])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,15 @@ func TestSortUnpackJobs(t *testing.T) {
},
}
}
nilConditionJob := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "nc",
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue, bundleUnpackRefLabel: "test"},
},
Status: batchv1.JobStatus{
Conditions: nil,
},
}
failedJobs := []*batchv1.Job{
testJob("f-1", true, 1),
testJob("f-2", true, 2),
Expand Down Expand Up @@ -2006,6 +2015,24 @@ func TestSortUnpackJobs(t *testing.T) {
}, {
name: "empty job list",
maxRetained: 1,
}, {
name: "nil job in list",
maxRetained: 1,
jobs: []*batchv1.Job{
failedJobs[2],
nil,
failedJobs[1],
},
expectedLatest: failedJobs[2],
}, {
name: "nil condition",
maxRetained: 3,
jobs: []*batchv1.Job{
failedJobs[2],
nilConditionJob,
failedJobs[1],
},
expectedLatest: nilConditionJob,
}, {
name: "retain oldest",
maxRetained: 1,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.