Skip to content

Commit 237c907

Browse files
committed
Code review 1 changes
Signed-off-by: Xavi Garcia <xavi.garcia@suse.com>
1 parent b96fe35 commit 237c907

2 files changed

Lines changed: 36 additions & 19 deletions

File tree

internal/cmd/controller/reconciler/schedule_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func setClusterScheduled(ctx context.Context, c client.Client, name, namespace s
273273
old := cluster.DeepCopy()
274274
cluster.Status.Scheduled = scheduled
275275

276-
// when this function is called is either because we're updating a
276+
// This function is called either because we're updating a
277277
// Schedule or because we're creating it.
278278
// In both cases ActiveSchedule should be false as a Schedule
279279
// always begins in OffSchedule mode until the first start call is executed.
@@ -414,7 +414,6 @@ func setClustersScheduled(ctx context.Context, c client.Client, clusters []strin
414414
}
415415

416416
func updateScheduledClusters(ctx context.Context, scheduler quartz.Scheduler, c client.Client, clustersNew []string, clustersOld []string, namespace string) error {
417-
// first look for clusters that are not scheduled yet and flag them as scheduled
418417
for _, cluster := range clustersNew {
419418
if err := setClusterScheduled(ctx, c, cluster, namespace, true); err != nil {
420419
return err

internal/cmd/controller/reconciler/schedule_controller_test.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ import (
1919
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2020
)
2121

22+
type expected struct {
23+
scheduledJob bool
24+
statusScheduled bool
25+
statusActiveSchedule bool
26+
}
27+
2228
var _ = Describe("ScheduleReconciler", func() {
2329
var (
2430
ctx context.Context
@@ -305,10 +311,14 @@ var _ = Describe("ScheduleReconciler", func() {
305311

306312
// clusters 1, 2 and 3 should be scheduled in the quartz.Scheduler and also
307313
// be flagged as Scheduled
308-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster", "default", true, true, false)
309-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster2", "default", true, true, false)
310-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster3", "default", true, true, false)
311-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster4", "default", false, false, false)
314+
checkState(scheduler, k8sclient, "test-cluster", "default",
315+
expected{scheduledJob: true, statusScheduled: true, statusActiveSchedule: false})
316+
checkState(scheduler, k8sclient, "test-cluster2", "default",
317+
expected{scheduledJob: true, statusScheduled: true, statusActiveSchedule: false})
318+
checkState(scheduler, k8sclient, "test-cluster3", "default",
319+
expected{scheduledJob: true, statusScheduled: true, statusActiveSchedule: false})
320+
checkState(scheduler, k8sclient, "test-cluster4", "default",
321+
expected{scheduledJob: false, statusScheduled: false, statusActiveSchedule: false})
312322

313323
// force the start of the schedule (so it sets .Status.ActiveSchedule=true)
314324
jobKey := scheduleKey(schedule)
@@ -323,10 +333,14 @@ var _ = Describe("ScheduleReconciler", func() {
323333
Expect(err).NotTo(HaveOccurred())
324334

325335
// check now that the clusters have the expected values, specially Status.ActiveSchedule
326-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster", "default", true, true, true)
327-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster2", "default", true, true, true)
328-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster3", "default", true, true, true)
329-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster4", "default", false, false, false)
336+
checkState(scheduler, k8sclient, "test-cluster", "default",
337+
expected{scheduledJob: true, statusScheduled: true, statusActiveSchedule: true})
338+
checkState(scheduler, k8sclient, "test-cluster2", "default",
339+
expected{scheduledJob: true, statusScheduled: true, statusActiveSchedule: true})
340+
checkState(scheduler, k8sclient, "test-cluster3", "default",
341+
expected{scheduledJob: true, statusScheduled: true, statusActiveSchedule: true})
342+
checkState(scheduler, k8sclient, "test-cluster4", "default",
343+
expected{scheduledJob: false, statusScheduled: false, statusActiveSchedule: false})
330344

331345
// update the schedule, now it only looks for the label foo=bar
332346
scheduleUpdated := &fleet.Schedule{}
@@ -345,29 +359,33 @@ var _ = Describe("ScheduleReconciler", func() {
345359
Expect(err).NotTo(HaveOccurred())
346360

347361
// cluster 2 and 4 should be still targeted.
348-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster", "default", false, false, false)
362+
checkState(scheduler, k8sclient, "test-cluster", "default",
363+
expected{scheduledJob: false, statusScheduled: false, statusActiveSchedule: false})
349364
// cluster 2 had Status.ActiveSchedule set to true, but because we updated the Schedule
350365
// it should be back to false.
351-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster2", "default", true, true, false)
352-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster3", "default", false, false, false)
353-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster4", "default", true, true, false)
366+
checkState(scheduler, k8sclient, "test-cluster2", "default",
367+
expected{scheduledJob: true, statusScheduled: true, statusActiveSchedule: false})
368+
checkState(scheduler, k8sclient, "test-cluster3", "default",
369+
expected{scheduledJob: false, statusScheduled: false, statusActiveSchedule: false})
370+
checkState(scheduler, k8sclient, "test-cluster4", "default",
371+
expected{scheduledJob: true, statusScheduled: true, statusActiveSchedule: false})
354372
})
355373
})
356374
})
357375

358-
func checkClusterIsScheduled(
376+
func checkState(
359377
scheduler quartz.Scheduler,
360378
k8sclient client.Client,
361379
cluster, namespace string,
362-
scheduledExpected, flaggedAsScheduledExpected, activeScheduleExpected bool) {
380+
expectedState expected) {
363381
isScheduled, err := isClusterScheduled(scheduler, cluster, namespace)
364382
Expect(err).NotTo(HaveOccurred())
365-
Expect(isScheduled).To(Equal(scheduledExpected))
383+
Expect(isScheduled).To(Equal(expectedState.scheduledJob))
366384

367385
key := client.ObjectKey{Name: cluster, Namespace: namespace}
368386
clusterObj := &fleet.Cluster{}
369387
err = k8sclient.Get(context.Background(), key, clusterObj)
370388
Expect(err).NotTo(HaveOccurred())
371-
Expect(clusterObj.Status.Scheduled).To(Equal(flaggedAsScheduledExpected))
372-
Expect(clusterObj.Status.ActiveSchedule).To(Equal(activeScheduleExpected))
389+
Expect(clusterObj.Status.Scheduled).To(Equal(expectedState.statusScheduled))
390+
Expect(clusterObj.Status.ActiveSchedule).To(Equal(expectedState.statusActiveSchedule))
373391
}

0 commit comments

Comments
 (0)