Skip to content
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
5 changes: 5 additions & 0 deletions pkg/controller/bundle/bundle_unpacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,11 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
if failedI != failedJ {
return !failedI // non-failed job goes first
}
// If both jobs have no failed condition, condI and condJ will be nil
// fallback to sorting by CreationTimestamp
if condI == nil || condJ == nil {
return jobs[i].CreationTimestamp.After(jobs[j].CreationTimestamp.Time)
}
return condI.LastTransitionTime.After(condJ.LastTransitionTime.Time)
})
if jobs[0] == nil {
Expand Down
19 changes: 17 additions & 2 deletions pkg/controller/bundle/bundle_unpacker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1951,8 +1951,9 @@ func TestSortUnpackJobs(t *testing.T) {
}
return &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue, BundleUnpackRefLabel: "test"},
Name: name,
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue, BundleUnpackRefLabel: "test"},
CreationTimestamp: metav1.Time{Time: time.Unix(ts, 0)},
},
Status: batchv1.JobStatus{
Conditions: conditions,
Expand All @@ -1976,6 +1977,11 @@ func TestSortUnpackJobs(t *testing.T) {
testJob("f-5", true, 5),
}
nonFailedJob := testJob("s-1", false, 1)
nonFailedJobs := []*batchv1.Job{
testJob("nf-1", false, 1),
testJob("nf-2", false, 2),
testJob("nf-3", false, 3),
}
for _, tc := range []struct {
name string
jobs []*batchv1.Job
Expand Down Expand Up @@ -2053,6 +2059,15 @@ func TestSortUnpackJobs(t *testing.T) {
nonFailedJob,
},
expectedLatest: nonFailedJob,
}, {
name: "multiple non-failed jobs sorted by creation time",
maxRetained: 3,
jobs: []*batchv1.Job{
nonFailedJobs[0],
nonFailedJobs[2],
nonFailedJobs[1],
},
expectedLatest: nonFailedJobs[2], // latest creation time should be first
},
} {
latest, toDelete := sortUnpackJobs(tc.jobs, tc.maxRetained)
Expand Down
Loading