Skip to content

Commit b2bed5b

Browse files
committed
Address naming + comments from sbueringer.
1 parent fc7c8c5 commit b2bed5b

File tree

9 files changed

+45
-44
lines changed

9 files changed

+45
-44
lines changed

pkg/config/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type Controller struct {
6666
// improves leadership failover time, as the caches will be prepopulated before the controller
6767
// transitions to be leader.
6868
//
69-
// When set to true, the controller will start its sources without transitioning to be leader.
69+
// When set to true, the controller will start its sources without waiting to become leader.
7070
// Defaults to false.
7171
NeedWarmup *bool
7272

pkg/controller/controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright 2018 The Kubernetes Authors.
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
23
34
Licensed under the Apache License, Version 2.0 (the "License");
45
you may not use this file except in compliance with the License.

pkg/controller/controller_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,11 @@ var _ = Describe("controller.Controller", func() {
475475
Expect(ok).To(BeFalse())
476476
})
477477

478-
It("should set ShouldWarmupWithoutLeadership correctly", func() {
478+
It("should set NeedWarmup correctly", func() {
479479
m, err := manager.New(cfg, manager.Options{})
480480
Expect(err).NotTo(HaveOccurred())
481481

482-
// Test with ShouldWarmupWithoutLeadership set to true
482+
// Test with NeedWarmup set to true
483483
ctrlWithWarmup, err := controller.New("warmup-enabled-ctrl", m, controller.Options{
484484
Reconciler: reconcile.Func(nil),
485485
NeedWarmup: ptr.To(true),
@@ -491,7 +491,7 @@ var _ = Describe("controller.Controller", func() {
491491
Expect(internalCtrlWithWarmup.NeedWarmup).NotTo(BeNil())
492492
Expect(*internalCtrlWithWarmup.NeedWarmup).To(BeTrue())
493493

494-
// Test with ShouldWarmupWithoutLeadership set to false
494+
// Test with NeedWarmup set to false
495495
ctrlWithoutWarmup, err := controller.New("warmup-disabled-ctrl", m, controller.Options{
496496
Reconciler: reconcile.Func(nil),
497497
NeedWarmup: ptr.To(false),
@@ -503,7 +503,7 @@ var _ = Describe("controller.Controller", func() {
503503
Expect(internalCtrlWithoutWarmup.NeedWarmup).NotTo(BeNil())
504504
Expect(*internalCtrlWithoutWarmup.NeedWarmup).To(BeFalse())
505505

506-
// Test with ShouldWarmupWithoutLeadership not set (should default to nil)
506+
// Test with NeedWarmup not set (should default to nil)
507507
ctrlWithDefaultWarmup, err := controller.New("warmup-default-ctrl", m, controller.Options{
508508
Reconciler: reconcile.Func(nil),
509509
})
@@ -514,8 +514,8 @@ var _ = Describe("controller.Controller", func() {
514514
Expect(internalCtrlWithDefaultWarmup.NeedWarmup).To(BeNil())
515515
})
516516

517-
It("should inherit ShouldWarmupWithoutLeadership from manager config", func() {
518-
// Test with manager default setting ShouldWarmupWithoutLeadership to true
517+
It("should inherit NeedWarmup from manager config", func() {
518+
// Test with manager default setting NeedWarmup to true
519519
managerWithWarmup, err := manager.New(cfg, manager.Options{
520520
Controller: config.Controller{
521521
NeedWarmup: ptr.To(true),

pkg/internal/controller/controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ type Controller[request comparable] struct {
9393
// didStartEventSources is used to indicate whether the event sources have been started.
9494
didStartEventSources atomic.Bool
9595

96-
// didEventSourcesFinishSync is used to indicate whether the event sources have finished
96+
// didEventSourcesFinishSyncSuccessfully is used to indicate whether the event sources have finished
9797
// successfully. It stores a *bool where
9898
// - nil: not finished syncing
9999
// - true: finished syncing without error
100100
// - false: finished syncing with error
101-
didEventSourcesFinishSync atomic.Value
101+
didEventSourcesFinishSyncSuccessfully atomic.Value
102102

103103
// LogConstructor is used to construct a logger to then log messages to users during reconciliation,
104104
// or for example when a watch is started.
@@ -175,10 +175,10 @@ func (c *Controller[request]) Warmup(ctx context.Context) error {
175175
return c.startEventSources(ctx)
176176
}
177177

178-
// DidFinishWarmup implements the manager.WarmupRunnable interface.
179-
func (c *Controller[request]) DidFinishWarmup(ctx context.Context) bool {
178+
// WaitForWarmupComplete returns true if warmup has completed successfully.
179+
func (c *Controller[request]) WaitForWarmupComplete(ctx context.Context) bool {
180180
err := wait.PollUntilContextCancel(ctx, syncedPollPeriod, true, func(ctx context.Context) (bool, error) {
181-
didFinishSync, ok := c.didEventSourcesFinishSync.Load().(*bool)
181+
didFinishSync, ok := c.didEventSourcesFinishSyncSuccessfully.Load().(*bool)
182182
if !ok {
183183
return false, errors.New("unexpected error: didEventSourcesFinishSync is not a bool pointer")
184184
}
@@ -279,7 +279,7 @@ func (c *Controller[request]) startEventSources(ctx context.Context) error {
279279
// CAS returns false if value is already true, so early exit since another goroutine must have
280280
// called startEventSources previously
281281
if !c.didStartEventSources.CompareAndSwap(false, true) {
282-
c.LogConstructor(nil).Info("Skipping starting event sources since it was already started")
282+
c.LogConstructor(nil).Info("Skipping starting event sources since they were previously started")
283283
return nil
284284
}
285285

@@ -337,7 +337,7 @@ func (c *Controller[request]) startEventSources(ctx context.Context) error {
337337
}
338338
err := errGroup.Wait()
339339

340-
c.didEventSourcesFinishSync.Store(ptr.To(err == nil))
340+
c.didEventSourcesFinishSyncSuccessfully.Store(ptr.To(err == nil))
341341

342342
return err
343343
}

pkg/manager/internal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
439439
return fmt.Errorf("failed to start other runnables: %w", err)
440440
}
441441

442-
// Start and wait for sources to start.
442+
// Start WarmupRunnables and wait for warmup to complete.
443443
if err := cm.runnables.Warmup.Start(cm.internalCtx); err != nil {
444444
return fmt.Errorf("failed to start warmup runnables: %w", err)
445445
}

pkg/manager/manager.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,11 @@ type LeaderElectionRunnable interface {
314314
NeedLeaderElection() bool
315315
}
316316

317-
// WarmupRunnable knows if a Runnable should be a warmup runnable. A warmup runnable is a runnable
317+
// warmupRunnable knows if a Runnable requires warmup. A warmup runnable is a runnable
318318
// that should be run when the manager is started, but before the leader election is acquired.
319-
// Note: Implementing this interface is only useful when LeaderElection is enabled, as the behavior
320-
// without leaderelection is to run LeaderElectionRunnables immediately.
321-
type WarmupRunnable interface {
319+
// Note: Implementing this interface is only useful when LeaderElection can be enabled, as the
320+
// behavior when leaderelection is not enabled is to run LeaderElectionRunnables immediately.
321+
type warmupRunnable interface {
322322
// Warmup is the implementation of the warmup runnable.
323323
Warmup(context.Context) error
324324

pkg/manager/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,7 +1953,7 @@ var _ = Describe("manger.Manager", func() {
19531953
By("Creating a runnable that implements WarmupRunnable interface")
19541954
// Create a warmup runnable
19551955
warmupRunnable := warmupRunnableFunc{
1956-
RunFunc: func(ctx context.Context) error {
1956+
StartFunc: func(ctx context.Context) error {
19571957
// This is the main runnable that will be executed after leader election
19581958
<-ctx.Done()
19591959
return nil
@@ -1968,7 +1968,7 @@ var _ = Describe("manger.Manager", func() {
19681968

19691969
By("Creating a runnable that requires leader election")
19701970
leaderElectionRunnable := leaderElectionRunnableFunc{
1971-
RunFunc: func(ctx context.Context) error {
1971+
StartFunc: func(ctx context.Context) error {
19721972
// This will only be called after leader election is won
19731973
close(leaderElectionRunnableCalled)
19741974
<-ctx.Done()

pkg/manager/runnable_group.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (r *runnables) Add(fn Runnable) error {
6767
})
6868
case webhook.Server:
6969
return r.Webhooks.Add(fn, nil)
70-
case WarmupRunnable, LeaderElectionRunnable:
71-
if warmupRunnable, ok := fn.(WarmupRunnable); ok {
70+
case warmupRunnable, LeaderElectionRunnable:
71+
if warmupRunnable, ok := fn.(warmupRunnable); ok {
7272
if err := r.Warmup.Add(
7373
RunnableFunc(warmupRunnable.Warmup),
7474
func(ctx context.Context) bool {

pkg/manager/runnable_group_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var _ = Describe("runnables", func() {
5656

5757
It("should add WarmupRunnable to the Warmup and LeaderElection group", func() {
5858
warmupRunnable := warmupRunnableFunc{
59-
RunFunc: func(c context.Context) error {
59+
StartFunc: func(c context.Context) error {
6060
<-c.Done()
6161
return nil
6262
},
@@ -72,8 +72,8 @@ var _ = Describe("runnables", func() {
7272
})
7373

7474
It("should add WarmupRunnable that doesn't needs leader election to warmup group only", func() {
75-
warmupRunnable := combinedRunnable{
76-
RunFunc: func(c context.Context) error {
75+
warmupRunnable := leaderElectionAndWarmupRunnable{
76+
StartFunc: func(c context.Context) error {
7777
<-c.Done()
7878
return nil
7979
},
@@ -93,8 +93,8 @@ var _ = Describe("runnables", func() {
9393
})
9494

9595
It("should add WarmupRunnable that needs leader election to Warmup and LeaderElection group, not Others", func() {
96-
warmupRunnable := combinedRunnable{
97-
RunFunc: func(c context.Context) error {
96+
warmupRunnable := leaderElectionAndWarmupRunnable{
97+
StartFunc: func(c context.Context) error {
9898
<-c.Done()
9999
return nil
100100
},
@@ -118,7 +118,7 @@ var _ = Describe("runnables", func() {
118118
var warmupExecuted atomic.Bool
119119

120120
warmupRunnable := warmupRunnableFunc{
121-
RunFunc: func(c context.Context) error {
121+
StartFunc: func(c context.Context) error {
122122
<-c.Done()
123123
return nil
124124
},
@@ -145,7 +145,7 @@ var _ = Describe("runnables", func() {
145145
expectedErr := fmt.Errorf("expected warmup error")
146146

147147
warmupRunnable := warmupRunnableFunc{
148-
RunFunc: func(c context.Context) error {
148+
StartFunc: func(c context.Context) error {
149149
<-c.Done()
150150
return nil
151151
},
@@ -354,29 +354,29 @@ var _ LeaderElectionRunnable = &leaderElectionRunnableFunc{}
354354
// leaderElectionRunnableFunc is a helper struct that implements LeaderElectionRunnable
355355
// for testing purposes.
356356
type leaderElectionRunnableFunc struct {
357-
RunFunc func(context.Context) error
357+
StartFunc func(context.Context) error
358358
NeedLeaderElectionFunc func() bool
359359
}
360360

361361
func (r leaderElectionRunnableFunc) Start(ctx context.Context) error {
362-
return r.RunFunc(ctx)
362+
return r.StartFunc(ctx)
363363
}
364364

365365
func (r leaderElectionRunnableFunc) NeedLeaderElection() bool {
366366
return r.NeedLeaderElectionFunc()
367367
}
368368

369-
var _ WarmupRunnable = &warmupRunnableFunc{}
369+
var _ warmupRunnable = &warmupRunnableFunc{}
370370

371371
// warmupRunnableFunc is a helper struct that implements WarmupRunnable
372372
// for testing purposes.
373373
type warmupRunnableFunc struct {
374-
RunFunc func(context.Context) error
374+
StartFunc func(context.Context) error
375375
WarmupFunc func(context.Context) error
376376
}
377377

378378
func (r warmupRunnableFunc) Start(ctx context.Context) error {
379-
return r.RunFunc(ctx)
379+
return r.StartFunc(ctx)
380380
}
381381

382382
func (r warmupRunnableFunc) Warmup(ctx context.Context) error {
@@ -387,25 +387,25 @@ func (r warmupRunnableFunc) WaitForWarmupComplete(ctx context.Context) bool {
387387
return true
388388
}
389389

390-
// combinedRunnable implements both WarmupRunnable and LeaderElectionRunnable
391-
type combinedRunnable struct {
392-
RunFunc func(context.Context) error
390+
// leaderElectionAndWarmupRunnable implements both WarmupRunnable and LeaderElectionRunnable
391+
type leaderElectionAndWarmupRunnable struct {
392+
StartFunc func(context.Context) error
393393
WarmupFunc func(context.Context) error
394394
NeedLeaderElectionFunc func() bool
395395
}
396396

397-
func (r combinedRunnable) Start(ctx context.Context) error {
398-
return r.RunFunc(ctx)
397+
func (r leaderElectionAndWarmupRunnable) Start(ctx context.Context) error {
398+
return r.StartFunc(ctx)
399399
}
400400

401-
func (r combinedRunnable) Warmup(ctx context.Context) error {
401+
func (r leaderElectionAndWarmupRunnable) Warmup(ctx context.Context) error {
402402
return r.WarmupFunc(ctx)
403403
}
404404

405-
func (r combinedRunnable) WaitForWarmupComplete(ctx context.Context) bool {
405+
func (r leaderElectionAndWarmupRunnable) WaitForWarmupComplete(ctx context.Context) bool {
406406
return true
407407
}
408408

409-
func (r combinedRunnable) NeedLeaderElection() bool {
409+
func (r leaderElectionAndWarmupRunnable) NeedLeaderElection() bool {
410410
return r.NeedLeaderElectionFunc()
411411
}

0 commit comments

Comments
 (0)