forked from aquasecurity/starboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_controller.go
131 lines (107 loc) · 3.84 KB
/
job_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package job
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/aquasecurity/starboard/pkg/operator/controller"
"github.com/aquasecurity/starboard/pkg/kube"
pods "github.com/aquasecurity/starboard/pkg/kube/pod"
"github.com/aquasecurity/starboard/pkg/operator/etc"
"github.com/aquasecurity/starboard/pkg/resources"
"k8s.io/apimachinery/pkg/api/errors"
batchv1 "k8s.io/api/batch/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
log = ctrl.Log.WithName("controller").WithName("job")
)
type JobController struct {
etc.Operator
client.Client
controller.Analyzer
controller.Reconciler
}
func (r *JobController) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := log.WithValues("job", req.NamespacedName)
if req.Namespace != r.Namespace {
log.V(1).Info("Ignoring Job not managed by this operator")
return ctrl.Result{}, nil
}
job := &batchv1.Job{}
err := r.Client.Get(ctx, req.NamespacedName, job)
if err != nil {
if errors.IsNotFound(err) {
log.V(1).Info("Ignoring Job that must have been deleted")
return ctrl.Result{}, nil
}
return ctrl.Result{}, fmt.Errorf("getting job from cache: %w", err)
}
if len(job.Status.Conditions) == 0 {
log.V(1).Info("Ignoring Job without status conditions")
return ctrl.Result{}, nil
}
switch jobCondition := job.Status.Conditions[0].Type; jobCondition {
case batchv1.JobComplete:
err = r.processCompleteScanJob(ctx, job)
case batchv1.JobFailed:
err = r.processFailedScanJob(ctx, job)
default:
err = fmt.Errorf("unrecognized scan job condition: %v", jobCondition)
}
return ctrl.Result{}, err
}
func (r *JobController) processCompleteScanJob(ctx context.Context, scanJob *batchv1.Job) error {
log := log.WithValues("job", fmt.Sprintf("%s/%s", scanJob.Namespace, scanJob.Name))
owner, err := kube.ObjectFromLabelsSet(scanJob.Labels)
if err != nil {
return fmt.Errorf("getting workload from scan job labels set: %w", err)
}
containerImages, err := resources.GetContainerImagesFromJob(scanJob)
if err != nil {
return fmt.Errorf("getting container images: %w", err)
}
hash, ok := scanJob.Labels[kube.LabelPodSpecHash]
if !ok {
return fmt.Errorf("expected label %s not set", kube.LabelPodSpecHash)
}
log.V(1).Info("Resolving workload properties",
"owner", owner, "hash", hash, "containerImages", containerImages)
hasVulnerabilityReports, err := r.HasVulnerabilityReports(ctx, owner, containerImages, hash)
if err != nil {
return err
}
if hasVulnerabilityReports {
log.V(1).Info("VulnerabilityReports already exist", "owner", owner)
log.V(1).Info("Deleting scan job")
return r.Client.Delete(ctx, scanJob, client.PropagationPolicy(metav1.DeletePropagationBackground))
}
err = r.ParseLogsAndSaveVulnerabilityReports(ctx, scanJob, owner, containerImages, hash)
if err != nil {
return err
}
log.V(1).Info("Deleting complete scan job")
return r.Client.Delete(ctx, scanJob, client.PropagationPolicy(metav1.DeletePropagationBackground))
}
func (r *JobController) processFailedScanJob(ctx context.Context, scanJob *batchv1.Job) error {
log := log.WithValues("job", fmt.Sprintf("%s/%s", scanJob.Namespace, scanJob.Name))
pod, err := r.Reconciler.GetPodControlledBy(ctx, scanJob)
if err != nil {
return err
}
statuses := pods.GetTerminatedContainersStatusesByPod(pod)
for container, status := range statuses {
if status.ExitCode == 0 {
continue
}
log.Error(nil, "Scan job container", "container", container, "status.reason", status.Reason, "status.message", status.Message)
}
log.V(1).Info("Deleting failed scan job")
return r.Client.Delete(ctx, scanJob, client.PropagationPolicy(metav1.DeletePropagationBackground))
}
func (r *JobController) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&batchv1.Job{}).
Complete(r)
}