Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion operator/controllers/execution/scans/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func checkIfAllJobsCompleted(jobs *batch.JobList) jobCompletionType {
hasCompleted := true

for _, job := range jobs.Items {
if job.Status.Failed >= *job.Spec.BackoffLimit {
// Check if the job has failed and the reason is "BackoffLimitExceeded"
Comment thread
Ilyesbdlala marked this conversation as resolved.
Resolved
if isBackoffLimitExceeded(job) {
return failed
} else if job.Status.Succeeded == 0 {
hasCompleted = false
Expand Down Expand Up @@ -110,3 +111,15 @@ func injectCustomCACertsIfConfigured(job *batch.Job) {
Value: mountPath,
})
}

// isBackoffLimitExceeded checks if a job has failed due to exceeding the backoff limit
func isBackoffLimitExceeded(job batch.Job) bool {
if job.Status.Failed > 0 {
for _, condition := range job.Status.Conditions {
if condition.Reason == "BackoffLimitExceeded" {
return true
}
}
}
return false
}
108 changes: 108 additions & 0 deletions operator/controllers/execution/scans/job_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0

package scancontrollers

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
batch "k8s.io/api/batch/v1"
)

var _ = Describe("ScanControllers", func() {
Context("checkIfAllJobsCompleted", func() {
It("should return completed if all jobs succeeded", func() {
jobs := &batch.JobList{
Items: []batch.Job{
{
Status: batch.JobStatus{
Succeeded: 1,
},
},
{
Status: batch.JobStatus{
Succeeded: 1,
},
},
},
}
Expect(checkIfAllJobsCompleted(jobs)).To(Equal(completed))
})

It("should return incomplete if any job did not succeed", func() {
jobs := &batch.JobList{
Items: []batch.Job{
{
Status: batch.JobStatus{
Succeeded: 1,
},
},
{
Status: batch.JobStatus{
Succeeded: 0,
},
},
},
}
Expect(checkIfAllJobsCompleted(jobs)).To(Equal(incomplete))
})

It("should return failed if any job exceeded backoff limit", func() {
jobs := &batch.JobList{
Items: []batch.Job{
{
Status: batch.JobStatus{
Failed: 1,
Conditions: []batch.JobCondition{
{
Reason: "BackoffLimitExceeded",
},
},
},
},
},
}
Expect(checkIfAllJobsCompleted(jobs)).To(Equal(failed))
})
})

Context("isBackoffLimitExceeded", func() {
It("should return true if job exceeded backoff limit", func() {
job := batch.Job{
Status: batch.JobStatus{
Failed: 1,
Conditions: []batch.JobCondition{
{
Reason: "BackoffLimitExceeded",
},
},
},
}
Expect(isBackoffLimitExceeded(job)).To(BeTrue())
})

It("should return false if job did not exceed backoff limit", func() {
job := batch.Job{
Status: batch.JobStatus{
Failed: 1,
Conditions: []batch.JobCondition{
{
Reason: "SomeOtherReason",
},
},
},
}
Expect(isBackoffLimitExceeded(job)).To(BeFalse())
})

It("should return false if job did not fail", func() {
job := batch.Job{
Status: batch.JobStatus{
Failed: 0,
},
}
Expect(isBackoffLimitExceeded(job)).To(BeFalse())
})
})
})