@@ -170,15 +170,126 @@ func (r *ScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
170170 return ctrl.Result {}, nil
171171 }
172172
173+ if nonCompletedHook .State == executionv1 .Pending {
174+ rules := []rbacv1.PolicyRule {
175+ {
176+ APIGroups : []string {"execution.experimental.securecodebox.io" },
177+ Resources : []string {"scans" },
178+ Verbs : []string {"get" },
179+ },
180+ }
181+ serviceAccountName := "scan-completion-hook"
182+ r .ensureServiceAccountExists (
183+ scan .Namespace ,
184+ serviceAccountName ,
185+ "ScanCompletionHooks need to access the current scan to view where its results are stored" ,
186+ rules ,
187+ )
188+
189+ rawFileURL , err := r .PresignedGetURL (scan .UID , scan .Status .RawResultFile )
190+ if err != nil {
191+ return ctrl.Result {}, err
192+ }
193+ findingsFileURL , err := r .PresignedGetURL (scan .UID , "findings.json" )
194+ if err != nil {
195+ return ctrl.Result {}, err
196+ }
197+
198+ var hook executionv1.ScanCompletionHook
199+ err = r .Get (ctx , types.NamespacedName {Name : nonCompletedHook .HookName , Namespace : scan .Namespace }, & hook )
200+ if err != nil {
201+ r .Log .Error (err , "Failed to get ReadAndWrite Hook for HookStatus" )
202+ return ctrl.Result {}, err
203+ }
204+
205+ standardEnvVars := []corev1.EnvVar {
206+ {
207+ Name : "NAMESPACE" ,
208+ ValueFrom : & corev1.EnvVarSource {
209+ FieldRef : & corev1.ObjectFieldSelector {
210+ FieldPath : "metadata.namespace" ,
211+ },
212+ },
213+ },
214+ {
215+ Name : "SCAN_NAME" ,
216+ Value : scan .Name ,
217+ },
218+ }
219+
220+ // Starting a new job based on the current ReadAndWrite Hook
221+ labels := scan .ObjectMeta .DeepCopy ().Labels
222+ if labels == nil {
223+ labels = make (map [string ]string )
224+ }
225+ labels ["experimental.securecodebox.io/job-type" ] = "read-and-write-hook"
226+ job := & batch.Job {
227+ ObjectMeta : metav1.ObjectMeta {
228+ Annotations : make (map [string ]string ),
229+ Name : fmt .Sprintf ("%s-%s" , hook .Name , scan .Name ),
230+ Namespace : scan .Namespace ,
231+ Labels : labels ,
232+ },
233+ Spec : batch.JobSpec {
234+ Template : corev1.PodTemplateSpec {
235+ ObjectMeta : metav1.ObjectMeta {
236+ Annotations : map [string ]string {
237+ "auto-discovery.experimental.securecodebox.io/ignore" : "true" ,
238+ },
239+ },
240+ Spec : corev1.PodSpec {
241+ ServiceAccountName : serviceAccountName ,
242+ RestartPolicy : corev1 .RestartPolicyNever ,
243+ Containers : []corev1.Container {
244+ {
245+ Name : "hook" ,
246+ Image : hook .Spec .Image ,
247+ Args : []string {
248+ rawFileURL ,
249+ findingsFileURL ,
250+ },
251+ Env : append (hook .Spec .Env , standardEnvVars ... ),
252+ ImagePullPolicy : "IfNotPresent" ,
253+ },
254+ },
255+ },
256+ },
257+ TTLSecondsAfterFinished : nil ,
258+ },
259+ }
260+ if err := ctrl .SetControllerReference (& scan , job , r .Scheme ); err != nil {
261+ r .Log .Error (err , "Unable to set controllerReference on job" , "job" , job )
262+ return ctrl.Result {}, err
263+ }
264+
265+ if err := r .Create (ctx , job ); err != nil {
266+ r .Log .Error (err , "Unable to create Job for ReadOnlyHook" , "job" , job )
267+ return ctrl.Result {}, err
268+ }
269+
270+ for i , hookStatus := range scan .Status .ReadAndWriteHookStatus {
271+ if hookStatus .HookName == nonCompletedHook .HookName {
272+ scan .Status .ReadAndWriteHookStatus [i ].State = executionv1 .InProgress
273+ }
274+ }
275+
276+ if err := r .Status ().Update (ctx , & scan ); err != nil {
277+ r .Log .Error (err , "unable to update Scan status" )
278+ return ctrl.Result {}, err
279+ }
280+ return ctrl.Result {}, err
281+ }
282+
283+ // if nonCompletedHook.State == executionv1.InProgress{
284+
285+ // }
173286 // hook := First Array entry which is not Completed.
174287
175288 // if hook == "Pending" => create Job
176289 // if hook == "InProgress" =>
177290 // if job == "Completed" => hook = "Completed"
178291 // (if job == "Failed" => hook = "Failed" => scan = "Failed")
179292
180- // hook = nil => scan = "ReadAndWriteHookCompleted"
181-
182293 // Scan Status auf ReadAndWriteHookCompleted setzen
183294 case "ReadAndWriteHookCompleted" :
184295 err = r .startReadOnlyHooks (& scan )
0 commit comments