Skip to content

Commit ef090b9

Browse files
committed
Second code review changes
Signed-off-by: Xavi Garcia <xavi.garcia@suse.com>
1 parent 9f0be4c commit ef090b9

9 files changed

Lines changed: 33 additions & 49 deletions

File tree

charts/fleet/ci/debug-values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ controller:
5151
gitrepo: "1"
5252
bundle: "1"
5353
bundledeployment: "1"
54+
schedule: "1"
5455

5556
shards:
5657
- id: shard0

charts/fleet/ci/nobootstrap-values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ controller:
5050
gitrepo: "1"
5151
bundle: "1"
5252
bundledeployment: "1"
53+
schedule: "1"
5354

5455
shards:
5556
- id: shard0

charts/fleet/ci/nodebug-values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ controller:
5050
gitrepo: "1"
5151
bundle: "1"
5252
bundledeployment: "1"
53+
schedule: "1"
5354

5455
shards:
5556
- id: shard0

charts/fleet/ci/nogitops-values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ controller:
5050
gitrepo: "1"
5151
bundle: "1"
5252
bundledeployment: "1"
53+
schedule: "1"
5354

5455
shards:
5556
- id: shard0

charts/fleet/ci/nohelmops-values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ controller:
5050
gitrepo: "1"
5151
bundle: "1"
5252
bundledeployment: "1"
53+
schedule: "1"
5354

5455
shards:
5556
- id: shard0

charts/fleet/templates/deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ spec:
8989
- name: IMAGESCAN_RECONCILER_WORKERS
9090
value: {{ quote $.Values.controller.reconciler.workers.imagescan }}
9191
{{- end }}
92+
{{- if $.Values.controller.reconciler.workers.schedule }}
93+
- name: SCHEDULE_RECONCILER_WORKERS
94+
value: {{ quote $.Values.controller.reconciler.workers.schedule }}
95+
{{- end }}
9296
{{- if $.Values.extraEnv }}
9397
{{ toYaml $.Values.extraEnv | indent 8}}
9498
{{- end }}

charts/fleet/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ controller:
119119
cluster: "50"
120120
clustergroup: "50"
121121
imagescan: "50"
122+
schedule: "50"
122123

123124
gitjob:
124125
replicas: 1

internal/cmd/controller/reconciler/cronduration_job.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,6 @@ func (c *CronDurationJob) executeStart(ctx context.Context) error {
214214
return err
215215
}
216216

