Skip to content

Commit 4dfefb0

Browse files
committed
🌱 Certwatcher: Don't require leaderelection
Controller-Runtime starts the certwatcher in the webhook server, which means it always runs regardless of the current replica being leader or not. It turns out that kubebuilder adds it to the manager and as a result, it only runs on leader replicas. Make it a `LeaderElectionRunnable` and don't require leader election so that it will work correctly even if other projects use it in ways that were not originally anticipated.
1 parent 2704163 commit 4dfefb0

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

pkg/certwatcher/certwatcher.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,9 @@ func (cw *CertWatcher) handleEvent(event fsnotify.Event) {
240240
log.Error(err, "error re-reading certificate")
241241
}
242242
}
243+
244+
// NeedLeaderElection indicates that the cert-manager
245+
// does not need leader election.
246+
func (cw *CertWatcher) NeedLeaderElection() bool {
247+
return false
248+
}

pkg/certwatcher/certwatcher_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737

3838
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
3939
"sigs.k8s.io/controller-runtime/pkg/certwatcher/metrics"
40+
"sigs.k8s.io/controller-runtime/pkg/manager"
4041
)
4142

4243
var _ = Describe("CertWatcher", func() {
@@ -92,6 +93,12 @@ var _ = Describe("CertWatcher", func() {
9293
}
9394
})
9495

96+
It("should not require LeaderElection", func() {
97+
leaderElectionRunnable, isLeaderElectionRunnable := any(watcher).(manager.LeaderElectionRunnable)
98+
Expect(isLeaderElectionRunnable).To(BeTrue())
99+
Expect(leaderElectionRunnable.NeedLeaderElection()).To(BeFalse())
100+
})
101+
95102
It("should read the initial cert/key", func() {
96103
// This test verifies the initial read succeeded. So interval doesn't matter.
97104
doneCh := startWatcher(10 * time.Second)

0 commit comments

Comments
 (0)