Skip to content

Commit

Permalink
fix: better job condition logic (support K8s 1.31) (#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgb authored Sep 4, 2024
1 parent ca91ee9 commit 85241de
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
10 changes: 5 additions & 5 deletions internal/controller/stas/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func (r *Indexer) SetupWithManager(mgr ctrl.Manager) error {
// TODO: non-exact field matches are not supported by the cache
// https://github.com/kubernetes-sigs/controller-runtime/blob/main/pkg/cache/internal/cache_reader.go#L116-L121
// So mapping to [Complete, Failed, NotFinished] where the last is a composite condition
switch jc := jobCondition(job); jc {
case batchv1.JobComplete:
return []string{string(jc)}
case batchv1.JobFailed:
return []string{string(jc)}
switch {
case isJobComplete(job):
return []string{string(batchv1.JobComplete)}
case isJobFailed(job):
return []string{string(batchv1.JobFailed)}
default:
return []string{jobNotFinished}
}
Expand Down
26 changes: 11 additions & 15 deletions internal/controller/stas/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stas

import (
"regexp"
"slices"

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -102,25 +103,20 @@ var managedByImageScanner = predicate.NewPredicateFuncs(func(object client.Objec
})

var jobIsFinished = predicate.NewPredicateFuncs(func(object client.Object) bool {
return isJobFinished(object.(*batchv1.Job))
job := object.(*batchv1.Job)
return isJobComplete(job) || isJobFailed(job)
})

// isJobFinished checks whether the given Job has finished execution.
// It does not discriminate between successful and failed terminations.
// https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/job/utils.go#L24-L33
func isJobFinished(j *batchv1.Job) bool {
c := jobCondition(j)
return c == batchv1.JobComplete || c == batchv1.JobFailed
func isJobComplete(j *batchv1.Job) bool {
return slices.ContainsFunc(j.Status.Conditions, func(condition batchv1.JobCondition) bool {
return condition.Type == batchv1.JobComplete && condition.Status == corev1.ConditionTrue
})
}

func jobCondition(j *batchv1.Job) batchv1.JobConditionType {
for _, c := range j.Status.Conditions {
if c.Status == corev1.ConditionTrue {
return c.Type
}
}

return ""
func isJobFailed(j *batchv1.Job) bool {
return slices.ContainsFunc(j.Status.Conditions, func(condition batchv1.JobCondition) bool {
return condition.Type == batchv1.JobFailed && condition.Status == corev1.ConditionTrue
})
}

func eventRegardingKind(kind string) predicate.Predicate {
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/stas/scan_job_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,15 @@ func (r *ScanJobReconciler) reconcileJob(ctx context.Context, job *batchv1.Job)
}
}(logs)

switch jc := jobCondition(job); jc {
case batchv1.JobComplete:
logf.FromContext(ctx).V(1).Info("Patching CIS status", "jobCondition", jc)
switch {
case isJobComplete(job):
logf.FromContext(ctx).V(1).Info("Patching CIS status", "jobCondition", batchv1.JobComplete)
return r.reconcileCompleteJob(ctx, job, logs, cis)
case batchv1.JobFailed:
logf.FromContext(ctx).V(1).Info("Patching CIS status", "jobCondition", jc)
case isJobFailed(job):
logf.FromContext(ctx).V(1).Info("Patching CIS status", "jobCondition", batchv1.JobFailed)
return r.reconcileFailedJob(ctx, job, logs, cis)
default:
return fmt.Errorf("I don't know how to handle job status %q", jc)
return fmt.Errorf("I don't know how to handle job status %q", job.Status.String())
}
}

Expand Down

0 comments on commit 85241de

Please sign in to comment.