Skip to content

Commit f0be27d

Browse files
Merge pull request #799 from ankitathomas/OCP34173-4.16
[release-4.16] OCPBUGS-36137: fix sorting unpack jobs
2 parents abccea9 + 782e7f8 commit f0be27d

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
@@ -850,14 +850,26 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
850850
// sort jobs so that latest job is first
851851
// with preference for non-failed jobs
852852
sort.Slice(jobs, func(i, j int) bool {
853+
if jobs[i] == nil || jobs[j] == nil {
854+
return jobs[i] != nil
855+
}
853856
condI, failedI := getCondition(jobs[i], batchv1.JobFailed)
854857
condJ, failedJ := getCondition(jobs[j], batchv1.JobFailed)
855858
if failedI != failedJ {
856859
return !failedI // non-failed job goes first
857860
}
858861
return condI.LastTransitionTime.After(condJ.LastTransitionTime.Time)
859862
})
863+
if jobs[0] == nil {
864+
// all nil jobs
865+
return
866+
}
860867
latest = jobs[0]
868+
nilJobsIndex := len(jobs) - 1
869+
for ; nilJobsIndex >= 0 && jobs[nilJobsIndex] == nil; nilJobsIndex-- {
870+
}
871+
872+
jobs = jobs[:nilJobsIndex+1] // exclude nil jobs from list of jobs to delete
861873
if len(jobs) <= maxRetainedJobs {
862874
return
863875
}
@@ -867,7 +879,7 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
867879
}
868880

869881
// cleanup old failed jobs, n-1 recent jobs and the oldest job
870-
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs); i++ {
882+
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs)-1; i++ {
871883
toDelete = append(toDelete, jobs[maxRetainedJobs+i])
872884
}
873885

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
@@ -1993,6 +1993,15 @@ func TestSortUnpackJobs(t *testing.T) {
19931993
},
19941994
}
19951995
}
1996+
nilConditionJob := &batchv1.Job{
1997+
ObjectMeta: metav1.ObjectMeta{
1998+
Name: "nc",
1999+
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue, bundleUnpackRefLabel: "test"},
2000+
},
2001+
Status: batchv1.JobStatus{
2002+
Conditions: nil,
2003+
},
2004+
}
19962005
failedJobs := []*batchv1.Job{
19972006
testJob("f-1", true, 1),
19982007
testJob("f-2", true, 2),
@@ -2024,6 +2033,24 @@ func TestSortUnpackJobs(t *testing.T) {
20242033
}, {
20252034
name: "empty job list",
20262035
maxRetained: 1,
2036+
}, {
2037+
name: "nil job in list",
2038+
maxRetained: 1,
2039+
jobs: []*batchv1.Job{
2040+
failedJobs[2],
2041+
nil,
2042+
failedJobs[1],
2043+
},
2044+
expectedLatest: failedJobs[2],
2045+
}, {
2046+
name: "nil condition",
2047+
maxRetained: 3,
2048+
jobs: []*batchv1.Job{
2049+
failedJobs[2],
2050+
nilConditionJob,
2051+
failedJobs[1],
2052+
},
2053+
expectedLatest: nilConditionJob,
20272054
}, {
20282055
name: "retain oldest",
20292056
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)