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

Commit 8f725c6

Browse files
committed
Extract scan status update logic
1 parent 584e956 commit 8f725c6

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

operator/controllers/execution/scan_controller.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,22 @@ func (r *ScanReconciler) createJobForHook(hook *executionv1.ScanCompletionHook,
10531053
return job.Name, nil
10541054
}
10551055

1056+
func (r *ScanReconciler) updateHookStatus(scan *executionv1.Scan, hookStatus executionv1.HookStatus) error {
1057+
for i, hook := range scan.Status.ReadAndWriteHookStatus {
1058+
if hook.HookName == hookStatus.HookName {
1059+
scan.Status.ReadAndWriteHookStatus[i] = hookStatus
1060+
break
1061+
}
1062+
}
1063+
if err := r.Status().Update(context.Background(), scan); err != nil {
1064+
r.Log.Error(err, "unable to update Scan status")
1065+
return err
1066+
}
1067+
return nil
1068+
}
1069+
10561070
func (r *ScanReconciler) executeReadAndWriteHooks(scan *executionv1.Scan) error {
1057-
// First Array entry which is not Completed.
1071+
// Get the first Hook Status which is not completed.
10581072
ctx := context.Background()
10591073
var nonCompletedHook *executionv1.HookStatus
10601074

@@ -1065,6 +1079,7 @@ func (r *ScanReconciler) executeReadAndWriteHooks(scan *executionv1.Scan) error
10651079
}
10661080
}
10671081

1082+
// If nil then all hooks are done
10681083
if nonCompletedHook == nil {
10691084
scan.Status.State = "ReadAndWriteHookCompleted"
10701085
if err := r.Status().Update(ctx, scan); err != nil {
@@ -1111,18 +1126,13 @@ func (r *ScanReconciler) executeReadAndWriteHooks(scan *executionv1.Scan) error
11111126
},
11121127
)
11131128

1114-
for i, hookStatus := range scan.Status.ReadAndWriteHookStatus {
1115-
if hookStatus.HookName == nonCompletedHook.HookName {
1116-
scan.Status.ReadAndWriteHookStatus[i].JobName = jobName
1117-
scan.Status.ReadAndWriteHookStatus[i].State = executionv1.InProgress
1118-
}
1119-
}
1120-
1121-
if err := r.Status().Update(ctx, scan); err != nil {
1122-
r.Log.Error(err, "unable to update Scan status")
1123-
return err
1124-
}
1125-
return nil
1129+
// Update the currently executed hook status to "InProgress"
1130+
err = r.updateHookStatus(scan, executionv1.HookStatus{
1131+
HookName: nonCompletedHook.HookName,
1132+
JobName: jobName,
1133+
State: executionv1.InProgress,
1134+
})
1135+
return err
11261136
case executionv1.InProgress:
11271137
jobStatus, err := r.checkIfJobIsCompleted(nonCompletedHook.JobName, scan.Namespace)
11281138
if err != nil {
@@ -1131,17 +1141,13 @@ func (r *ScanReconciler) executeReadAndWriteHooks(scan *executionv1.Scan) error
11311141
}
11321142
switch jobStatus {
11331143
case completed:
1134-
for i, hookStatus := range scan.Status.ReadAndWriteHookStatus {
1135-
if hookStatus.HookName == nonCompletedHook.HookName {
1136-
scan.Status.ReadAndWriteHookStatus[i].State = executionv1.Completed
1137-
}
1138-
}
1139-
1140-
if err := r.Status().Update(ctx, scan); err != nil {
1141-
r.Log.Error(err, "unable to update Scan status")
1142-
return err
1143-
}
1144-
return nil
1144+
// Job is completed => set current Hook to completed
1145+
err = r.updateHookStatus(scan, executionv1.HookStatus{
1146+
HookName: nonCompletedHook.HookName,
1147+
JobName: nonCompletedHook.JobName,
1148+
State: executionv1.Completed,
1149+
})
1150+
return err
11451151
case incomplete:
11461152
// Still waiting for job to finish
11471153
return nil

0 commit comments

Comments
 (0)