Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Allow to provide a custom lock interface to manager #2027

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 19 additions & 8 deletions pkg/manager/manager.go
Expand Up @@ -193,6 +193,12 @@ type Options struct {
// LeaseDuration time first.
LeaderElectionReleaseOnCancel bool

// LeaderElectionResourceLockInterface allows to provide a custom resourcelock.Interface that was created outside
// of the controller-runtime. If this value is set the options LeaderElectionID, LeaderElectionNamespace,
// LeaderElectionResourceLock, LeaseDuration, RenewDeadline and RetryPeriod will be ignored. This can be useful if you
// want to use a locking mechanism that is currently not supported, like a MultiLock across two Kubernetes clusters.
LeaderElectionResourceLockInterface resourcelock.Interface

// LeaseDuration is the duration that non-leader candidates will
// wait to force acquire leadership. This is measured against time of
// last observed ack. Default is 15 seconds.
Expand Down Expand Up @@ -377,14 +383,19 @@ func New(config *rest.Config, options Options) (Manager, error) {
}
}

resourceLock, err := options.newResourceLock(leaderConfig, leaderRecorderProvider, leaderelection.Options{
LeaderElection: options.LeaderElection,
LeaderElectionResourceLock: options.LeaderElectionResourceLock,
LeaderElectionID: options.LeaderElectionID,
LeaderElectionNamespace: options.LeaderElectionNamespace,
})
if err != nil {
return nil, err
var resourceLock resourcelock.Interface
if options.LeaderElectionResourceLockInterface != nil && options.LeaderElection {
resourceLock = options.LeaderElectionResourceLockInterface
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If options.LeaderElectionResourceLockInterface is nil we will go into the else clause even if options.LeaderElection is false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah nvm, missed the fact that we only construct it her and whether or not we use it is determined elsewhere

resourceLock, err = options.newResourceLock(leaderConfig, leaderRecorderProvider, leaderelection.Options{
LeaderElection: options.LeaderElection,
LeaderElectionResourceLock: options.LeaderElectionResourceLock,
LeaderElectionID: options.LeaderElectionID,
LeaderElectionNamespace: options.LeaderElectionNamespace,
})
if err != nil {
return nil, err
}
}

// Create the metrics listener. This will throw an error if the metrics bind
Expand Down
19 changes: 19 additions & 0 deletions pkg/manager/manager_test.go
Expand Up @@ -511,6 +511,25 @@ var _ = Describe("manger.Manager", func() {
Expect(err).To(BeNil())
Expect(record.HolderIdentity).To(BeEmpty())
})
When("using a custom LeaderElectionResourceLockInterface", func() {
It("should use the custom LeaderElectionResourceLockInterface", func() {
rl, err := fakeleaderelection.NewResourceLock(nil, nil, leaderelection.Options{})
Expect(err).NotTo(HaveOccurred())

m, err := New(cfg, Options{
LeaderElection: true,
LeaderElectionResourceLockInterface: rl,
newResourceLock: func(config *rest.Config, recorderProvider recorder.Provider, options leaderelection.Options) (resourcelock.Interface, error) {
return nil, fmt.Errorf("this should not be called")
},
})
Expect(m).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
cm, ok := m.(*controllerManager)
Expect(ok).To(BeTrue())
Expect(cm.resourceLock).To(Equal(rl))
})
})
})

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