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

Commit 6f2a3d8

Browse files
committed
Check if Job for Parser exists before creation & refactoring
1 parent 9edbe82 commit 6f2a3d8

File tree

1 file changed

+36
-50
lines changed

1 file changed

+36
-50
lines changed

operator/controllers/scan_controller.go

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,36 @@ func (r *ScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
124124
return ctrl.Result{}, nil
125125
}
126126

127-
func (r *ScanReconciler) getScanJob(scan *scansv1.Scan) (*batch.Job, error) {
127+
func (r *ScanReconciler) getJob(name, namespace string) (*batch.Job, error) {
128128
ctx := context.Background()
129-
namespacedName := fmt.Sprintf("%s/%s", scan.Namespace, scan.Name)
130-
log := r.Log.WithValues("scan_done_check", namespacedName)
131129

132130
// check if k8s job for scan was already created
133131
var job batch.Job
134-
err := r.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("scan-%s", scan.Name), Namespace: scan.Namespace}, &job)
132+
err := r.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, &job)
135133
if apierrors.IsNotFound(err) {
136134
return nil, nil
137135
} else if err != nil {
138-
log.Error(err, "unable to get child Pod")
136+
r.Log.Error(err, "unable to get job")
139137
return nil, err
140138
}
141139

142140
return &job, nil
143141
}
144142

143+
func (r *ScanReconciler) checkIfJobIsCompleted(name, namespace string) (bool, error) {
144+
job, err := r.getJob(name, namespace)
145+
if err != nil {
146+
return false, err
147+
}
148+
return (job.Status.Succeeded != 0), nil
149+
}
150+
145151
func (r *ScanReconciler) startScan(scan *scansv1.Scan) error {
146152
ctx := context.Background()
147153
namespacedName := fmt.Sprintf("%s/%s", scan.Namespace, scan.Name)
148154
log := r.Log.WithValues("scan_init", namespacedName)
149155

150-
job, err := r.getScanJob(scan)
156+
job, err := r.getJob(fmt.Sprintf("scan-%s", scan.Name), scan.Namespace)
151157
if err != nil {
152158
return err
153159
}
@@ -209,16 +215,17 @@ func (r *ScanReconciler) startScan(scan *scansv1.Scan) error {
209215
return nil
210216
}
211217

218+
// Checking if scan has completed
212219
func (r *ScanReconciler) checkIfScanIsCompleted(scan *scansv1.Scan) error {
213-
job, err := r.getScanJob(scan)
220+
ctx := context.Background()
221+
222+
done, err := r.checkIfJobIsCompleted(fmt.Sprintf("scan-%s", scan.Name), scan.Namespace)
214223
if err != nil {
215224
return err
216225
}
217-
ctx := context.Background()
218226

219-
// Checking if scan has completed
220-
// TODO: Handle scan job failure cases
221-
if job.Status.Succeeded != 0 {
227+
// TODO: Handle job failure cases
228+
if done {
222229
r.Log.V(7).Info("Scan is completed")
223230
scan.Status.State = "ScanCompleted"
224231
if err := r.Status().Update(ctx, scan); err != nil {
@@ -234,6 +241,15 @@ func (r *ScanReconciler) startParser(scan *scansv1.Scan) error {
234241
namespacedName := fmt.Sprintf("%s/%s", scan.Namespace, scan.Name)
235242
log := r.Log.WithValues("scan_parse", namespacedName)
236243

244+
job, err := r.getJob(fmt.Sprintf("parse-%s", scan.Name), scan.Namespace)
245+
if err != nil {
246+
return err
247+
}
248+
if job != nil {
249+
log.V(8).Info("Job already exists. Doesn't need to be created.")
250+
return nil
251+
}
252+
237253
parseType := scan.Status.RawResultType
238254

239255
// get the scan template for the scan
@@ -270,7 +286,7 @@ func (r *ScanReconciler) startParser(scan *scansv1.Scan) error {
270286
)
271287

272288
automountServiceAccountToken := true
273-
job := &batch.Job{
289+
job = &batch.Job{
274290
ObjectMeta: metav1.ObjectMeta{
275291
Annotations: make(map[string]string),
276292
Name: fmt.Sprintf("parse-%s", scan.Name),
@@ -336,55 +352,25 @@ func (r *ScanReconciler) startParser(scan *scansv1.Scan) error {
336352
return nil
337353
}
338354

355+
// Checking if Parser has completed
339356
func (r *ScanReconciler) checkIfParsingIsCompleted(scan *scansv1.Scan) error {
340357
ctx := context.Background()
341-
namespacedName := fmt.Sprintf("%s/%s", scan.Namespace, scan.Name)
342-
log := r.Log.WithValues("scan_done_check", namespacedName)
343358

344-
// check if k8s job for scan was already created
345-
var childJobs batch.JobList
346-
if err := r.List(
347-
ctx,
348-
&childJobs,
349-
client.InNamespace(scan.Namespace),
350-
client.MatchingField(ownerKey, scan.Name),
351-
client.MatchingLabels{
352-
"experimental.securecodebox.io/job-type": "parser",
353-
},
354-
); err != nil {
355-
log.Error(err, "unable to list child jobs")
359+
done, err := r.checkIfJobIsCompleted(fmt.Sprintf("parse-%s", scan.Name), scan.Namespace)
360+
if err != nil {
356361
return err
357362
}
358363

359-
log.V(9).Info("Got related jobs", "count", len(childJobs.Items))
360-
361-
if len(childJobs.Items) == 0 {
362-
// Unexpected. Job should exist in "Parsing" State. Resetting to Init
363-
log.Info("Scan is in Parsing State but doesn't have a associated Parse Job running. Resetting status to 'ScanCompleted'")
364-
scan.Status.State = "ScanCompleted"
365-
if err := r.Status().Update(ctx, scan); err != nil {
366-
log.Error(err, "unable to update Scan status")
367-
return err
368-
}
369-
return nil
370-
} else if len(childJobs.Items) > 1 {
371-
// yoo that wasn't expected
372-
return errors.New("Scan had more than one parse job. Thats not expected")
373-
}
374-
375-
// Job exists as expected
376-
job := childJobs.Items[0]
377-
378-
// Checking if parsing has completed
379-
// TODO: Handle parse job failure cases
380-
if job.Status.Succeeded != 0 {
381-
log.V(7).Info("Parsing is completed")
364+
// TODO: Handle job failure cases
365+
if done {
366+
r.Log.V(7).Info("Scan is completed")
382367
scan.Status.State = "ParseCompleted"
383368
if err := r.Status().Update(ctx, scan); err != nil {
384-
log.Error(err, "unable to update Scan status")
369+
r.Log.Error(err, "unable to update Scan status")
385370
return err
386371
}
387372
}
373+
388374
return nil
389375
}
390376

0 commit comments

Comments
 (0)