Skip to content

Commit 20a3cbc

Browse files
[OCPBUGS-34173]: fix sorting unpack jobs (#3299)
* fix sorting unpack jobs Signed-off-by: Ankita Thomas <[email protected]> * Add test cases to reproduce panic Signed-off-by: Catherine Chan-Tse <[email protected]> * handle nil jobs while sorting unpack jobs Signed-off-by: Ankita Thomas <[email protected]> --------- Signed-off-by: Ankita Thomas <[email protected]> Signed-off-by: Catherine Chan-Tse <[email protected]> Co-authored-by: Catherine Chan-Tse <[email protected]>
1 parent fa8824c commit 20a3cbc

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

pkg/controller/bundle/bundle_unpacker.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -863,14 +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
}
871874
return condI.LastTransitionTime.After(condJ.LastTransitionTime.Time)
872875
})
876+
if jobs[0] == nil {
877+
// all nil jobs
878+
return
879+
}
873880
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
874886
if len(jobs) <= maxRetainedJobs {
875887
return
876888
}
@@ -880,7 +892,7 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
880892
}
881893

882894
// cleanup old failed jobs, n-1 recent jobs and the oldest job
883-
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs); i++ {
895+
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs)-1; i++ {
884896
toDelete = append(toDelete, jobs[maxRetainedJobs+i])
885897
}
886898

pkg/controller/bundle/bundle_unpacker_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,15 @@ func TestSortUnpackJobs(t *testing.T) {
19971997
},
19981998
}
19991999
}
2000+
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+
}
20002009
failedJobs := []*batchv1.Job{
20012010
testJob("f-1", true, 1),
20022011
testJob("f-2", true, 2),
@@ -2028,6 +2037,24 @@ func TestSortUnpackJobs(t *testing.T) {
20282037
}, {
20292038
name: "empty job list",
20302039
maxRetained: 1,
2040+
}, {
2041+
name: "nil job in list",
2042+
maxRetained: 1,
2043+
jobs: []*batchv1.Job{
2044+
failedJobs[2],
2045+
nil,
2046+
failedJobs[1],
2047+
},
2048+
expectedLatest: failedJobs[2],
2049+
}, {
2050+
name: "nil condition",
2051+
maxRetained: 3,
2052+
jobs: []*batchv1.Job{
2053+
failedJobs[2],
2054+
nilConditionJob,
2055+
failedJobs[1],
2056+
},
2057+
expectedLatest: nilConditionJob,
20312058
}, {
20322059
name: "retain oldest",
20332060
maxRetained: 1,

0 commit comments

Comments
 (0)