Skip to content

Commit 9f9e840

Browse files
authored
Merge pull request #2027 from johscheuer/allow-custom-lock-interface
✨ Allow to provide a custom lock interface to manager
2 parents 15d69a2 + b31c39c commit 9f9e840

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
@@ -194,6 +194,12 @@ type Options struct {
194194
// LeaseDuration time first.
195195
LeaderElectionReleaseOnCancel bool
196196

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

384-
resourceLock, err := options.newResourceLock(leaderConfig, leaderRecorderProvider, leaderelection.Options{
385-
LeaderElection: options.LeaderElection,
386-
LeaderElectionResourceLock: options.LeaderElectionResourceLock,
387-
LeaderElectionID: options.LeaderElectionID,
388-
LeaderElectionNamespace: options.LeaderElectionNamespace,
389-
})
390-
if err != nil {
391-
return nil, err
390+
var resourceLock resourcelock.Interface
391+
if options.LeaderElectionResourceLockInterface != nil && options.LeaderElection {
392+
resourceLock = options.LeaderElectionResourceLockInterface
393+
} else {
394+
resourceLock, err = options.newResourceLock(leaderConfig, leaderRecorderProvider, leaderelection.Options{
395+
LeaderElection: options.LeaderElection,
396+
LeaderElectionResourceLock: options.LeaderElectionResourceLock,
397+
LeaderElectionID: options.LeaderElectionID,
398+
LeaderElectionNamespace: options.LeaderElectionNamespace,
399+
})
400+
if err != nil {
401+
return nil, err
402+
}
392403
}
393404

394405
// 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
@@ -517,6 +517,25 @@ var _ = Describe("manger.Manager", func() {
517517
Expect(err).To(BeNil())
518518
Expect(record.HolderIdentity).To(BeEmpty())
519519
})
520+
When("using a custom LeaderElectionResourceLockInterface", func() {
521+
It("should use the custom LeaderElectionResourceLockInterface", func() {
522+
rl, err := fakeleaderelection.NewResourceLock(nil, nil, leaderelection.Options{})
523+
Expect(err).NotTo(HaveOccurred())
524+
525+
m, err := New(cfg, Options{
526+
LeaderElection: true,
527+
LeaderElectionResourceLockInterface: rl,
528+
newResourceLock: func(config *rest.Config, recorderProvider recorder.Provider, options leaderelection.Options) (resourcelock.Interface, error) {
529+
return nil, fmt.Errorf("this should not be called")
530+
},
531+
})
532+
Expect(m).ToNot(BeNil())
533+
Expect(err).ToNot(HaveOccurred())
534+
cm, ok := m.(*controllerManager)
535+
Expect(ok).To(BeTrue())
536+
Expect(cm.resourceLock).To(Equal(rl))
537+
})
538+
})
520539
})
521540

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

0 commit comments

Comments
 (0)