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

Commit f7be4f0

Browse files
committed
Check if job has completed
1 parent cf5eb78 commit f7be4f0

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

operator/apis/execution/v1/scan_types.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ const (
5656
Pending HookState = "Pending"
5757
InProgress HookState = "InProgress"
5858
Completed HookState = "Completed"
59-
60-
// Cancelled HookState = "Cancelled"
61-
// Failed HookState = "Failed"
59+
Cancelled HookState = "Cancelled"
60+
Failed HookState = "Failed"
6261
)
6362

6463
type HookStatus struct {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apiVersion: execution.experimental.securecodebox.io/v1
22
kind: ScanCompletionHook
33
metadata:
4-
name: "echo-rw"
4+
name: "sleep-rw"
55
spec:
6-
image: "echo"
6+
image: "sleep"
77
type: "ReadAndWrite"
88
env: []
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apiVersion: execution.experimental.securecodebox.io/v1
22
kind: ScanCompletionHook
33
metadata:
4-
name: "sleep"
4+
name: "echo-r"
55
spec:
6-
image: "sleep"
6+
image: "echo"
77
type: "ReadOnly"
88
env: []

operator/controllers/execution/scan_controller.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ func (r *ScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
223223
labels = make(map[string]string)
224224
}
225225
labels["experimental.securecodebox.io/job-type"] = "read-and-write-hook"
226+
var backOffLimit int32 = 3
226227
job := &batch.Job{
227228
ObjectMeta: metav1.ObjectMeta{
228229
Annotations: make(map[string]string),
@@ -231,6 +232,7 @@ func (r *ScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
231232
Labels: labels,
232233
},
233234
Spec: batch.JobSpec{
235+
BackoffLimit: &backOffLimit,
234236
Template: corev1.PodTemplateSpec{
235237
ObjectMeta: metav1.ObjectMeta{
236238
Annotations: map[string]string{
@@ -269,6 +271,7 @@ func (r *ScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
269271

270272
for i, hookStatus := range scan.Status.ReadAndWriteHookStatus {
271273
if hookStatus.HookName == nonCompletedHook.HookName {
274+
scan.Status.ReadAndWriteHookStatus[i].JobName = job.Name
272275
scan.Status.ReadAndWriteHookStatus[i].State = executionv1.InProgress
273276
}
274277
}
@@ -280,9 +283,46 @@ func (r *ScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
280283
return ctrl.Result{}, err
281284
}
282285

283-
// if nonCompletedHook.State == executionv1.InProgress{
286+
if nonCompletedHook.State == executionv1.InProgress {
287+
jobStatus, err := r.checkIfJobIsCompleted(nonCompletedHook.JobName, scan.Namespace)
288+
if err != nil {
289+
r.Log.Error(err, "Failed to check job status for ReadAndWrite Hook")
290+
return ctrl.Result{}, err
291+
}
292+
switch jobStatus {
293+
case completed:
294+
for i, hookStatus := range scan.Status.ReadAndWriteHookStatus {
295+
if hookStatus.HookName == nonCompletedHook.HookName {
296+
scan.Status.ReadAndWriteHookStatus[i].State = executionv1.Completed
297+
}
298+
}
299+
300+
if err := r.Status().Update(ctx, &scan); err != nil {
301+
r.Log.Error(err, "unable to update Scan status")
302+
return ctrl.Result{}, err
303+
}
304+
return ctrl.Result{}, err
305+
case incomplete:
306+
// Still waiting for job to finish
307+
return ctrl.Result{}, err
308+
309+
case failed:
310+
for i, hookStatus := range scan.Status.ReadAndWriteHookStatus {
311+
if hookStatus.HookName == nonCompletedHook.HookName {
312+
scan.Status.ReadAndWriteHookStatus[i].State = executionv1.Failed
313+
} else if hookStatus.State == executionv1.Pending {
314+
scan.Status.ReadAndWriteHookStatus[i].State = executionv1.Cancelled
315+
}
316+
}
317+
scan.Status.State = "Errored"
318+
scan.Status.ErrorDescription = fmt.Sprintf("Failed to execute ReadAndWrite Hook '%s' in job '%s'. Check the logs of the hook for more information.", nonCompletedHook.HookName, nonCompletedHook.JobName)
319+
if err := r.Status().Update(ctx, &scan); err != nil {
320+
r.Log.Error(err, "unable to update Scan status")
321+
return ctrl.Result{}, err
322+
}
323+
}
324+
}
284325

285-
// }
286326
// hook := First Array entry which is not Completed.
287327

288328
// if hook == "Pending" => create Job

0 commit comments

Comments
 (0)