@@ -124,11 +124,38 @@ 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 ) {
128+ ctx := context .Background ()
129+ namespacedName := fmt .Sprintf ("%s/%s" , scan .Namespace , scan .Name )
130+ log := r .Log .WithValues ("scan_done_check" , namespacedName )
131+
132+ // check if k8s job for scan was already created
133+ var job batch.Job
134+ err := r .Get (ctx , types.NamespacedName {Name : fmt .Sprintf ("scan-%s" , scan .Name ), Namespace : scan .Namespace }, & job )
135+ if apierrors .IsNotFound (err ) {
136+ return nil , nil
137+ } else if err != nil {
138+ log .Error (err , "unable to get child Pod" )
139+ return nil , err
140+ }
141+
142+ return & job , nil
143+ }
144+
127145func (r * ScanReconciler ) startScan (scan * scansv1.Scan ) error {
128146 ctx := context .Background ()
129147 namespacedName := fmt .Sprintf ("%s/%s" , scan .Namespace , scan .Name )
130148 log := r .Log .WithValues ("scan_init" , namespacedName )
131149
150+ job , err := r .getScanJob (scan )
151+ if err != nil {
152+ return err
153+ }
154+ if job != nil {
155+ log .V (8 ).Info ("Job already exists. Doesn't need to be created." )
156+ return nil
157+ }
158+
132159 // get the scan template for the scan
133160 var scanTemplate scansv1.ScanTemplate
134161 if err := r .Get (ctx , types.NamespacedName {Name : scan .Spec .ScanType , Namespace : scan .Namespace }, & scanTemplate ); err != nil {
@@ -154,7 +181,7 @@ func (r *ScanReconciler) startScan(scan *scansv1.Scan) error {
154181 rules ,
155182 )
156183
157- job , err : = r .constructJobForScan (scan , & scanTemplate )
184+ job , err = r .constructJobForScan (scan , & scanTemplate )
158185 if err != nil {
159186 log .Error (err , "unable to create job object ScanTemplate" )
160187 // we'll ignore not-found errors, since they can't be fixed by an immediate
@@ -183,52 +210,19 @@ func (r *ScanReconciler) startScan(scan *scansv1.Scan) error {
183210}
184211
185212func (r * ScanReconciler ) checkIfScanIsCompleted (scan * scansv1.Scan ) error {
186- ctx := context .Background ()
187- namespacedName := fmt .Sprintf ("%s/%s" , scan .Namespace , scan .Name )
188- log := r .Log .WithValues ("scan_done_check" , namespacedName )
189-
190- // check if k8s job for scan was already created
191- var childJobs batch.JobList
192- if err := r .List (
193- ctx ,
194- & childJobs ,
195- client .InNamespace (scan .Namespace ),
196- client .MatchingField (ownerKey , scan .Name ),
197- client.MatchingLabels {
198- "experimental.securecodebox.io/job-type" : "scanner" ,
199- },
200- ); err != nil {
201- log .Error (err , "unable to list child Pods" )
213+ job , err := r .getScanJob (scan )
214+ if err != nil {
202215 return err
203216 }
204-
205- // TODO: What if the Pod doesn't match our spec? Recreate?
206-
207- log .V (9 ).Info ("Got related jobs" , "count" , len (childJobs .Items ))
208-
209- if len (childJobs .Items ) == 0 {
210- // Unexpected. Job should exisit in Scanning State. Resetting to Init
211- scan .Status .State = "Init"
212- if err := r .Status ().Update (ctx , scan ); err != nil {
213- log .Error (err , "unable to update Scan status" )
214- return err
215- }
216- return nil
217- } else if len (childJobs .Items ) > 1 {
218- // yoo that wasn't expected
219- return errors .New ("Scan had more than one job. Thats not expected" )
220- }
221-
222- // Job exists as expected
223- job := childJobs .Items [0 ]
217+ ctx := context .Background ()
224218
225219 // Checking if scan has completed
226220 // TODO: Handle scan job failure cases
227221 if job .Status .Succeeded != 0 {
228- log .V (7 ).Info ("Scan is completed" )
222+ r . Log .V (7 ).Info ("Scan is completed" )
229223 scan .Status .State = "ScanCompleted"
230224 if err := r .Status ().Update (ctx , scan ); err != nil {
231- log .Error (err , "unable to update Scan status" )
225+ r . Log .Error (err , "unable to update Scan status" )
232226 return err
233227 }
234228 }
0 commit comments