Skip to content

Commit b31c39c

Browse files
committed
Allow to provide a custom lock interface to manager
1 parent 8ad090e commit b31c39c

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

pkg/manager/manager.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ type Options struct {
193193
// LeaseDuration time first.
194194
LeaderElectionReleaseOnCancel bool
195195

196+
// LeaderElectionResourceLockInterface allows to provide a custom resourcelock.Interface that was created outside
197+
// of the controller-runtime. If this value is set the options LeaderElectionID, LeaderElectionNamespace,
198+
// LeaderElectionResourceLock, LeaseDuration, RenewDeadline and RetryPeriod will be ignored. This can be useful if you
199+
// want to use a locking mechanism that is currently not supported, like a MultiLock across two Kubernetes clusters.
200+
LeaderElectionResourceLockInterface resourcelock.Interface
201+
196202
// LeaseDuration is the duration that non-leader candidates will
197203
// wait to force acquire leadership. This is measured against time of
198204
// last observed ack. Default is 15 seconds.
@@ -377,14 +383,19 @@ func New(config *rest.Config, options Options) (Manager, error) {
377383
}
378384
}
379385

380-
resourceLock, err := options.newResourceLock(leaderConfig, leaderRecorderProvider, leaderelection.Options{
381-
LeaderElection: options.LeaderElection,
382-
LeaderElectionResourceLock: options.LeaderElectionResourceLock,
383-
LeaderElectionID: options.LeaderElectionID,
384-
LeaderElectionNamespace: options.LeaderElectionNamespace,
385-
})
386-
if err != nil {
387-
return nil, err
386+
var resourceLock resourcelock.Interface
387+
if options.LeaderElectionResourceLockInterface != nil && options.LeaderElection {
388+
resourceLock = options.LeaderElectionResourceLockInterface
389+
} else {
390+
resourceLock, err = options.newResourceLock(leaderConfig, leaderRecorderProvider, leaderelection.Options{
391+
LeaderElection: options.LeaderElection,
392+
LeaderElectionResourceLock: options.LeaderElectionResourceLock,
393+
LeaderElectionID: options.LeaderElectionID,
394+
LeaderElectionNamespace: options.LeaderElectionNamespace,
395+
})
396+
if err != nil {
397+
return nil, err
398+
}
388399
}
389400

390401
// Create the metrics listener. This will throw an error if the metrics bind

pkg/manager/manager_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,25 @@ var _ = Describe("manger.Manager", func() {
511511
Expect(err).To(BeNil())
512512
Expect(record.HolderIdentity).To(BeEmpty())
513513
})
514+
When("using a custom LeaderElectionResourceLockInterface", func() {
515+
It("should use the custom LeaderElectionResourceLockInterface", func() {
516+
rl, err := fakeleaderelection.NewResourceLock(nil, nil, leaderelection.Options{})
517+
Expect(err).NotTo(HaveOccurred())
518+
519+
m, err := New(cfg, Options{
520+
LeaderElection: true,
521+
LeaderElectionResourceLockInterface: rl,
522+
newResourceLock: func(config *rest.Config, recorderProvider recorder.Provider, options leaderelection.Options) (resourcelock.Interface, error) {
523+
return nil, fmt.Errorf("this should not be called")
524+
},
525+
})
526+
Expect(m).ToNot(BeNil())
527+
Expect(err).ToNot(HaveOccurred())
528+
cm, ok := m.(*controllerManager)
529+
Expect(ok).To(BeTrue())
530+
Expect(cm.resourceLock).To(Equal(rl))
531+
})
532+
})
514533
})
515534

516535
It("should create a listener for the metrics if a valid address is provided", func() {

0 commit comments

Comments
 (0)