Skip to content

Commit 9d4d9f8

Browse files
Merge pull request #817 from openshift-cherrypick-robot/cherry-pick-799-to-release-4.15
[release-4.15] OCPBUGS-36812: fix sorting unpack jobs
2 parents 28befe2 + ea32706 commit 9d4d9f8

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
@@ -860,14 +860,26 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
860860
// sort jobs so that latest job is first
861861
// with preference for non-failed jobs
862862
sort.Slice(jobs, func(i, j int) bool {
863+
if jobs[i] == nil || jobs[j] == nil {
864+
return jobs[i] != nil
865+
}
863866
condI, failedI := getCondition(jobs[i], batchv1.JobFailed)
864867
condJ, failedJ := getCondition(jobs[j], batchv1.JobFailed)
865868
if failedI != failedJ {
866869
return !failedI // non-failed job goes first
867870
}
868871
return condI.LastTransitionTime.After(condJ.LastTransitionTime.Time)
869872
})
873+
if jobs[0] == nil {
874+
// all nil jobs
875+
return
876+
}
870877
latest = jobs[0]
878+
nilJobsIndex := len(jobs) - 1
879+
for ; nilJobsIndex >= 0 && jobs[nilJobsIndex] == nil; nilJobsIndex-- {
880+
}
881+
882+
jobs = jobs[:nilJobsIndex+1] // exclude nil jobs from list of jobs to delete
871883
if len(jobs) <= maxRetainedJobs {
872884
return
873885
}
@@ -877,7 +889,7 @@ func sortUnpackJobs(jobs []*batchv1.Job, maxRetainedJobs int) (latest *batchv1.J
877889
}
878890

879891
// cleanup old failed jobs, n-1 recent jobs and the oldest job
880-
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs); i++ {
892+
for i := 0; i < maxRetainedJobs && i+maxRetainedJobs < len(jobs)-1; i++ {
881893
toDelete = append(toDelete, jobs[maxRetainedJobs+i])
882894
}
883895

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
@@ -1979,6 +1979,15 @@ func TestSortUnpackJobs(t *testing.T) {
19791979
},
19801980
}
19811981
}
1982+
nilConditionJob := &batchv1.Job{
1983+
ObjectMeta: metav1.ObjectMeta{
1984+
Name: "nc",
1985+
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue, bundleUnpackRefLabel: "test"},
1986+
},
1987+
Status: batchv1.JobStatus{
1988+
Conditions: nil,
1989+
},
1990+
}
19821991
failedJobs := []*batchv1.Job{
19831992
testJob("f-1", true, 1),
19841993
testJob("f-2", true, 2),
@@ -2010,6 +2019,24 @@ func TestSortUnpackJobs(t *testing.T) {
20102019
}, {
20112020
name: "empty job list",
20122021
maxRetained: 1,
2022+
}, {
2023+
name: "nil job in list",
2024+
maxRetained: 1,
2025+
jobs: []*batchv1.Job{
2026+
failedJobs[2],
2027+
nil,
2028+
failedJobs[1],
2029+
},
2030+
expectedLatest: failedJobs[2],
2031+
}, {
2032+
name: "nil condition",
2033+
maxRetained: 3,
2034+
jobs: []*batchv1.Job{
2035+
failedJobs[2],
2036+
nilConditionJob,
2037+
failedJobs[1],
2038+
},
2039+
expectedLatest: nilConditionJob,
20132040
}, {
20142041
name: "retain oldest",
20152042
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)