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

feat: set leader election when single controller deployments are configured #3529

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Adding a new version? You'll need three changes:
that all time series for those metrics will get a new label designating the
address of the dataplane that the configuration push has been targeted for.
[#3521](https://github.com/Kong/kubernetes-ingress-controller/pull/3521)
- Leader election is enabled by default then kong admin service discovery is enabled.
[#3529](https://github.com/Kong/kubernetes-ingress-controller/pull/3529)

### Fixed

Expand Down
28 changes: 22 additions & 6 deletions internal/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
knativev1alpha1 "knative.dev/networking/pkg/apis/networking/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

Expand Down Expand Up @@ -168,12 +169,7 @@ func Run(ctx context.Context, c *Config, diagnostic util.ConfigDumpDiagnostic, d
if err := mgr.AddHealthzCheck("health", healthz.Ping); err != nil {
return fmt.Errorf("unable to setup healthz: %w", err)
}
if err := mgr.AddReadyzCheck("check", func(_ *http.Request) error {
if !synchronizer.IsReady() {
return errors.New("synchronizer not yet configured")
}
return nil
}); err != nil {
if err := mgr.AddReadyzCheck("check", readyzHandler(mgr, synchronizer)); err != nil {
return fmt.Errorf("unable to setup readyz: %w", err)
}

Expand Down Expand Up @@ -206,6 +202,26 @@ func Run(ctx context.Context, c *Config, diagnostic util.ConfigDumpDiagnostic, d
return mgr.Start(ctx)
}

type IsReady interface {
IsReady() bool
}

func readyzHandler(mgr manager.Manager, dataplaneSynchronizer IsReady) func(*http.Request) error {
return func(_ *http.Request) error {
select {
// If we're elected as leader then report readiness based on the readiness
// of dataplane synchronizer.
case <-mgr.Elected():
if !dataplaneSynchronizer.IsReady() {
return errors.New("synchronizer not yet configured")
}
// If we're not the leader then just report as ready.
default:
}
return nil
}
}

func getScheme() (*runtime.Scheme, error) {
scheme := runtime.NewScheme()

Expand Down
4 changes: 4 additions & 0 deletions internal/manager/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func leaderElectionEnabled(logger logr.Logger, c *Config, dbmode string) bool {
}

if dbmode == "off" {
if c.KongAdminSvc.Name != "" {
logger.Info("DB-less mode detected with service detection, enabling leader election")
return true
}
logger.Info("DB-less mode detected, disabling leader election")
return false
}
Expand Down