diff --git a/.golangci.yaml b/.golangci.yaml index 3f635b9470..ca64847537 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -84,6 +84,8 @@ linters-settings: alias: gateway${1} - pkg: github.com/kong/kubernetes-ingress-controller/v2/pkg/apis/configuration/(v[\w\d]+) alias: kong${1} + - pkg: github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config + alias: dpconf forbidigo: exclude-godoc-examples: false forbid: diff --git a/internal/clients/manager.go b/internal/clients/manager.go index 75a2461b77..51fb945c9f 100644 --- a/internal/clients/manager.go +++ b/internal/clients/manager.go @@ -11,9 +11,9 @@ import ( "golang.org/x/exp/maps" "github.com/kong/kubernetes-ingress-controller/v3/internal/adminapi" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/internal/util" "github.com/kong/kubernetes-ingress-controller/v3/internal/util/clock" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" ) // DefaultReadinessReconciliationInterval is the interval at which the manager will run readiness reconciliation loop. @@ -50,7 +50,7 @@ type AdminAPIClientsManager struct { discoveredAdminAPIsNotifyChan chan []adminapi.DiscoveredAdminAPI gatewayClientsChangesSubscribers []chan struct{} - dbMode string + dbMode dpconf.DBMode ctx context.Context onceNotifyLoopRunning sync.Once @@ -90,7 +90,7 @@ func WithReadinessReconciliationTicker(ticker Ticker) AdminAPIClientsManagerOpti } // WithDBMode allows to set the DBMode of the Kong gateway instances behind the admin API service. -func (c *AdminAPIClientsManager) WithDBMode(dbMode string) *AdminAPIClientsManager { +func (c *AdminAPIClientsManager) WithDBMode(dbMode dpconf.DBMode) *AdminAPIClientsManager { c.dbMode = dbMode return c } @@ -194,7 +194,7 @@ func (c *AdminAPIClientsManager) GatewayClientsToConfigure() []*adminapi.Client defer c.lock.RUnlock() readyGatewayClients := lo.Values(c.readyGatewayClients) // With DB-less mode, we should send the configuration to ALL gateway instances. - if dataplaneutil.IsDBLessMode(c.dbMode) { + if c.dbMode.IsDBLessMode() { return readyGatewayClients } // When a gateway is DB-backed, we return a random client diff --git a/internal/dataplane/client.go b/internal/dataplane/client.go index 41765bc343..48d651527d 100644 --- a/internal/dataplane/client.go +++ b/internal/dataplane/client.go @@ -2,6 +2,8 @@ package dataplane import ( "context" + + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" ) // ----------------------------------------------------------------------------- @@ -24,7 +26,7 @@ const ( type Client interface { // DBMode informs the caller which DB mode the data-plane has employed // (e.g. "off" (dbless) or "postgres"). - DBMode() string + DBMode() dpconf.DBMode // Update the data-plane by parsing the current configuring and applying // it to the backend API. diff --git a/internal/dataplane/config/dbmode.go b/internal/dataplane/config/dbmode.go new file mode 100644 index 0000000000..1ffbe5477e --- /dev/null +++ b/internal/dataplane/config/dbmode.go @@ -0,0 +1,31 @@ +package config + +import "fmt" + +type DBMode string + +const ( + DBModeOff DBMode = "off" + DBModePostgres DBMode = "postgres" +) + +func NewDBMode(mode string) (DBMode, error) { + switch mode { + case "", string(DBModeOff): + return DBModeOff, nil + case string(DBModePostgres): + return DBModePostgres, nil + } + return "", fmt.Errorf("unsupported db mode: %q", mode) +} + +// IsDBLessMode can be used to detect the proxy mode (db or dbless). +func (m DBMode) IsDBLessMode() bool { + return m == "" || m == DBModeOff +} + +// IsDBBacked returns true if the gateway is DB backed. +// reverse of IsDBLessMode for readability. +func (m DBMode) IsDBBacked() bool { + return !m.IsDBLessMode() +} diff --git a/internal/dataplane/config/router.go b/internal/dataplane/config/router.go new file mode 100644 index 0000000000..96c9a30e5e --- /dev/null +++ b/internal/dataplane/config/router.go @@ -0,0 +1,13 @@ +package config + +type RouterFlavor string + +const ( + RouterFlavorTraditional RouterFlavor = "traditional" + RouterFlavorTraditionalCompatible RouterFlavor = "traditional_compatible" + RouterFlavorExpressions RouterFlavor = "expressions" +) + +func ShouldEnableExpressionRoutes(rf RouterFlavor) bool { + return rf == RouterFlavorExpressions +} diff --git a/internal/dataplane/kong_client.go b/internal/dataplane/kong_client.go index 06b824eb2a..ff40e345ad 100644 --- a/internal/dataplane/kong_client.go +++ b/internal/dataplane/kong_client.go @@ -25,6 +25,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/adminapi" "github.com/kong/kubernetes-ingress-controller/v3/internal/clients" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/configfetcher" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/deckgen" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/failures" @@ -34,7 +35,6 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/metrics" "github.com/kong/kubernetes-ingress-controller/v3/internal/store" "github.com/kong/kubernetes-ingress-controller/v3/internal/util" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" k8sobj "github.com/kong/kubernetes-ingress-controller/v3/internal/util/kubernetes/object" "github.com/kong/kubernetes-ingress-controller/v3/internal/util/kubernetes/object/status" ) @@ -75,7 +75,7 @@ type KongClient struct { kongConfig sendconfig.Config // dbmode indicates the current database mode of the backend Kong Admin API - dbmode string + dbmode dpconf.DBMode // lock is used to ensure threadsafety of the KongClient object lock sync.RWMutex @@ -150,7 +150,7 @@ func NewKongClient( diagnostic util.ConfigDumpDiagnostic, kongConfig sendconfig.Config, eventRecorder record.EventRecorder, - dbMode string, + dbMode dpconf.DBMode, clientsProvider clients.AdminAPIClientsProvider, updateStrategyResolver sendconfig.UpdateStrategyResolver, configChangeDetector sendconfig.ConfigurationChangeDetector, @@ -368,7 +368,7 @@ func (c *KongClient) KubernetesObjectConfigurationStatus(obj client.Object) k8so // ----------------------------------------------------------------------------- // DBMode indicates which database the Kong Gateway is using. -func (c *KongClient) DBMode() string { +func (c *KongClient) DBMode() dpconf.DBMode { c.lock.RLock() defer c.lock.RUnlock() return c.dbmode @@ -382,7 +382,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 dataplaneutil.IsDBLessMode(c.dbmode) { + if c.dbmode.IsDBLessMode() { // 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. @@ -478,7 +478,7 @@ func (c *KongClient) sendOutToGatewayClients( // since only ONE gateway client is chosen to send requests and store SHA of latest configurations, // we should propagate the SHA from the chosen client to other clients // as well as they will pick the configuration from the shared database. - if dataplaneutil.DBBacked(c.dbmode) && + if c.dbmode.IsDBBacked() && len(gatewayClients) > 1 { for _, client := range gatewayClients { client.SetLastConfigSHA([]byte(shas[0])) diff --git a/internal/dataplane/kong_client_test.go b/internal/dataplane/kong_client_test.go index 73ad66480f..c74c112b8a 100644 --- a/internal/dataplane/kong_client_test.go +++ b/internal/dataplane/kong_client_test.go @@ -29,6 +29,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/adminapi" "github.com/kong/kubernetes-ingress-controller/v3/internal/clients" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/configfetcher" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/deckgen" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/failures" @@ -38,7 +39,6 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/metrics" "github.com/kong/kubernetes-ingress-controller/v3/internal/store" "github.com/kong/kubernetes-ingress-controller/v3/internal/util" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" "github.com/kong/kubernetes-ingress-controller/v3/internal/versions" "github.com/kong/kubernetes-ingress-controller/v3/test/mocks" ) @@ -144,7 +144,7 @@ var ( type mockGatewayClientsProvider struct { gatewayClients []*adminapi.Client konnectClient *adminapi.KonnectClient - dbMode string + dbMode dpconf.DBMode } func (p mockGatewayClientsProvider) KonnectClient() *adminapi.KonnectClient { @@ -156,7 +156,7 @@ func (p mockGatewayClientsProvider) GatewayClients() []*adminapi.Client { } func (p mockGatewayClientsProvider) GatewayClientsToConfigure() []*adminapi.Client { - if dataplaneutil.IsDBLessMode(p.dbMode) { + if p.dbMode.IsDBLessMode() { return p.gatewayClients } if len(p.gatewayClients) == 0 { @@ -681,7 +681,6 @@ func setupTestKongClient( timeout := time.Second diagnostic := util.ConfigDumpDiagnostic{} config := sendconfig.Config{} - dbMode := "off" if eventRecorder == nil { eventRecorder = mocks.NewEventRecorder() @@ -693,7 +692,7 @@ func setupTestKongClient( diagnostic, config, eventRecorder, - dbMode, + dpconf.DBModeOff, clientsProvider, updateStrategyResolver, configChangeDetector, diff --git a/internal/dataplane/synchronizer.go b/internal/dataplane/synchronizer.go index ad3fd7cb93..49f3f10944 100644 --- a/internal/dataplane/synchronizer.go +++ b/internal/dataplane/synchronizer.go @@ -9,7 +9,7 @@ import ( "github.com/go-logr/logr" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" ) // ----------------------------------------------------------------------------- @@ -42,7 +42,7 @@ type Synchronizer struct { // dataplane client to send updates to the Kong Admin API dataplaneClient Client - dbMode string + dbMode dpconf.DBMode // server configuration, flow control, channels and utility attributes stagger time.Duration @@ -140,7 +140,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 dataplaneutil.IsDBLessMode(p.dbMode) { + if p.dbMode.IsDBLessMode() { return p.configApplied } // If the proxy has a database, it is ready immediately diff --git a/internal/dataplane/synchronizer_test.go b/internal/dataplane/synchronizer_test.go index f87a1b5750..d47facb57b 100644 --- a/internal/dataplane/synchronizer_test.go +++ b/internal/dataplane/synchronizer_test.go @@ -12,6 +12,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" + + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" ) const testSynchronizerTick = time.Millisecond * 10 @@ -22,7 +24,7 @@ func TestSynchronizer(t *testing.T) { } t.Log("setting up a fake dataplane client to test the synchronizer") - c := &fakeDataplaneClient{dbmode: "postgres"} + c := &fakeDataplaneClient{dbmode: dpconf.DBModePostgres} t.Log("configuring the dataplane synchronizer") ctx, cancel := context.WithCancel(context.Background()) @@ -90,7 +92,10 @@ func TestSynchronizer(t *testing.T) { } func TestSynchronizer_IsReadyDoesntBlockWhenDataPlaneIsBlocked(t *testing.T) { - for _, dbMode := range []string{"off", "postgres"} { + for _, dbMode := range []dpconf.DBMode{ + dpconf.DBModeOff, + dpconf.DBModePostgres, + } { dbMode := dbMode t.Run(fmt.Sprintf("dbmode=%s", dbMode), func(t *testing.T) { c := &fakeDataplaneClient{dbmode: dbMode, t: t} @@ -126,14 +131,14 @@ func TestSynchronizer_IsReadyDoesntBlockWhenDataPlaneIsBlocked(t *testing.T) { // fakeDataplaneClient fakes the dataplane.Client interface so that we can // unit test the dataplane.Synchronizer. type fakeDataplaneClient struct { - dbmode string + dbmode dpconf.DBMode updateCount atomic.Uint64 lock sync.RWMutex clientCallBlockDuration time.Duration t *testing.T } -func (c *fakeDataplaneClient) DBMode() string { +func (c *fakeDataplaneClient) DBMode() dpconf.DBMode { c.lock.RLock() defer c.lock.RUnlock() if c.clientCallBlockDuration > 0 { diff --git a/internal/dataplane/translator/translator.go b/internal/dataplane/translator/translator.go index 16a686d302..50b9e2b25c 100644 --- a/internal/dataplane/translator/translator.go +++ b/internal/dataplane/translator/translator.go @@ -20,6 +20,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "github.com/kong/kubernetes-ingress-controller/v3/internal/annotations" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/failures" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/kongstate" "github.com/kong/kubernetes-ingress-controller/v3/internal/gatewayapi" @@ -34,13 +35,7 @@ import ( // Translator - Public Constants and Package Variables // ----------------------------------------------------------------------------- -const ( - KindGateway = gatewayapi.Kind("Gateway") - - // kongRouterFlavorExpressions is the value used in router_flavor of kong configuration - // to enable expression based router of kong. - kongRouterFlavorExpressions = "expressions" -) +const KindGateway = gatewayapi.Kind("Gateway") // ----------------------------------------------------------------------------- // Translator - Public Types @@ -65,31 +60,18 @@ type FeatureFlags struct { } func NewFeatureFlags( - logger logr.Logger, featureGates featuregates.FeatureGates, - routerFlavor string, + routerFlavor dpconf.RouterFlavor, updateStatusFlag bool, ) FeatureFlags { return FeatureFlags{ ReportConfiguredKubernetesObjects: updateStatusFlag, - ExpressionRoutes: shouldEnableTranslatorExpressionRoutes(logger, routerFlavor), + ExpressionRoutes: dpconf.ShouldEnableExpressionRoutes(routerFlavor), FillIDs: featureGates.Enabled(featuregates.FillIDsFeature), RewriteURIs: featureGates.Enabled(featuregates.RewriteURIsFeature), } } -func shouldEnableTranslatorExpressionRoutes( - logger logr.Logger, - routerFlavor string, -) bool { - if routerFlavor != kongRouterFlavorExpressions { - logger.V(util.InfoLevel).Info("Gateway is running with non-expression router flavor", "flavor", routerFlavor) - return false - } - logger.V(util.InfoLevel).Info("The expression routes mode enabled") - return true -} - // LicenseGetter is an interface for getting the Kong Enterprise license. type LicenseGetter interface { // GetLicense returns an optional license. diff --git a/internal/dataplane/translator/translator_test.go b/internal/dataplane/translator/translator_test.go index 84039c5dc3..f344023a11 100644 --- a/internal/dataplane/translator/translator_test.go +++ b/internal/dataplane/translator/translator_test.go @@ -14,8 +14,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "go.uber.org/zap/zaptest/observer" corev1 "k8s.io/api/core/v1" discoveryv1 "k8s.io/api/discovery/v1" netv1 "k8s.io/api/networking/v1" @@ -26,6 +24,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "github.com/kong/kubernetes-ingress-controller/v3/internal/annotations" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/kongstate" "github.com/kong/kubernetes-ingress-controller/v3/internal/store" "github.com/kong/kubernetes-ingress-controller/v3/internal/util" @@ -4505,45 +4504,35 @@ func TestNewFeatureFlags(t *testing.T) { name string featureGates map[string]bool - routerFlavor string + routerFlavor dpconf.RouterFlavor updateStatusFlag bool expectedFeatureFlags FeatureFlags - expectInfoLog string }{ { - name: "default", - featureGates: map[string]bool{}, - routerFlavor: "traditional", + name: "traditional compatible router and update status enabled", + featureGates: map[string]bool{}, + + routerFlavor: dpconf.RouterFlavorTraditionalCompatible, updateStatusFlag: true, expectedFeatureFlags: FeatureFlags{ ReportConfiguredKubernetesObjects: true, }, }, { - name: "expression routes feature gate enabled and router flavor matches", - routerFlavor: kongRouterFlavorExpressions, + name: "expression router and update status disabled", + routerFlavor: dpconf.RouterFlavorExpressions, expectedFeatureFlags: FeatureFlags{ ExpressionRoutes: true, }, - expectInfoLog: "The expression routes mode enabled", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - core, logs := observer.New(zap.InfoLevel) - logger := zapr.NewLogger(zap.New(core)) - actualFlags := NewFeatureFlags(logger, tc.featureGates, tc.routerFlavor, tc.updateStatusFlag) + actualFlags := NewFeatureFlags(tc.featureGates, tc.routerFlavor, tc.updateStatusFlag) require.Equal(t, tc.expectedFeatureFlags, actualFlags) - - if tc.expectInfoLog != "" { - lastEntry := logs.All()[logs.Len()-1] - require.NotNil(t, lastEntry) - require.Equal(t, zapcore.InfoLevel, lastEntry.Level) - require.Equal(t, tc.expectInfoLog, lastEntry.Message) - } }) } } diff --git a/internal/manager/run.go b/internal/manager/run.go index 5d8ba7b788..250be66ccb 100644 --- a/internal/manager/run.go +++ b/internal/manager/run.go @@ -22,6 +22,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/clients" "github.com/kong/kubernetes-ingress-controller/v3/internal/controllers/gateway" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/configfetcher" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/sendconfig" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/translator" @@ -36,7 +37,6 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/manager/utils/kongconfig" "github.com/kong/kubernetes-ingress-controller/v3/internal/store" "github.com/kong/kubernetes-ingress-controller/v3/internal/util" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" "github.com/kong/kubernetes-ingress-controller/v3/internal/util/kubernetes/object/status" ) @@ -115,12 +115,12 @@ func Run( kongConfig := sendconfig.Config{ Version: kongSemVersion, - InMemory: dataplaneutil.IsDBLessMode(dbMode), + InMemory: dbMode.IsDBLessMode(), Concurrency: c.Concurrency, FilterTags: c.FilterTags, SkipCACertificates: c.SkipCACertificates, EnableReverseSync: c.EnableReverseSync, - ExpressionRoutes: routerFlavor == "expressions", + ExpressionRoutes: dpconf.ShouldEnableExpressionRoutes(routerFlavor), } kongConfig.Init(ctx, setupLog, initialKongClients) @@ -160,7 +160,6 @@ func Run( } translatorFeatureFlags := translator.NewFeatureFlags( - logger, featureGates, routerFlavor, c.UpdateStatus, diff --git a/internal/manager/setup.go b/internal/manager/setup.go index f7e6844384..eee1053ebc 100644 --- a/internal/manager/setup.go +++ b/internal/manager/setup.go @@ -26,10 +26,10 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/admission" "github.com/kong/kubernetes-ingress-controller/v3/internal/clients" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/translator" "github.com/kong/kubernetes-ingress-controller/v3/internal/manager/scheme" "github.com/kong/kubernetes-ingress-controller/v3/internal/util" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" ) // ----------------------------------------------------------------------------- @@ -56,7 +56,7 @@ func SetupLoggers(c *Config, output io.Writer) (logr.Logger, error) { return logger, nil } -func setupManagerOptions(ctx context.Context, logger logr.Logger, c *Config, dbmode string) (ctrl.Options, error) { +func setupManagerOptions(ctx context.Context, logger logr.Logger, c *Config, dbmode dpconf.DBMode) (ctrl.Options, error) { logger.Info("Building the manager runtime scheme and loading apis into the scheme") scheme, err := scheme.Get() if err != nil { @@ -113,13 +113,13 @@ func setupManagerOptions(ctx context.Context, logger logr.Logger, c *Config, dbm return managerOpts, nil } -func leaderElectionEnabled(logger logr.Logger, c *Config, dbmode string) bool { +func leaderElectionEnabled(logger logr.Logger, c *Config, dbmode dpconf.DBMode) bool { if c.Konnect.ConfigSynchronizationEnabled { logger.Info("Konnect config synchronisation enabled, enabling leader election") return true } - if dataplaneutil.IsDBLessMode(dbmode) { + if dbmode.IsDBLessMode() { if c.KongAdminSvc.IsPresent() { logger.Info("DB-less mode detected with service detection, enabling leader election") return true diff --git a/internal/manager/utils/kongconfig/root.go b/internal/manager/utils/kongconfig/root.go index 208143052a..6579d45804 100644 --- a/internal/manager/utils/kongconfig/root.go +++ b/internal/manager/utils/kongconfig/root.go @@ -14,13 +14,14 @@ import ( "golang.org/x/sync/errgroup" "github.com/kong/kubernetes-ingress-controller/v3/internal/adminapi" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" ) // KongStartUpOptions includes start up configurations of Kong that could change behavior of Kong Ingress Controller. // The fields are extracted from results of Kong gateway configuration root. type KongStartUpOptions struct { - DBMode string - RouterFlavor string + DBMode dpconf.DBMode + RouterFlavor dpconf.RouterFlavor Version kong.Version } @@ -75,7 +76,7 @@ func extractConfigurationFromRoot(r Root) (map[string]any, error) { return rootConfig, nil } -func DBModeFromRoot(r Root) (string, error) { +func DBModeFromRoot(r Root) (dpconf.DBMode, error) { rootConfig, err := extractConfigurationFromRoot(r) if err != nil { return "", err @@ -91,10 +92,10 @@ func DBModeFromRoot(r Root) (string, error) { return "", fmt.Errorf("invalid %q type, expected a string, got %T", dbModeKey, dbMode) } - return dbModeStr, nil + return dpconf.NewDBMode(dbModeStr) } -func RouterFlavorFromRoot(r Root) (string, error) { +func RouterFlavorFromRoot(r Root) (dpconf.RouterFlavor, error) { rootConfig, err := extractConfigurationFromRoot(r) if err != nil { return "", err @@ -109,7 +110,7 @@ func RouterFlavorFromRoot(r Root) (string, error) { if !ok { return "", fmt.Errorf("invalid %q type, expected a string, got %T", routerFlavorKey, routerFlavor) } - return routerFlavorStr, nil + return dpconf.RouterFlavor(routerFlavorStr), nil } func KongVersionFromRoot(r Root) (kong.Version, error) { @@ -151,7 +152,7 @@ func (kr Root) Key(skipCACerts bool) string { return "" } - return dbMode + return string(dbMode) } func validateRootFunc(skipCACerts bool) func(Root, int) error { @@ -170,16 +171,14 @@ func getRootKeyFunc(skipCACerts bool) func(Root) string { } // validateDBMode validates the provided dbMode string. -func validateDBMode(dbMode string, skipCACerts bool) error { +func validateDBMode(dbMode dpconf.DBMode, skipCACerts bool) error { switch dbMode { - case "off", "": + case "", dpconf.DBModeOff: if skipCACerts { return fmt.Errorf("--skip-ca-certificates is not available for use with DB-less Kong instances") } - case "postgres": + case dpconf.DBModePostgres: return nil - case "cassandra": - return fmt.Errorf("Cassandra-backed deployments of Kong managed by the ingress controller are no longer supported; you must migrate to a Postgres-backed or DB-less deployment") default: return fmt.Errorf("%s is not a supported database backend", dbMode) } diff --git a/internal/manager/utils/kongconfig/root_test.go b/internal/manager/utils/kongconfig/root_test.go index 1c8bd02383..705623a662 100644 --- a/internal/manager/utils/kongconfig/root_test.go +++ b/internal/manager/utils/kongconfig/root_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/internal/versions" ) @@ -21,15 +22,15 @@ func TestValidateRoots(t *testing.T) { testCases := []struct { name string configStr string - expectedDBMode string - expectedRouterFlavor string + expectedDBMode dpconf.DBMode + expectedRouterFlavor dpconf.RouterFlavor expectedKongVersion string }{ { name: "dbless config with version 3.4.1", configStr: dblessConfigJSON3_4_1, - expectedDBMode: "off", - expectedRouterFlavor: "traditional_compatible", + expectedDBMode: dpconf.DBModeOff, + expectedRouterFlavor: dpconf.RouterFlavorTraditionalCompatible, expectedKongVersion: versions.KICv3VersionCutoff.String(), }, } diff --git a/internal/util/dataplane/mode.go b/internal/util/dataplane/mode.go deleted file mode 100644 index 6aabc12b7b..0000000000 --- a/internal/util/dataplane/mode.go +++ /dev/null @@ -1,12 +0,0 @@ -package dataplane - -// IsDBLessMode can be used to detect the proxy mode (db or dbless). -func IsDBLessMode(mode string) bool { - return mode == "" || mode == "off" -} - -// DBBacked returns true if the gateway is DB backed. -// reverse of IsDBLessMode for readability. -func DBBacked(mode string) bool { - return !IsDBLessMode(mode) -} diff --git a/test/integration/version_test.go b/test/integration/version_test.go index 16b660b917..81301c19f8 100644 --- a/test/integration/version_test.go +++ b/test/integration/version_test.go @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/test" "github.com/kong/kubernetes-ingress-controller/v3/test/consts" "github.com/kong/kubernetes-ingress-controller/v3/test/internal/helpers" @@ -38,7 +39,7 @@ func RunWhenKongVersion(t *testing.T, vRangeStr string, msg ...any) { } } -func RunWhenKongDBMode(t *testing.T, dbmode string, msg ...any) { +func RunWhenKongDBMode(t *testing.T, dbmode dpconf.DBMode, msg ...any) { t.Helper() actual := eventuallyGetKongDBMode(t, proxyAdminURL) @@ -84,12 +85,12 @@ func eventuallyGetKongVersion(t *testing.T, adminURL *url.URL) kong.Version { return version } -func eventuallyGetKongDBMode(t *testing.T, adminURL *url.URL) string { +func eventuallyGetKongDBMode(t *testing.T, adminURL *url.URL) dpconf.DBMode { t.Helper() var ( err error - dbmode string + dbmode dpconf.DBMode ) require.EventuallyWithT(t, func(t *assert.CollectT) { @@ -101,12 +102,12 @@ func eventuallyGetKongDBMode(t *testing.T, adminURL *url.URL) string { return dbmode } -func eventuallyGetKongRouterFlavor(t *testing.T, adminURL *url.URL) string { +func eventuallyGetKongRouterFlavor(t *testing.T, adminURL *url.URL) dpconf.RouterFlavor { t.Helper() var ( err error - routerFlavor string + routerFlavor dpconf.RouterFlavor ) require.EventuallyWithT(t, func(t *assert.CollectT) { diff --git a/test/internal/helpers/kong.go b/test/internal/helpers/kong.go index 66291dd75a..51b879dc73 100644 --- a/test/internal/helpers/kong.go +++ b/test/internal/helpers/kong.go @@ -11,6 +11,7 @@ import ( "github.com/samber/lo" "github.com/kong/kubernetes-ingress-controller/v3/internal/adminapi" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/internal/manager/utils/kongconfig" "github.com/kong/kubernetes-ingress-controller/v3/internal/versions" ) @@ -73,7 +74,7 @@ func (e TooOldKongGatewayError) Error() string { } // GetKongDBMode returns kong dbmode using the provided Admin API URL. -func GetKongDBMode(ctx context.Context, proxyAdminURL *url.URL, kongTestPassword string) (string, error) { +func GetKongDBMode(ctx context.Context, proxyAdminURL *url.URL, kongTestPassword string) (dpconf.DBMode, error) { jsonResp, err := GetKongRootConfig(ctx, proxyAdminURL, kongTestPassword) if err != nil { return "", err @@ -86,7 +87,7 @@ func GetKongDBMode(ctx context.Context, proxyAdminURL *url.URL, kongTestPassword } // GetKongRouterFlavor gets router flavor of Kong using the provided Admin API URL. -func GetKongRouterFlavor(ctx context.Context, proxyAdminURL *url.URL, kongTestPassword string) (string, error) { +func GetKongRouterFlavor(ctx context.Context, proxyAdminURL *url.URL, kongTestPassword string) (dpconf.RouterFlavor, error) { jsonResp, err := GetKongRootConfig(ctx, proxyAdminURL, kongTestPassword) if err != nil { return "", err