Skip to content

Commit

Permalink
leaderelection: use 'leases' as default resource lock object
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
  • Loading branch information
joelanford committed Jan 11, 2022
1 parent e52a8b1 commit d2c2839
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
12 changes: 7 additions & 5 deletions pkg/leaderelection/leader_election.go
Expand Up @@ -27,6 +27,7 @@ import (
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/leaderelection/resourcelock"

"sigs.k8s.io/controller-runtime/pkg/recorder"
)

Expand All @@ -39,7 +40,7 @@ type Options struct {
LeaderElection bool

// LeaderElectionResourceLock determines which resource lock to use for leader election,
// defaults to "configmapsleases".
// defaults to "leases".
LeaderElectionResourceLock string

// LeaderElectionNamespace determines the namespace in which the leader
Expand All @@ -57,11 +58,12 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
return nil, nil
}

// Default resource lock to "configmapsleases". We must keep this default until we are sure all controller-runtime
// users have upgraded from the original default ConfigMap lock to a controller-runtime version that has this new
// default. Many users of controller-runtime skip versions, so we should be extremely conservative here.
// Default resource lock to "leases". The previous default (from v0.7.0 to v0.11.x) was configmapsleases, which was
// used to migrate from configmaps to leases. Since the default was "configmapsleases" for over a year, spanning
// five minor releases, any actively maintained operators are very likely to have a released version that uses
// "configmapsleases". Therefore defaulting to "leases" should be safe.
if options.LeaderElectionResourceLock == "" {
options.LeaderElectionResourceLock = resourcelock.ConfigMapsLeasesResourceLock
options.LeaderElectionResourceLock = resourcelock.LeasesResourceLock
}

// LeaderElectionID must be provided to prevent clashes
Expand Down
10 changes: 3 additions & 7 deletions pkg/manager/manager_test.go
Expand Up @@ -446,18 +446,14 @@ var _ = Describe("manger.Manager", func() {
// We must keep this default until we are sure all controller-runtime users have upgraded from the original default
// ConfigMap lock to a controller-runtime version that has this new default. Many users of controller-runtime skip
// versions, so we should be extremely conservative here.
It("should default to ConfigMapsLeasesResourceLock", func() {
It("should default to sLeasesResourceLock", func() {
m, err := New(cfg, Options{LeaderElection: true, LeaderElectionID: "controller-runtime", LeaderElectionNamespace: "my-ns"})
Expect(m).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
cm, ok := m.(*controllerManager)
Expect(ok).To(BeTrue())
multilock, isMultiLock := cm.resourceLock.(*resourcelock.MultiLock)
Expect(isMultiLock).To(BeTrue())
_, primaryIsConfigMapLock := multilock.Primary.(*resourcelock.ConfigMapLock)
Expect(primaryIsConfigMapLock).To(BeTrue())
_, secondaryIsLeaseLock := multilock.Secondary.(*resourcelock.LeaseLock)
Expect(secondaryIsLeaseLock).To(BeTrue())
_, isLeaseLock := cm.resourceLock.(*resourcelock.LeaseLock)
Expect(isLeaseLock).To(BeTrue())

})
It("should use the specified ResourceLock", func() {
Expand Down

0 comments on commit d2c2839

Please sign in to comment.