Skip to content

Commit

Permalink
feat: set leader election when single controller deployments are conf…
Browse files Browse the repository at this point in the history
…igured
  • Loading branch information
pmalek committed Feb 10, 2023
1 parent 8473b53 commit 3b4a0df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
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

0 comments on commit 3b4a0df

Please sign in to comment.