Skip to content

Commit ea32706

Browse files
ankitathomasoceanc80
authored andcommitted
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]> Upstream-repository: operator-lifecycle-manager Upstream-commit: 20a3cbcde1f46ed7f4d56042ccc8b5d962e4d1b8
1 parent 89cf370 commit ea32706

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

staging/operator-lifecycle-manager/pkg/controller/bundle/bundle_unpacker.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,14 +847,26 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
847847
// sort jobs so that latest job is first
848848
// with preference for non-failed jobs
849849
sort.Slice(jobs, func(i, j int) bool {
850+
if jobs[i] == nil || jobs[j] == nil {
851+
return jobs[i] != nil
852+
}
850853
condI, failedI := getCondition(jobs[i], batchv1.JobFailed)
851854
condJ, failedJ := getCondition(jobs[j], batchv1.JobFailed)
852855
if failedI != failedJ {
853856
return !failedI // non-failed job goes first
854857
}
855858
return condI.LastTransitionTime.After(condJ.LastTransitionTime.Time)
856859
})
860+
if jobs[0] == nil {
861+
// all nil jobs
862+
return
863+
}
857864
latest = jobs[0]
865+
nilJobsIndex := len(jobs) - 1
866+
for ; nilJobsIndex >= 0 && jobs[nilJobsIndex] == nil; nilJobsIndex-- {
867+
}
868+
869+
jobs = jobs[:nilJobsIndex+1] // exclude nil jobs from list of jobs to delete
858870
if len(jobs) <= maxRetainedJobs {
859871
return
860872
}
@@ -864,7 +876,7 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
864876
}
865877

866878
// cleanup old failed jobs, n-1 recent jobs and the oldest job
867-
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs); i++ {
879+
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs)-1; i++ {
868880
toDelete = append(toDelete, jobs[maxRetainedJobs+i])
869881
}
870882

staging/operator-lifecycle-manager/pkg/controller/bundle/bundle_unpacker_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,15 @@ func TestSortUnpackJobs(t *testing.T) {
19751975
},
19761976
}
19771977
}
1978+
nilConditionJob := &batchv1.Job{
1979+
ObjectMeta: metav1.ObjectMeta{
1980+
Name: "nc",
1981+
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue, bundleUnpackRefLabel: "test"},
1982+
},
1983+
Status: batchv1.JobStatus{
1984+
Conditions: nil,
1985+
},
1986+
}
19781987
failedJobs := []*batchv1.Job{
19791988
testJob("f-1", true, 1),
19801989
testJob("f-2", true, 2),
@@ -2006,6 +2015,24 @@ func TestSortUnpackJobs(t *testing.T) {
20062015
}, {
20072016
name: "empty job list",
20082017
maxRetained: 1,
2018+
}, {
2019+
name: "nil job in list",
2020+
maxRetained: 1,
2021+
jobs: []*batchv1.Job{
2022+
failedJobs[2],
2023+
nil,
2024+
failedJobs[1],
2025+
},
2026+
expectedLatest: failedJobs[2],
2027+
}, {
2028+
name: "nil condition",
2029+
maxRetained: 3,
2030+
jobs: []*batchv1.Job{
2031+
failedJobs[2],
2032+
nilConditionJob,
2033+
failedJobs[1],
2034+
},
2035+
expectedLatest: nilConditionJob,
20092036
}, {
20102037
name: "retain oldest",
20112038
maxRetained: 1,

vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/controller/bundle/bundle_unpacker.go

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)