Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 3159a4a

Browse files
committed
#21 Use OwnerReferences and labels to find jobs
Names are not totally reliable to find the jobs anymore when we truncate them.
1 parent df71a34 commit 3159a4a

File tree

1 file changed

+54
-69
lines changed

1 file changed

+54
-69
lines changed

operator/controllers/execution/scan_controller.go

Lines changed: 54 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ func (r *ScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
116116
err = r.setHookStatus(&scan)
117117
case "ReadAndWriteHookProcessing":
118118
err = r.executeReadAndWriteHooks(&scan)
119-
120119
case "ReadAndWriteHookCompleted":
121120
err = r.startReadOnlyHooks(&scan)
122121
case "ReadOnlyHookProcessing":
@@ -129,21 +128,6 @@ func (r *ScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
129128
return ctrl.Result{}, nil
130129
}
131130

132-
func (r *ScanReconciler) getJob(name, namespace string) (*batch.Job, error) {
133-
ctx := context.Background()
134-
135-
var job batch.Job
136-
err := r.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, &job)
137-
if apierrors.IsNotFound(err) {
138-
return nil, nil
139-
} else if err != nil {
140-
r.Log.Error(err, "unable to get job")
141-
return nil, err
142-
}
143-
144-
return &job, nil
145-
}
146-
147131
type jobCompletionType string
148132

