Skip to content

Commit

Permalink
✨ Add controller option for setting leader election (#2117)
Browse files Browse the repository at this point in the history
* Add controller option for overrding the need for leader election

* Update Options to be clearer

Addresses PR review comment
  • Loading branch information
jonstacks committed Jan 5, 2023
1 parent 056869c commit b756161
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/controller/controller.go
Expand Up @@ -58,6 +58,10 @@ type Options struct {
// RecoverPanic indicates whether the panic caused by reconcile should be recovered.
// Defaults to the Controller.RecoverPanic setting from the Manager if unset.
RecoverPanic *bool

// NeedLeaderElection indicates whether the controller needs to use leader election.
// Defaults to true, which means the controller will use leader election.
NeedLeaderElection *bool
}

// Controller implements a Kubernetes API. A Controller manages a work queue fed reconcile.Requests
Expand Down Expand Up @@ -156,6 +160,7 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
Name: name,
LogConstructor: options.LogConstructor,
RecoverPanic: options.RecoverPanic,
LeaderElected: options.NeedLeaderElection,
}, nil
}

Expand Down
44 changes: 44 additions & 0 deletions pkg/controller/controller_test.go
Expand Up @@ -178,6 +178,50 @@ var _ = Describe("controller.Controller", func() {
Expect(ctrl.RecoverPanic).NotTo(BeNil())
Expect(*ctrl.RecoverPanic).To(BeFalse())
})

It("should default NeedLeaderElection on the controller to true", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Reconciler: rec,
})
Expect(err).NotTo(HaveOccurred())

ctrl, ok := c.(*internalcontroller.Controller)
Expect(ok).To(BeTrue())

Expect(ctrl.NeedLeaderElection()).To(BeTrue())
})

It("should allow for setting leaderElected to false", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Reconciler: rec,
NeedLeaderElection: pointer.Bool(false),
})
Expect(err).NotTo(HaveOccurred())

ctrl, ok := c.(*internalcontroller.Controller)
Expect(ok).To(BeTrue())

Expect(ctrl.NeedLeaderElection()).To(BeFalse())
})

It("should implement manager.LeaderElectionRunnable", func() {
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Reconciler: rec,
})
Expect(err).NotTo(HaveOccurred())

_, ok := c.(manager.LeaderElectionRunnable)
Expect(ok).To(BeTrue())
})
})
})

Expand Down
11 changes: 11 additions & 0 deletions pkg/internal/controller/controller.go
Expand Up @@ -93,6 +93,9 @@ type Controller struct {

// RecoverPanic indicates whether the panic caused by reconcile should be recovered.
RecoverPanic *bool

// LeaderElected indicates whether the controller is leader elected or always running.
LeaderElected *bool
}

// watchDescription contains all the information necessary to start a watch.
Expand Down Expand Up @@ -152,6 +155,14 @@ func (c *Controller) Watch(src source.Source, evthdler handler.EventHandler, prc
return src.Start(c.ctx, evthdler, c.Queue, prct...)
}

// NeedLeaderElection implements the manager.LeaderElectionRunnable interface.
func (c *Controller) NeedLeaderElection() bool {
if c.LeaderElected == nil {
return true
}
return *c.LeaderElected
}

// Start implements controller.Controller.
func (c *Controller) Start(ctx context.Context) error {
// use an IIFE to get proper lock handling
Expand Down

0 comments on commit b756161

Please sign in to comment.