Skip to content

Commit

Permalink
Add is elected leader check for readiness (#54)
Browse files Browse the repository at this point in the history
* Add is elected leader check for readiness

* leader election release oncancel set to true

* fix lint
  • Loading branch information
ishaan-tf authored Dec 31, 2024
1 parent 4bbfd4a commit 2ac49a7
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions operator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/tls"
"flag"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -159,7 +160,7 @@ func mainWithError() error {
// the manager stops, so would be fine to enable this option. However,
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
// LeaderElectionReleaseOnCancel: true,
LeaderElectionReleaseOnCancel: true,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down Expand Up @@ -209,7 +210,7 @@ func mainWithError() error {
sentry.CaptureException(err)
return fmt.Errorf("main: %w", err)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
if err := mgr.AddReadyzCheck("readyz", leaderReadinessCheck(mgr)); err != nil {
setupLog.Error(err, "unable to set up ready check")
sentry.CaptureException(err)
return fmt.Errorf("main: %w", err)
Expand All @@ -224,3 +225,14 @@ func mainWithError() error {

return nil
}

func leaderReadinessCheck(mgr ctrl.Manager) healthz.Checker {
return func(req *http.Request) error {
select {
case <-mgr.Elected():
return nil
default:
return fmt.Errorf("controller is not the leader yet")
}
}
}

0 comments on commit 2ac49a7

Please sign in to comment.