217-
// Sets ActiveSchedule to true for all matching clusters.
218-
for _, cluster := range clusters {
219-
if err := setClusterActiveSchedule(context.Background(), c.client, cluster, c.Schedule.Namespace, true); err != nil {
220-
return err
221-
}
222-
}
223-
224217
// Sets Scheduled to false for all clusters that previously matched but no longer do.
225218
for _, cluster := range c.MatchingClusters {
226219
if !slices.Contains(clusters, cluster) {
@@ -231,9 +224,11 @@ func (c *CronDurationJob) executeStart(ctx context.Context) error {
231224
}
232225
}
233226

234-
// Sets Scheduled to true to all the matching clusters
235-
if err := setClustersScheduled(ctx, c.client, clusters, c.Schedule.Namespace, true); err != nil {
236-
return err
227+
// Sets ActiveSchedule to true for all matching clusters.
228+
for _, cluster := range clusters {
229+
if err := setClusterActiveSchedule(ctx, c.client, cluster, c.Schedule.Namespace, true); err != nil {
230+
return err
231+
}
237232
}
238233
c.MatchingClusters = clusters
239234

internal/cmd/controller/reconciler/cronduration_job_test.go

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package reconciler
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

78
. "github.com/onsi/ginkgo/v2"
@@ -298,14 +299,13 @@ var _ = Describe("CronDurationJob", func() {
298299

299300
Context("scheduleJob", func() {
300301
It("should schedule a job and update status", func() {
301-
// wait a bit if we are too close to second 59...
302-
// this is to ensure calculations based on minutes and seconds
303-
waitIfTimeIsTooCloseTo59()
304-
302+
// set the schedule time to now plus 10 seconds so we can check
303+
// later knowning the expected value.
304+
scheduleTime := time.Now().Add(10 * time.Second)
305+
schedule.Spec.Schedule = fmt.Sprintf("%d %d %d * * *", scheduleTime.Second(), scheduleTime.Minute(), scheduleTime.Hour())
305306
job, err := newCronDurationJob(ctx, schedule, scheduler, k8sclient)
306307
Expect(err).NotTo(HaveOccurred())
307308

308-
timeBeforeSchedule := time.Now()
309309
err = job.scheduleJob(ctx)
310310
Expect(err).NotTo(HaveOccurred())
311311

@@ -319,33 +319,32 @@ var _ = Describe("CronDurationJob", func() {
319319
err = k8sclient.Get(ctx, types.NamespacedName{Name: "test-schedule", Namespace: "default"}, updatedSchedule)
320320
Expect(err).NotTo(HaveOccurred())
321321
Expect(updatedSchedule.Status.Active).To(BeFalse())
322-
// time should be the next minute and 0 seconds
323-
Expect(updatedSchedule.Status.NextStartTime.Time.Second()).To(BeZero())
324-
Expect(updatedSchedule.Status.NextStartTime.Time.Minute()).To(Equal(timeBeforeSchedule.Add(time.Minute).Minute()))
325-
Expect(updatedSchedule.Status.NextStartTime.Time.Hour()).To(Equal(timeBeforeSchedule.Hour()))
326-
// Expect(updatedSchedule.Status.NextStartTime.Time).To(BeTemporally("~", timeBeforeSchedule.Add(time.Minute), 2*time.Second))
322+
323+
Expect(updatedSchedule.Status.NextStartTime.Time.Second()).To(Equal(scheduleTime.Second()))
324+
Expect(updatedSchedule.Status.NextStartTime.Time.Minute()).To(Equal(scheduleTime.Minute()))
325+
Expect(updatedSchedule.Status.NextStartTime.Time.Hour()).To(Equal(scheduleTime.Hour()))
327326
Expect(updatedSchedule.Status.MatchingClusters).To(Equal(job.MatchingClusters))
328327
})
329328
})
330329

331330
Context("updateJob", func() {
332331
It("should update an existing job", func() {
333-
// wait a bit if we are too close to second 59...
334-
// this is to ensure calculations based on minutes and seconds
335-
waitIfTimeIsTooCloseTo59()
336-
332+
// set the schedule time to now plus 10 seconds so we can check
333+
// later knowning the expected value.
334+
scheduleTime := time.Now().Add(10 * time.Second)
335+
schedule.Spec.Schedule = fmt.Sprintf("%d %d %d * * *", scheduleTime.Second(), scheduleTime.Minute(), scheduleTime.Hour())
337336
job, err := newCronDurationJob(ctx, schedule, scheduler, k8sclient)
338337
Expect(err).NotTo(HaveOccurred())
339338

340339
err = job.scheduleJob(ctx)
341340
Expect(err).NotTo(HaveOccurred())
342341

343342
// Modify schedule to force an update
344-
schedule.Spec.Schedule = "0 */2 * * * *"
343+
newScheduleTime := time.Now().Add(30 * time.Second)
344+
schedule.Spec.Schedule = fmt.Sprintf("%d %d %d * * *", newScheduleTime.Second(), newScheduleTime.Minute(), newScheduleTime.Hour())
345345
updatedJob, err := newCronDurationJob(ctx, schedule, scheduler, k8sclient)
346346
Expect(err).NotTo(HaveOccurred())
347347

348-
timeBeforeUpdate := time.Now()
349348
err = updatedJob.updateJob(ctx)
350349
Expect(err).NotTo(HaveOccurred())
351350

@@ -361,18 +360,9 @@ var _ = Describe("CronDurationJob", func() {
361360
Expect(err).NotTo(HaveOccurred())
362361
Expect(updatedSchedule.Status.Active).To(BeFalse())
363362

364-
// next fire time should be the next even minute and 0 seconds
365-
Expect(updatedSchedule.Status.NextStartTime.Time.Second()).To(BeZero())
366-
if timeBeforeUpdate.Minute()%2 == 0 {
367-
Expect(updatedSchedule.Status.NextStartTime.Time.Minute()).To(Equal(timeBeforeUpdate.Add(2 * time.Minute).Minute()))
368-
} else {
369-
Expect(updatedSchedule.Status.NextStartTime.Time.Minute()).To(Equal(timeBeforeUpdate.Add(1 * time.Minute).Minute()))
370-
}
371-
if timeBeforeUpdate.Minute() < 59 {
372-
Expect(updatedSchedule.Status.NextStartTime.Time.Hour()).To(Equal(timeBeforeUpdate.Hour()))
373-
} else {
374-
Expect(updatedSchedule.Status.NextStartTime.Time.Hour()).To(Equal(timeBeforeUpdate.Add(1 * time.Hour).Hour()))
375-
}
363+
Expect(updatedSchedule.Status.NextStartTime.Time.Second()).To(Equal(newScheduleTime.Second()))
364+
Expect(updatedSchedule.Status.NextStartTime.Time.Minute()).To(Equal(newScheduleTime.Minute()))
365+
Expect(updatedSchedule.Status.NextStartTime.Time.Hour()).To(Equal(newScheduleTime.Hour()))
376366
Expect(updatedSchedule.Status.MatchingClusters).To(Equal(job.MatchingClusters))
377367
})
378368
})
@@ -552,14 +542,3 @@ var _ = Describe("CronDurationJob", func() {
552542
})
553543
})
554544
})
555-
556-
func waitIfTimeIsTooCloseTo59() {
557-
for {
558-
now := time.Now()
559-
// if we are up to second 55... just break
560-
if now.Second() < 55 {
561-
break
562-
}
563-
time.Sleep(200 * time.Millisecond)
564-
}
565-
}

0 commit comments

Comments
 (0)