From ab1d46e8ff02022c43458ed371b98ebff6b17e23 Mon Sep 17 00:00:00 2001 From: Mattia Lavacca Date: Wed, 19 Jul 2023 12:50:04 +0200 Subject: [PATCH] chore: isDBLess helper introduced Signed-off-by: Mattia Lavacca --- internal/dataplane/kong_client.go | 3 ++- internal/dataplane/synchronizer.go | 3 ++- internal/manager/config_validation.go | 3 ++- internal/manager/run.go | 3 ++- internal/manager/setup.go | 3 ++- internal/util/dataplane/mode.go | 6 ++++++ 6 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 internal/util/dataplane/mode.go diff --git a/internal/dataplane/kong_client.go b/internal/dataplane/kong_client.go index 1a9d27c45b5..e1c7d9e9b76 100644 --- a/internal/dataplane/kong_client.go +++ b/internal/dataplane/kong_client.go @@ -33,6 +33,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v2/internal/metrics" "github.com/kong/kubernetes-ingress-controller/v2/internal/store" "github.com/kong/kubernetes-ingress-controller/v2/internal/util" + "github.com/kong/kubernetes-ingress-controller/v2/internal/util/dataplane" k8sobj "github.com/kong/kubernetes-ingress-controller/v2/internal/util/kubernetes/object" "github.com/kong/kubernetes-ingress-controller/v2/internal/util/kubernetes/object/status" ) @@ -391,7 +392,7 @@ func (c *KongClient) Update(ctx context.Context) error { defer c.lock.Unlock() // If Kong is running in dbless mode, we can fetch and store the last good configuration. - if c.dbmode == "" || c.dbmode == "off" { + if dataplane.IsDBLessMode(c.dbmode) { // Fetch the last valid configuration from the proxy only in case there is no valid // configuration already stored in memory. This can happen when KIC restarts and there // already is a Kong Proxy with a valid configuration loaded. diff --git a/internal/dataplane/synchronizer.go b/internal/dataplane/synchronizer.go index df55928a3ee..f23836bdd41 100644 --- a/internal/dataplane/synchronizer.go +++ b/internal/dataplane/synchronizer.go @@ -9,6 +9,7 @@ import ( "github.com/bombsimon/logrusr/v2" "github.com/go-logr/logr" + "github.com/kong/kubernetes-ingress-controller/v2/internal/util/dataplane" "github.com/sirupsen/logrus" ) @@ -140,7 +141,7 @@ func (p *Synchronizer) IsReady() bool { defer p.lock.RUnlock() // If the proxy is has no database, it is only ready after a successful sync // Otherwise, it has no configuration loaded - if p.dbMode == "off" { + if dataplane.IsDBLessMode(p.dbMode) { return p.configApplied } // If the proxy has a database, it is ready immediately diff --git a/internal/manager/config_validation.go b/internal/manager/config_validation.go index 2533ece44a4..93a90c87748 100644 --- a/internal/manager/config_validation.go +++ b/internal/manager/config_validation.go @@ -11,6 +11,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v2/internal/adminapi" cfgtypes "github.com/kong/kubernetes-ingress-controller/v2/internal/manager/config/types" + "github.com/kong/kubernetes-ingress-controller/v2/internal/util/dataplane" ) // https://github.com/kubernetes-sigs/gateway-api/blob/547122f7f55ac0464685552898c560658fb40073/apis/v1beta1/shared_types.go#L448-L463 @@ -131,7 +132,7 @@ func (c *Config) ValidateGatewayDiscovery(dbMode string) error { if c.KongAdminSvc.IsAbsent() { return nil } - if dbMode != "off" && dbMode != "" { + if !dataplane.IsDBLessMode(dbMode) { return fmt.Errorf("gateway discovery is only supported in dbless mode, not db %s", dbMode) } return nil diff --git a/internal/manager/run.go b/internal/manager/run.go index 82d36ac57d4..fee175d21ec 100644 --- a/internal/manager/run.go +++ b/internal/manager/run.go @@ -35,6 +35,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v2/internal/manager/utils/kongconfig" "github.com/kong/kubernetes-ingress-controller/v2/internal/store" "github.com/kong/kubernetes-ingress-controller/v2/internal/util" + dataplaneutil "github.com/kong/kubernetes-ingress-controller/v2/internal/util/dataplane" "github.com/kong/kubernetes-ingress-controller/v2/internal/util/kubernetes/object/status" "github.com/kong/kubernetes-ingress-controller/v2/internal/versions" ) @@ -109,7 +110,7 @@ func Run(ctx context.Context, c *Config, diagnostic util.ConfigDumpDiagnostic, d kongConfig := sendconfig.Config{ Version: kongSemVersion, - InMemory: (dbMode == "off") || (dbMode == ""), + InMemory: dataplaneutil.IsDBLessMode(dbMode), Concurrency: c.Concurrency, FilterTags: c.FilterTags, SkipCACertificates: c.SkipCACertificates, diff --git a/internal/manager/setup.go b/internal/manager/setup.go index 1b7e45219a7..81fba46fbd2 100644 --- a/internal/manager/setup.go +++ b/internal/manager/setup.go @@ -26,6 +26,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v2/internal/dataplane" "github.com/kong/kubernetes-ingress-controller/v2/internal/manager/scheme" "github.com/kong/kubernetes-ingress-controller/v2/internal/util" + dataplaneutil "github.com/kong/kubernetes-ingress-controller/v2/internal/util/dataplane" ) // ----------------------------------------------------------------------------- @@ -109,7 +110,7 @@ func leaderElectionEnabled(logger logr.Logger, c *Config, dbmode string) bool { return true } - if dbmode == "off" { + if dataplaneutil.IsDBLessMode(dbmode) { if c.KongAdminSvc.IsPresent() { logger.Info("DB-less mode detected with service detection, enabling leader election") return true diff --git a/internal/util/dataplane/mode.go b/internal/util/dataplane/mode.go new file mode 100644 index 00000000000..6a6175fd07c --- /dev/null +++ b/internal/util/dataplane/mode.go @@ -0,0 +1,6 @@ +package dataplane + +// IsDBLessMode can be used to detect the proxy mode (db or dbless) +func IsDBLessMode(mode string) bool { + return mode == "" || mode == "off" +}