@@ -34,6 +34,21 @@ type ScheduledScanReconciler struct {
3434 client.Client
3535 Log logr.Logger
3636 Scheme * runtime.Scheme
37+ Clock
38+ }
39+
40+ /*
41+ We'll mock out the clock to make it easier to jump around in time while testing,
42+ the "real" clock just calls `time.Now`.
43+ */
44+ type realClock struct {}
45+
46+ func (_ realClock ) Now () time.Time { return time .Now () }
47+
48+ // clock knows how to get the current time.
49+ // It can be used to fake out timing for testing.
50+ type Clock interface {
51+ Now () time.Time
3752}
3853
3954// +kubebuilder:rbac:groups=execution.securecodebox.io,resources=scheduledscans,verbs=get;list;watch;create;update;patch;delete
@@ -101,14 +116,14 @@ func (r *ScheduledScanReconciler) Reconcile(ctx context.Context, req ctrl.Reques
101116 }
102117
103118 // Calculate the next schedule
104- nextSchedule , err := getNextSchedule (scheduledScan , time .Now ())
119+ nextSchedule , err := getNextSchedule (r , scheduledScan , r . Clock .Now ())
105120 if err != nil {
106121 log .Error (err , "Unable to calculate next schedule" )
107122 return ctrl.Result {}, err
108123 }
109124
110125 // check if it is time to start the next Scan
111- if ! time .Now ().Before (nextSchedule ) {
126+ if ! r . Clock .Now ().Before (nextSchedule ) {
112127 if scheduledScan .Spec .RetriggerOnScanTypeChange == true {
113128 // generate hash for current state of the configured ScanType
114129 var scanType executionv1.ScanType
@@ -156,13 +171,13 @@ func (r *ScheduledScanReconciler) Reconcile(ctx context.Context, req ctrl.Reques
156171 }
157172
158173 // Recalculate next schedule
159- nextSchedule = time .Now ().Add (scheduledScan .Spec .Interval .Duration )
174+ nextSchedule = r . Clock .Now ().Add (scheduledScan .Spec .Interval .Duration )
160175 }
161176
162- return ctrl.Result {RequeueAfter : nextSchedule .Sub (time .Now ())}, nil
177+ return ctrl.Result {RequeueAfter : nextSchedule .Sub (r . Clock .Now ())}, nil
163178}
164179
165- func getNextSchedule (scheduledScan executionv1.ScheduledScan , now time.Time ) (next time.Time , err error ) {
180+ func getNextSchedule (r * ScheduledScanReconciler , scheduledScan executionv1.ScheduledScan , now time.Time ) (next time.Time , err error ) {
166181 // check if the Cron schedule is set
167182 if scheduledScan .Spec .Schedule != "" {
168183 sched , err := cron .ParseStandard (scheduledScan .Spec .Schedule )
@@ -189,7 +204,7 @@ func getNextSchedule(scheduledScan executionv1.ScheduledScan, now time.Time) (ne
189204 if scheduledScan .Status .LastScheduleTime != nil {
190205 nextSchedule = scheduledScan .Status .LastScheduleTime .Add (scheduledScan .Spec .Interval .Duration )
191206 } else {
192- nextSchedule = time .Now ().Add (- 1 * time .Second )
207+ nextSchedule = r . Clock .Now ().Add (- 1 * time .Second )
193208 }
194209 return nextSchedule , nil
195210 }
@@ -246,6 +261,10 @@ func (r *ScheduledScanReconciler) deleteOldScans(scans []executionv1.Scan, maxCo
246261
247262// SetupWithManager sets up the controller and initializes every thing it needs
248263func (r * ScheduledScanReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
264+ // set up a real clock, since we're not in a test
265+ if r .Clock == nil {
266+ r .Clock = realClock {}
267+ }
249268 ctx := context .Background ()
250269 if err := mgr .GetFieldIndexer ().IndexField (ctx , & executionv1.Scan {}, ownerKey , func (rawObj client.Object ) []string {
251270 // grab the job object, extract the owner...
0 commit comments