Skip to content

Commit 5fe4295

Browse files
committed
handle nil jobs while sorting unpack jobs
Signed-off-by: Ankita Thomas <[email protected]>
1 parent 800b761 commit 5fe4295

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

pkg/controller/bundle/bundle_unpacker.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -863,24 +863,26 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
863863
// sort jobs so that latest job is first
864864
// with preference for non-failed jobs
865865
sort.Slice(jobs, func(i, j int) bool {
866+
if jobs[i] == nil || jobs[j] == nil {
867+
return jobs[i] != nil
868+
}
866869
condI, failedI := getCondition(jobs[i], batchv1.JobFailed)
867870
condJ, failedJ := getCondition(jobs[j], batchv1.JobFailed)
868871
if failedI != failedJ {
869872
return !failedI // non-failed job goes first
870873
}
871-
// sort multiple jobs without the Failed condition
872-
if condI == nil && condJ == nil {
873-
return jobs[i].CreationTimestamp.After(jobs[j].CreationTimestamp.Time)
874-
}
875-
if condI == nil {
876-
return false
877-
}
878-
if condJ == nil {
879-
return true
880-
}
881874
return condI.LastTransitionTime.After(condJ.LastTransitionTime.Time)
882875
})
876+
if jobs[0] == nil {
877+
// all nil jobs
878+
return
879+
}
883880
latest = jobs[0]
881+
nilJobsIndex := len(jobs) - 1
882+
for ; nilJobsIndex >= 0 && jobs[nilJobsIndex] == nil; nilJobsIndex-- {
883+
}
884+
885+
jobs = jobs[:nilJobsIndex+1] // exclude nil jobs from list of jobs to delete
884886
if len(jobs) <= maxRetainedJobs {
885887
return
886888
}
@@ -890,7 +892,7 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
890892
}
891893

892894
// cleanup old failed jobs, n-1 recent jobs and the oldest job
893-
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs); i++ {
895+
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs)-1; i++ {
894896
toDelete = append(toDelete, jobs[maxRetainedJobs+i])
895897
}
896898

pkg/controller/bundle/bundle_unpacker_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,14 +1998,14 @@ func TestSortUnpackJobs(t *testing.T) {
19981998
}
19991999
}
20002000
nilConditionJob := &batchv1.Job{
2001-
ObjectMeta: metav1.ObjectMeta{
2002-
Name: "nc",
2003-
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue, bundleUnpackRefLabel: "test"},
2004-
},
2005-
Status: batchv1.JobStatus{
2006-
Conditions: nil,
2007-
},
2008-
}
2001+
ObjectMeta: metav1.ObjectMeta{
2002+
Name: "nc",
2003+
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue, bundleUnpackRefLabel: "test"},
2004+
},
2005+
Status: batchv1.JobStatus{
2006+
Conditions: nil,
2007+
},
2008+
}
20092009
failedJobs := []*batchv1.Job{
20102010
testJob("f-1", true, 1),
20112011
testJob("f-2", true, 2),
@@ -2045,6 +2045,7 @@ func TestSortUnpackJobs(t *testing.T) {
20452045
nil,
20462046
failedJobs[1],
20472047
},
2048+
expectedLatest: failedJobs[2],
20482049
}, {
20492050
name: "nil condition",
20502051
maxRetained: 3,

0 commit comments

Comments
 (0)