Skip to content

Commit f6b98e2

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 f6b98e2

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

pkg/certwatcher/certwatcher.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,7 @@ func (cw *CertWatcher) handleEvent(event fsnotify.Event) {
240240
log.Error(err, "error re-reading certificate")
241241
}
242242
}
243+
244+
func (cw *CertWatcher) NeedLeaderElection() bool {
245+
return false
246+
}

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)