149133
const (
@@ -153,22 +137,51 @@ const (
153137
unknown jobCompletionType = "Unknown"
154138
)
155139

156-
func (r *ScanReconciler) checkIfJobIsCompleted(name, namespace string) (jobCompletionType, error) {
157-
job, err := r.getJob(name, namespace)
158-
if err != nil {
159-
return unknown, err
140+
func allJobsCompleted(jobs *batch.JobList) jobCompletionType {
141+
hasCompleted := true
142+
143+
for _, job := range jobs.Items {
144+
if job.Status.Failed > 0 {
145+
return failed
146+
} else if job.Status.Succeeded == 0 {
147+
hasCompleted = false
148+
}
160149
}
161-
if job == nil {
162-
return unknown, errors.New("Both Job and error were nil. This isn't really expected")
150+
151+
if hasCompleted {
152+
return completed
163153
}
154+
return incomplete
155+
}
156+
157+
func (r *ScanReconciler) getJobsForScan(scan *executionv1.Scan, labels client.MatchingLabels) (*batch.JobList, error) {
158+
ctx := context.Background()
164159

165-
if job.Status.Succeeded != 0 {
166-
return completed, nil
160+
// check if k8s job for scan was already created
161+
var jobs batch.JobList
162+
if err := r.List(
163+
ctx,
164+
&jobs,
165+
client.InNamespace(scan.Namespace),
166+
client.MatchingField(ownerKey, scan.Name),
167+
labels,
168+
); err != nil {
169+
r.Log.Error(err, "Unable to list child jobs")
170+
return nil, err
167171
}
168-
if job.Status.Failed != 0 {
169-
return failed, nil
172+
173+
return &jobs, nil
174+
}
175+
176+
func (r *ScanReconciler) checkIfJobIsCompleted(scan *executionv1.Scan, labels client.MatchingLabels) (jobCompletionType, error) {
177+
jobs, err := r.getJobsForScan(scan, labels)
178+
if err != nil {
179+
return unknown, err
170180
}
171-
return unknown, nil
181+
182+
r.Log.V(9).Info("Got related jobs", "count", len(jobs.Items))
183+
184+
return allJobsCompleted(jobs), nil
172185
}
173186

174187
// Helper functions to check and remove string from a slice of strings.
@@ -220,11 +233,11 @@ func (r *ScanReconciler) startScan(scan *executionv1.Scan) error {
220233
namespacedName := fmt.Sprintf("%s/%s", scan.Namespace, scan.Name)
221234
log := r.Log.WithValues("scan_init", namespacedName)
222235

223-
job, err := r.getJob(fmt.Sprintf("scan-%s", scan.Name), scan.Namespace)
236+
jobs, err := r.getJobsForScan(scan, client.MatchingLabels{"experimental.securecodebox.io/job-type": "scanner"})
224237
if err != nil {
225238
return err
226239
}
227-
if job != nil {
240+
if len(jobs.Items) > 0 {
228241
log.V(8).Info("Job already exists. Doesn't need to be created.")
229242
return nil
230243
}
@@ -267,7 +280,7 @@ func (r *ScanReconciler) startScan(scan *executionv1.Scan) error {
267280
rules,
268281
)
269282

270-
job, err = r.constructJobForScan(scan, &scanType)
283+
job, err := r.constructJobForScan(scan, &scanType)
271284
if err != nil {
272285
log.Error(err, "unable to create job object ScanType")
273286
return err
@@ -296,7 +309,7 @@ func (r *ScanReconciler) startScan(scan *executionv1.Scan) error {
296309
func (r *ScanReconciler) checkIfScanIsCompleted(scan *executionv1.Scan) error {
297310
ctx := context.Background()
298311

299-
status, err := r.checkIfJobIsCompleted(fmt.Sprintf("scan-%s", scan.Name), scan.Namespace)
312+
status, err := r.checkIfJobIsCompleted(scan, client.MatchingLabels{"experimental.securecodebox.io/job-type": "scanner"})
300313
if err != nil {
301314
return err
302315
}
@@ -326,11 +339,11 @@ func (r *ScanReconciler) startParser(scan *executionv1.Scan) error {
326339
namespacedName := fmt.Sprintf("%s/%s", scan.Namespace, scan.Name)
327340
log := r.Log.WithValues("scan_parse", namespacedName)
328341

329-
job, err := r.getJob(fmt.Sprintf("parse-%s", scan.Name), scan.Namespace)
342+
jobs, err := r.getJobsForScan(scan, client.MatchingLabels{"experimental.securecodebox.io/job-type": "parser"})
330343
if err != nil {
331344
return err
332345
}
333-
if job != nil {
346+
if len(jobs.Items) > 0 {
334347
log.V(8).Info("Job already exists. Doesn't need to be created.")
335348
return nil
336349
}
@@ -384,7 +397,7 @@ func (r *ScanReconciler) startParser(scan *executionv1.Scan) error {
384397
labels["experimental.securecodebox.io/job-type"] = "parser"
385398
automountServiceAccountToken := true
386399
var backOffLimit int32 = 3
387-
job = &batch.Job{
400+
job := &batch.Job{
388401
ObjectMeta: metav1.ObjectMeta{
389402
Annotations: make(map[string]string),
390403
Name: fmt.Sprintf("parse-%s", scan.Name),
@@ -459,7 +472,7 @@ func (r *ScanReconciler) startParser(scan *executionv1.Scan) error {
459472
func (r *ScanReconciler) checkIfParsingIsCompleted(scan *executionv1.Scan) error {
460473
ctx := context.Background()
461474

462-
status, err := r.checkIfJobIsCompleted(fmt.Sprintf("parse-%s", scan.Name), scan.Namespace)
475+
status, err := r.checkIfJobIsCompleted(scan, client.MatchingLabels{"experimental.securecodebox.io/job-type": "parser"})
463476
if err != nil {
464477
return err
465478
}
@@ -729,44 +742,13 @@ func (r *ScanReconciler) startReadOnlyHooks(scan *executionv1.Scan) error {
729742
return nil
730743
}
731744

732-
func allJobsCompleted(jobs *batch.JobList) jobCompletionType {
733-
hasCompleted := true
734-
735-
for _, job := range jobs.Items {
736-
if job.Status.Failed > 0 {
737-
return failed
738-
} else if job.Status.Succeeded == 0 {
739-
hasCompleted = false
740-
}
741-
}
742-
743-
if hasCompleted {
744-
return completed
745-
}
746-
return incomplete
747-
}
748-
749745
func (r *ScanReconciler) checkIfReadOnlyHookIsCompleted(scan *executionv1.Scan) error {
750746
ctx := context.Background()
751-
752-
// check if k8s job for scan was already created
753-
var readOnlyHookJobs batch.JobList
754-
if err := r.List(
755-
ctx,
756-
&readOnlyHookJobs,
757-
client.InNamespace(scan.Namespace),
758-
client.MatchingField(ownerKey, scan.Name),
759-
client.MatchingLabels{
760-
"experimental.securecodebox.io/job-type": "read-only-hook",
761-
},
762-
); err != nil {
763-
r.Log.Error(err, "Unable to list child jobs")
747+
readOnlyHookCompletion, err := r.checkIfJobIsCompleted(scan, client.MatchingLabels{"experimental.securecodebox.io/job-type": "read-only-hook"})
748+
if err != nil {
764749
return err
765750
}
766751

767-
r.Log.V(9).Info("Got related jobs", "count", len(readOnlyHookJobs.Items))
768-
769-
readOnlyHookCompletion := allJobsCompleted(&readOnlyHookJobs)
770752
if readOnlyHookCompletion == completed {
771753
r.Log.V(7).Info("All ReadOnlyHooks have completed")
772754
scan.Status.State = "Done"
@@ -1143,7 +1125,10 @@ func (r *ScanReconciler) executeReadAndWriteHooks(scan *executionv1.Scan) error
11431125
})
11441126
return err
11451127
case executionv1.InProgress:
1146-
jobStatus, err := r.checkIfJobIsCompleted(nonCompletedHook.JobName, scan.Namespace)
1128+
jobStatus, err := r.checkIfJobIsCompleted(scan, client.MatchingLabels{
1129+
"experimental.securecodebox.io/job-type": "read-and-write-hook",
1130+
"experimental.securecodebox.io/hook-name": nonCompletedHook.HookName,
1131+
})
11471132
if err != nil {
11481133
r.Log.Error(err, "Failed to check job status for ReadAndWrite Hook")
11491134
return err

0 commit comments

Comments
 (0)