Skip to content

Commit 2a0249f

Browse files
LeaderElectionRunnable interface updated
1 parent 3222f6f commit 2a0249f

File tree

6 files changed

+25
-29
lines changed

6 files changed

+25
-29
lines changed

pkg/controller/controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ type Options struct {
3636
// Reconciler reconciles an object
3737
Reconciler reconcile.Reconciler
3838

39-
// LeaderElection determines whether or not to use leader election when
40-
// starting the controller.
39+
// LeaderElection determines whether or not to use leader election when starting the controller.
40+
// Defaults to true
4141
LeaderElection bool
4242

43-
// LeaderElectionID determines the name of the configmap that leader election
44-
// will use for holding the leader lock.
43+
// LeaderElectionID determines the name of the configmap that leader election will use for holding the leader lock.
4544
LeaderElectionID string
4645
}
4746

pkg/internal/controller/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ func (c *Controller) updateMetrics(reconcileTime time.Duration) {
265265
ctrlmetrics.ReconcileTime.WithLabelValues(c.Name).Observe(reconcileTime.Seconds())
266266
}
267267

268-
func (c *Controller) GetLeaderElection() bool {
268+
func (c *Controller) NeedLeaderElection() bool {
269269
return c.LeaderElection
270270
}
271271

272-
func (c *Controller) GetLeaderElectionID() string {
272+
func (c *Controller) GetID() string {
273273
return c.LeaderElectionID
274274
}

pkg/manager/internal.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,33 +336,33 @@ func (cm *controllerManager) startLeaderElectionRunnables() {
336336
// Write any Start errors to a channel so we can return them
337337
ctrl := c
338338
go func() {
339-
if sr, ok := ctrl.(SingletonRunnable); ok && sr.GetLeaderElection() {
340-
runnableLeaderElectionID := ctrl.(SingletonRunnable).GetLeaderElectionID()
339+
if leRunnable, ok := ctrl.(LeaderElectionRunnable); ok {
340+
leID := leRunnable.GetID()
341341

342342
// Check that leader election ID is defined
343-
if runnableLeaderElectionID == "" {
343+
if leID == "" {
344344
cm.errChan <- errors.New("LeaderElectionID must be configured")
345345
return
346346
}
347347

348348
// Check that leader election ID is unique
349-
if _, exists := cm.resourceLocksMap[runnableLeaderElectionID]; exists {
349+
if _, exists := cm.resourceLocksMap[leID]; exists {
350350
cm.errChan <- errors.New("LeaderElectionID must be unique")
351351
return
352352
}
353353

354354
// Create resource lock
355355
resourceLock, err := cm.newResourceLock(cm.config, cm.recorderProvider, crleaderelection.Options{
356356
LeaderElection: true,
357-
LeaderElectionID: runnableLeaderElectionID,
357+
LeaderElectionID: leID,
358358
LeaderElectionNamespace: cm.leaderElectionNamespace,
359359
})
360360
if err != nil {
361361
cm.errChan <- err
362362
}
363363

364-
cm.resourceLocksMap[runnableLeaderElectionID] = resourceLock
365-
err = cm.startLeaderElection(runnableLeaderElectionID, leaderelection.LeaderCallbacks{
364+
cm.resourceLocksMap[leID] = resourceLock
365+
err = cm.startLeaderElection(leID, leaderelection.LeaderCallbacks{
366366
OnStartedLeading: func(_ context.Context) {
367367
cm.errChan <- ctrl.Start(cm.internalStop)
368368
},

pkg/manager/manager.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,9 @@ type LeaderElectionRunnable interface {
190190
// NeedLeaderElection returns true if the Runnable needs to be run in the leader election mode.
191191
// e.g. controllers need to be run in leader election mode, while webhook server doesn't.
192192
NeedLeaderElection() bool
193-
}
194-
195-
// SingletonRunnable knows if Runnable needs leader election.
196-
// Runnable also have to be leader election Runnable(started after manager's leader election)
197-
type SingletonRunnable interface {
198-
// GetLeaderElection returns true if need to perform leader election
199-
GetLeaderElection() bool
200193

201-
// GetLeaderElectionID returns leader election ID
202-
GetLeaderElectionID() string
194+
// GetID returns leader election ID
195+
GetID() string
203196
}
204197

205198
// New returns a new Manager for creating Controllers.

pkg/manager/manager_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ var _ = Describe("manger.Manager", func() {
289289
m, err := New(cfg, options)
290290
Expect(err).ToNot(HaveOccurred())
291291

292-
r := &singletonRunnable{
292+
r := &leRunnable{
293293
leaderElectionID: "",
294294
}
295295
Expect(m.Add(r)).To(Succeed())
@@ -303,7 +303,7 @@ var _ = Describe("manger.Manager", func() {
303303
m, err := New(cfg, options)
304304
Expect(err).ToNot(HaveOccurred())
305305

306-
r := &singletonRunnable{
306+
r := &leRunnable{
307307
leaderElectionID: options.LeaderElectionID,
308308
}
309309
Expect(m.Add(r)).To(Succeed())
@@ -740,18 +740,18 @@ func (i *injectable) Start(<-chan struct{}) error {
740740
return nil
741741
}
742742

743-
type singletonRunnable struct {
743+
type leRunnable struct {
744744
leaderElectionID string
745745
}
746746

747-
func (*singletonRunnable) Start(<-chan struct{}) error {
747+
func (*leRunnable) Start(<-chan struct{}) error {
748748
return nil
749749
}
750750

751-
func (*singletonRunnable) GetLeaderElection() bool {
751+
func (*leRunnable) NeedLeaderElection() bool {
752752
return true
753753
}
754754

755-
func (sr *singletonRunnable) GetLeaderElectionID() string {
756-
return sr.leaderElectionID
755+
func (le *leRunnable) GetID() string {
756+
return le.leaderElectionID
757757
}

pkg/webhook/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ func (*Server) NeedLeaderElection() bool {
9595
return false
9696
}
9797

98+
func (*Server) GetID() string {
99+
return ""
100+
}
101+
98102
// Register marks the given webhook as being served at the given path.
99103
// It panics if two hooks are registered on the same path.
100104
func (s *Server) Register(path string, hook http.Handler) {

0 commit comments

Comments
 (0)