From 9cc0fad41d140f1254ed2b5807581fc1ce0357e3 Mon Sep 17 00:00:00 2001 From: Yi Tao Date: Mon, 6 Nov 2023 17:18:00 +0800 Subject: [PATCH 1/8] define DBMode as a dedicated type --- internal/clients/manager.go | 4 ++-- internal/dataplane/client.go | 4 +++- internal/dataplane/kong_client.go | 6 +++--- internal/dataplane/kong_client_test.go | 5 ++--- internal/dataplane/synchronizer.go | 2 +- internal/dataplane/synchronizer_test.go | 13 +++++++++---- internal/manager/setup.go | 4 ++-- internal/manager/utils/kongconfig/root.go | 15 ++++++++------- internal/util/dataplane/mode.go | 14 +++++++++++--- test/integration/version_test.go | 7 ++++--- test/internal/helpers/kong.go | 3 ++- 11 files changed, 47 insertions(+), 30 deletions(-) diff --git a/internal/clients/manager.go b/internal/clients/manager.go index 75a2461b77..79db56b42d 100644 --- a/internal/clients/manager.go +++ b/internal/clients/manager.go @@ -50,7 +50,7 @@ type AdminAPIClientsManager struct { discoveredAdminAPIsNotifyChan chan []adminapi.DiscoveredAdminAPI gatewayClientsChangesSubscribers []chan struct{} - dbMode string + dbMode dataplaneutil.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 dataplaneutil.DBMode) *AdminAPIClientsManager { c.dbMode = dbMode return c } diff --git a/internal/dataplane/client.go b/internal/dataplane/client.go index 41765bc343..4609ee6b1f 100644 --- a/internal/dataplane/client.go +++ b/internal/dataplane/client.go @@ -2,6 +2,8 @@ package dataplane import ( "context" + + "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" ) // ----------------------------------------------------------------------------- @@ -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() dataplane.DBMode // Update the data-plane by parsing the current configuring and applying // it to the backend API. diff --git a/internal/dataplane/kong_client.go b/internal/dataplane/kong_client.go index 06b824eb2a..b257b72bf2 100644 --- a/internal/dataplane/kong_client.go +++ b/internal/dataplane/kong_client.go @@ -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 dataplaneutil.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 dataplaneutil.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() dataplaneutil.DBMode { c.lock.RLock() defer c.lock.RUnlock() return c.dbmode diff --git a/internal/dataplane/kong_client_test.go b/internal/dataplane/kong_client_test.go index 73ad66480f..8f1a79cf52 100644 --- a/internal/dataplane/kong_client_test.go +++ b/internal/dataplane/kong_client_test.go @@ -144,7 +144,7 @@ var ( type mockGatewayClientsProvider struct { gatewayClients []*adminapi.Client konnectClient *adminapi.KonnectClient - dbMode string + dbMode dataplaneutil.DBMode } func (p mockGatewayClientsProvider) KonnectClient() *adminapi.KonnectClient { @@ -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, + dataplaneutil.DBModeOff, clientsProvider, updateStrategyResolver, configChangeDetector, diff --git a/internal/dataplane/synchronizer.go b/internal/dataplane/synchronizer.go index ad3fd7cb93..33bc51f751 100644 --- a/internal/dataplane/synchronizer.go +++ b/internal/dataplane/synchronizer.go @@ -42,7 +42,7 @@ type Synchronizer struct { // dataplane client to send updates to the Kong Admin API dataplaneClient Client - dbMode string + dbMode dataplaneutil.DBMode // server configuration, flow control, channels and utility attributes stagger time.Duration diff --git a/internal/dataplane/synchronizer_test.go b/internal/dataplane/synchronizer_test.go index f87a1b5750..e183a24c78 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" + + dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" ) 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: dataplaneutil.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 []dataplaneutil.DBMode{ + dataplaneutil.DBModeOff, + dataplaneutil.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 dataplaneutil.DBMode updateCount atomic.Uint64 lock sync.RWMutex clientCallBlockDuration time.Duration t *testing.T } -func (c *fakeDataplaneClient) DBMode() string { +func (c *fakeDataplaneClient) DBMode() dataplaneutil.DBMode { c.lock.RLock() defer c.lock.RUnlock() if c.clientCallBlockDuration > 0 { diff --git a/internal/manager/setup.go b/internal/manager/setup.go index f7e6844384..d4eeca9d8f 100644 --- a/internal/manager/setup.go +++ b/internal/manager/setup.go @@ -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 dataplaneutil.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,7 +113,7 @@ 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 dataplaneutil.DBMode) bool { if c.Konnect.ConfigSynchronizationEnabled { logger.Info("Konnect config synchronisation enabled, enabling leader election") return true diff --git a/internal/manager/utils/kongconfig/root.go b/internal/manager/utils/kongconfig/root.go index 208143052a..4bf25a8805 100644 --- a/internal/manager/utils/kongconfig/root.go +++ b/internal/manager/utils/kongconfig/root.go @@ -14,12 +14,13 @@ import ( "golang.org/x/sync/errgroup" "github.com/kong/kubernetes-ingress-controller/v3/internal/adminapi" + dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" ) // 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 + DBMode dataplaneutil.DBMode RouterFlavor string 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) (dataplaneutil.DBMode, error) { rootConfig, err := extractConfigurationFromRoot(r) if err != nil { return "", err @@ -91,7 +92,7 @@ func DBModeFromRoot(r Root) (string, error) { return "", fmt.Errorf("invalid %q type, expected a string, got %T", dbModeKey, dbMode) } - return dbModeStr, nil + return dataplaneutil.DBMode(dbModeStr), nil } func RouterFlavorFromRoot(r Root) (string, 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,13 +171,13 @@ func getRootKeyFunc(skipCACerts bool) func(Root) string { } // validateDBMode validates the provided dbMode string. -func validateDBMode(dbMode string, skipCACerts bool) error { +func validateDBMode(dbMode dataplaneutil.DBMode, skipCACerts bool) error { switch dbMode { - case "off", "": + case "", dataplaneutil.DBModeOff: if skipCACerts { return fmt.Errorf("--skip-ca-certificates is not available for use with DB-less Kong instances") } - case "postgres": + case dataplaneutil.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") diff --git a/internal/util/dataplane/mode.go b/internal/util/dataplane/mode.go index 6aabc12b7b..0a97ec9843 100644 --- a/internal/util/dataplane/mode.go +++ b/internal/util/dataplane/mode.go @@ -1,12 +1,20 @@ package dataplane +type DBMode string + +const ( + DBModeOff = "off" + DBModePostgres = "postgres" + DBModeCassandra = "cassandra" +) + // IsDBLessMode can be used to detect the proxy mode (db or dbless). -func IsDBLessMode(mode string) bool { - return mode == "" || mode == "off" +func IsDBLessMode(mode DBMode) bool { + return mode == "" || mode == DBModeOff } // DBBacked returns true if the gateway is DB backed. // reverse of IsDBLessMode for readability. -func DBBacked(mode string) bool { +func DBBacked(mode DBMode) bool { return !IsDBLessMode(mode) } diff --git a/test/integration/version_test.go b/test/integration/version_test.go index 16b660b917..855d11cdb7 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" + dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" "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 dataplaneutil.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) dataplaneutil.DBMode { t.Helper() var ( err error - dbmode string + dbmode dataplaneutil.DBMode ) require.EventuallyWithT(t, func(t *assert.CollectT) { diff --git a/test/internal/helpers/kong.go b/test/internal/helpers/kong.go index 66291dd75a..52cc64e57c 100644 --- a/test/internal/helpers/kong.go +++ b/test/internal/helpers/kong.go @@ -12,6 +12,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/adminapi" "github.com/kong/kubernetes-ingress-controller/v3/internal/manager/utils/kongconfig" + dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" "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) (dataplaneutil.DBMode, error) { jsonResp, err := GetKongRootConfig(ctx, proxyAdminURL, kongTestPassword) if err != nil { return "", err From ca27487185a368872a68dee8d7956beb32a7d04d Mon Sep 17 00:00:00 2001 From: Yi Tao Date: Mon, 6 Nov 2023 17:26:58 +0800 Subject: [PATCH 2/8] define router flavor as a dedicated type --- internal/dataplane/translator/translator.go | 12 +++------- .../dataplane/translator/translator_test.go | 22 +++++-------------- internal/manager/run.go | 2 +- internal/manager/utils/kongconfig/root.go | 6 ++--- internal/util/dataplane/mode.go | 12 ++++++++++ test/integration/version_test.go | 4 ++-- test/internal/helpers/kong.go | 2 +- 7 files changed, 27 insertions(+), 33 deletions(-) diff --git a/internal/dataplane/translator/translator.go b/internal/dataplane/translator/translator.go index 16a686d302..ed44130826 100644 --- a/internal/dataplane/translator/translator.go +++ b/internal/dataplane/translator/translator.go @@ -26,6 +26,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/manager/featuregates" "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" kongv1alpha1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1alpha1" kongv1beta1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1beta1" ) @@ -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,9 +60,8 @@ type FeatureFlags struct { } func NewFeatureFlags( - logger logr.Logger, featureGates featuregates.FeatureGates, - routerFlavor string, + routerFlavor dataplaneutil.RouterFlavor, updateStatusFlag bool, ) FeatureFlags { return FeatureFlags{ diff --git a/internal/dataplane/translator/translator_test.go b/internal/dataplane/translator/translator_test.go index 84039c5dc3..ddd2517e06 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" @@ -30,6 +28,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/store" "github.com/kong/kubernetes-ingress-controller/v3/internal/util" "github.com/kong/kubernetes-ingress-controller/v3/internal/util/builder" + dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" kongv1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1" kongv1beta1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1beta1" "github.com/kong/kubernetes-ingress-controller/v3/test/helpers/certificate" @@ -4505,16 +4504,15 @@ func TestNewFeatureFlags(t *testing.T) { name string featureGates map[string]bool - routerFlavor string + routerFlavor dataplaneutil.RouterFlavor updateStatusFlag bool expectedFeatureFlags FeatureFlags - expectInfoLog string }{ { name: "default", featureGates: map[string]bool{}, - routerFlavor: "traditional", + routerFlavor: dataplaneutil.RouterFlavorExpressions, updateStatusFlag: true, expectedFeatureFlags: FeatureFlags{ ReportConfiguredKubernetesObjects: true, @@ -4522,28 +4520,18 @@ func TestNewFeatureFlags(t *testing.T) { }, { name: "expression routes feature gate enabled and router flavor matches", - routerFlavor: kongRouterFlavorExpressions, + routerFlavor: dataplaneutil.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..7496102ee6 100644 --- a/internal/manager/run.go +++ b/internal/manager/run.go @@ -120,7 +120,7 @@ func Run( FilterTags: c.FilterTags, SkipCACertificates: c.SkipCACertificates, EnableReverseSync: c.EnableReverseSync, - ExpressionRoutes: routerFlavor == "expressions", + ExpressionRoutes: dataplaneutil.ShouldEnableExpressionRoutes(routerFlavor), } kongConfig.Init(ctx, setupLog, initialKongClients) diff --git a/internal/manager/utils/kongconfig/root.go b/internal/manager/utils/kongconfig/root.go index 4bf25a8805..0730f8198a 100644 --- a/internal/manager/utils/kongconfig/root.go +++ b/internal/manager/utils/kongconfig/root.go @@ -21,7 +21,7 @@ import ( // The fields are extracted from results of Kong gateway configuration root. type KongStartUpOptions struct { DBMode dataplaneutil.DBMode - RouterFlavor string + RouterFlavor dataplaneutil.RouterFlavor Version kong.Version } @@ -95,7 +95,7 @@ func DBModeFromRoot(r Root) (dataplaneutil.DBMode, error) { return dataplaneutil.DBMode(dbModeStr), nil } -func RouterFlavorFromRoot(r Root) (string, error) { +func RouterFlavorFromRoot(r Root) (dataplaneutil.RouterFlavor, error) { rootConfig, err := extractConfigurationFromRoot(r) if err != nil { return "", err @@ -110,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 dataplaneutil.RouterFlavor(routerFlavorStr), nil } func KongVersionFromRoot(r Root) (kong.Version, error) { diff --git a/internal/util/dataplane/mode.go b/internal/util/dataplane/mode.go index 0a97ec9843..9571e67e8a 100644 --- a/internal/util/dataplane/mode.go +++ b/internal/util/dataplane/mode.go @@ -18,3 +18,15 @@ func IsDBLessMode(mode DBMode) bool { func DBBacked(mode DBMode) bool { return !IsDBLessMode(mode) } + +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/test/integration/version_test.go b/test/integration/version_test.go index 855d11cdb7..7d71f93b84 100644 --- a/test/integration/version_test.go +++ b/test/integration/version_test.go @@ -102,12 +102,12 @@ func eventuallyGetKongDBMode(t *testing.T, adminURL *url.URL) dataplaneutil.DBMo return dbmode } -func eventuallyGetKongRouterFlavor(t *testing.T, adminURL *url.URL) string { +func eventuallyGetKongRouterFlavor(t *testing.T, adminURL *url.URL) dataplaneutil.RouterFlavor { t.Helper() var ( err error - routerFlavor string + routerFlavor dataplaneutil.RouterFlavor ) require.EventuallyWithT(t, func(t *assert.CollectT) { diff --git a/test/internal/helpers/kong.go b/test/internal/helpers/kong.go index 52cc64e57c..f7b779f5cf 100644 --- a/test/internal/helpers/kong.go +++ b/test/internal/helpers/kong.go @@ -87,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) (dataplaneutil.RouterFlavor, error) { jsonResp, err := GetKongRootConfig(ctx, proxyAdminURL, kongTestPassword) if err != nil { return "", err From eb78c09331432f5c8da76b79e0ed72b46566a323 Mon Sep 17 00:00:00 2001 From: Yi Tao Date: Tue, 7 Nov 2023 11:17:58 +0800 Subject: [PATCH 3/8] add NewDBMode and fix unit tests --- .../dataplane/translator/translator_test.go | 6 +++--- internal/manager/utils/kongconfig/root.go | 4 +--- internal/manager/utils/kongconfig/root_test.go | 9 +++++---- internal/util/dataplane/mode.go | 17 ++++++++++++++--- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/internal/dataplane/translator/translator_test.go b/internal/dataplane/translator/translator_test.go index ddd2517e06..3b81f3bee5 100644 --- a/internal/dataplane/translator/translator_test.go +++ b/internal/dataplane/translator/translator_test.go @@ -4510,16 +4510,16 @@ func TestNewFeatureFlags(t *testing.T) { expectedFeatureFlags FeatureFlags }{ { - name: "default", + name: "traditional compatible router and update status enabled", featureGates: map[string]bool{}, - routerFlavor: dataplaneutil.RouterFlavorExpressions, + routerFlavor: dataplaneutil.RouterFlavorTraditionalCompatible, updateStatusFlag: true, expectedFeatureFlags: FeatureFlags{ ReportConfiguredKubernetesObjects: true, }, }, { - name: "expression routes feature gate enabled and router flavor matches", + name: "expression router and update status disabled", routerFlavor: dataplaneutil.RouterFlavorExpressions, expectedFeatureFlags: FeatureFlags{ ExpressionRoutes: true, diff --git a/internal/manager/utils/kongconfig/root.go b/internal/manager/utils/kongconfig/root.go index 0730f8198a..024e3b3229 100644 --- a/internal/manager/utils/kongconfig/root.go +++ b/internal/manager/utils/kongconfig/root.go @@ -92,7 +92,7 @@ func DBModeFromRoot(r Root) (dataplaneutil.DBMode, error) { return "", fmt.Errorf("invalid %q type, expected a string, got %T", dbModeKey, dbMode) } - return dataplaneutil.DBMode(dbModeStr), nil + return dataplaneutil.NewDBMode(dbModeStr) } func RouterFlavorFromRoot(r Root) (dataplaneutil.RouterFlavor, error) { @@ -179,8 +179,6 @@ func validateDBMode(dbMode dataplaneutil.DBMode, skipCACerts bool) error { } case dataplaneutil.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..693d71971e 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" + dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" "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 dataplaneutil.DBMode + expectedRouterFlavor dataplaneutil.RouterFlavor expectedKongVersion string }{ { name: "dbless config with version 3.4.1", configStr: dblessConfigJSON3_4_1, - expectedDBMode: "off", - expectedRouterFlavor: "traditional_compatible", + expectedDBMode: dataplaneutil.DBModeOff, + expectedRouterFlavor: dataplaneutil.RouterFlavorTraditionalCompatible, expectedKongVersion: versions.KICv3VersionCutoff.String(), }, } diff --git a/internal/util/dataplane/mode.go b/internal/util/dataplane/mode.go index 9571e67e8a..69e48a3399 100644 --- a/internal/util/dataplane/mode.go +++ b/internal/util/dataplane/mode.go @@ -1,13 +1,24 @@ package dataplane +import "fmt" + type DBMode string const ( - DBModeOff = "off" - DBModePostgres = "postgres" - DBModeCassandra = "cassandra" + 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 IsDBLessMode(mode DBMode) bool { return mode == "" || mode == DBModeOff From 668715a471e5ba7b7e5b95672fdfc8a621931270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Burzy=C5=84ski?= Date: Wed, 8 Nov 2023 03:57:20 +0100 Subject: [PATCH 4/8] chore: move DBMode and RouterFlavor to internal/dataplane/config (#5104) - Moved DBMode and RouterFlavor enums from internal/util/dataplane to internal/dataplane/config. - Added a linter rule for internal/dataplane/config alias to be enforced to dpconf --- .golangci.yaml | 2 ++ internal/clients/manager.go | 8 +++---- internal/dataplane/client.go | 4 ++-- .../mode.go => dataplane/config/dbmode.go} | 22 +++++-------------- internal/dataplane/config/router.go | 13 +++++++++++ internal/dataplane/kong_client.go | 12 +++++----- internal/dataplane/kong_client_test.go | 8 +++---- internal/dataplane/synchronizer.go | 6 ++--- internal/dataplane/synchronizer_test.go | 14 ++++++------ internal/dataplane/translator/translator.go | 6 ++--- .../dataplane/translator/translator_test.go | 8 +++---- internal/manager/run.go | 6 ++--- internal/manager/setup.go | 8 +++---- internal/manager/utils/kongconfig/root.go | 20 ++++++++--------- .../manager/utils/kongconfig/root_test.go | 10 ++++----- test/integration/version_test.go | 12 +++++----- test/internal/helpers/kong.go | 6 ++--- 17 files changed, 84 insertions(+), 81 deletions(-) rename internal/{util/dataplane/mode.go => dataplane/config/dbmode.go} (51%) create mode 100644 internal/dataplane/config/router.go 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 79db56b42d..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 dataplaneutil.DBMode + 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 dataplaneutil.DBMode) *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 4609ee6b1f..48d651527d 100644 --- a/internal/dataplane/client.go +++ b/internal/dataplane/client.go @@ -3,7 +3,7 @@ package dataplane import ( "context" - "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" ) // ----------------------------------------------------------------------------- @@ -26,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() dataplane.DBMode + DBMode() dpconf.DBMode // Update the data-plane by parsing the current configuring and applying // it to the backend API. diff --git a/internal/util/dataplane/mode.go b/internal/dataplane/config/dbmode.go similarity index 51% rename from internal/util/dataplane/mode.go rename to internal/dataplane/config/dbmode.go index 69e48a3399..341d8d03a2 100644 --- a/internal/util/dataplane/mode.go +++ b/internal/dataplane/config/dbmode.go @@ -1,4 +1,4 @@ -package dataplane +package config import "fmt" @@ -20,24 +20,12 @@ func NewDBMode(mode string) (DBMode, error) { } // IsDBLessMode can be used to detect the proxy mode (db or dbless). -func IsDBLessMode(mode DBMode) bool { - return mode == "" || mode == DBModeOff +func (m DBMode) IsDBLessMode() bool { + return m == "" || m == DBModeOff } // DBBacked returns true if the gateway is DB backed. // reverse of IsDBLessMode for readability. -func DBBacked(mode DBMode) bool { - return !IsDBLessMode(mode) -} - -type RouterFlavor string - -const ( - RouterFlavorTraditional RouterFlavor = "traditional" - RouterFlavorTraditionalCompatible RouterFlavor = "traditional_compatible" - RouterFlavorExpressions RouterFlavor = "expressions" -) - -func ShouldEnableExpressionRoutes(rf RouterFlavor) bool { - return rf == RouterFlavorExpressions +func (m DBMode) DBBacked() 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 b257b72bf2..5c827e69a2 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 dataplaneutil.DBMode + 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 dataplaneutil.DBMode, + 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() dataplaneutil.DBMode { +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.DBBacked() && 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 8f1a79cf52..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 dataplaneutil.DBMode + 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 { @@ -692,7 +692,7 @@ func setupTestKongClient( diagnostic, config, eventRecorder, - dataplaneutil.DBModeOff, + dpconf.DBModeOff, clientsProvider, updateStrategyResolver, configChangeDetector, diff --git a/internal/dataplane/synchronizer.go b/internal/dataplane/synchronizer.go index 33bc51f751..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 dataplaneutil.DBMode + 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 e183a24c78..d47facb57b 100644 --- a/internal/dataplane/synchronizer_test.go +++ b/internal/dataplane/synchronizer_test.go @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/require" "go.uber.org/zap" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" ) const testSynchronizerTick = time.Millisecond * 10 @@ -24,7 +24,7 @@ func TestSynchronizer(t *testing.T) { } t.Log("setting up a fake dataplane client to test the synchronizer") - c := &fakeDataplaneClient{dbmode: dataplaneutil.DBModePostgres} + c := &fakeDataplaneClient{dbmode: dpconf.DBModePostgres} t.Log("configuring the dataplane synchronizer") ctx, cancel := context.WithCancel(context.Background()) @@ -92,9 +92,9 @@ func TestSynchronizer(t *testing.T) { } func TestSynchronizer_IsReadyDoesntBlockWhenDataPlaneIsBlocked(t *testing.T) { - for _, dbMode := range []dataplaneutil.DBMode{ - dataplaneutil.DBModeOff, - dataplaneutil.DBModePostgres, + for _, dbMode := range []dpconf.DBMode{ + dpconf.DBModeOff, + dpconf.DBModePostgres, } { dbMode := dbMode t.Run(fmt.Sprintf("dbmode=%s", dbMode), func(t *testing.T) { @@ -131,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 dataplaneutil.DBMode + dbmode dpconf.DBMode updateCount atomic.Uint64 lock sync.RWMutex clientCallBlockDuration time.Duration t *testing.T } -func (c *fakeDataplaneClient) DBMode() dataplaneutil.DBMode { +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 ed44130826..623f6e7985 100644 --- a/internal/dataplane/translator/translator.go +++ b/internal/dataplane/translator/translator.go @@ -20,13 +20,13 @@ 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" "github.com/kong/kubernetes-ingress-controller/v3/internal/manager/featuregates" "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" kongv1alpha1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1alpha1" kongv1beta1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1beta1" ) @@ -61,12 +61,12 @@ type FeatureFlags struct { func NewFeatureFlags( featureGates featuregates.FeatureGates, - routerFlavor dataplaneutil.RouterFlavor, + 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), } diff --git a/internal/dataplane/translator/translator_test.go b/internal/dataplane/translator/translator_test.go index 3b81f3bee5..0774fc3340 100644 --- a/internal/dataplane/translator/translator_test.go +++ b/internal/dataplane/translator/translator_test.go @@ -24,11 +24,11 @@ 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" "github.com/kong/kubernetes-ingress-controller/v3/internal/util/builder" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" kongv1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1" kongv1beta1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1beta1" "github.com/kong/kubernetes-ingress-controller/v3/test/helpers/certificate" @@ -4504,7 +4504,7 @@ func TestNewFeatureFlags(t *testing.T) { name string featureGates map[string]bool - routerFlavor dataplaneutil.RouterFlavor + routerFlavor dpconf.RouterFlavor updateStatusFlag bool expectedFeatureFlags FeatureFlags @@ -4512,7 +4512,7 @@ func TestNewFeatureFlags(t *testing.T) { { name: "traditional compatible router and update status enabled", featureGates: map[string]bool{}, - routerFlavor: dataplaneutil.RouterFlavorTraditionalCompatible, + routerFlavor: dpconf.RouterFlavorTraditionalCompatible, updateStatusFlag: true, expectedFeatureFlags: FeatureFlags{ ReportConfiguredKubernetesObjects: true, @@ -4520,7 +4520,7 @@ func TestNewFeatureFlags(t *testing.T) { }, { name: "expression router and update status disabled", - routerFlavor: dataplaneutil.RouterFlavorExpressions, + routerFlavor: dpconf.RouterFlavorExpressions, expectedFeatureFlags: FeatureFlags{ ExpressionRoutes: true, }, diff --git a/internal/manager/run.go b/internal/manager/run.go index 7496102ee6..71920ab8d6 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: dataplaneutil.ShouldEnableExpressionRoutes(routerFlavor), + ExpressionRoutes: dpconf.ShouldEnableExpressionRoutes(routerFlavor), } kongConfig.Init(ctx, setupLog, initialKongClients) diff --git a/internal/manager/setup.go b/internal/manager/setup.go index d4eeca9d8f..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 dataplaneutil.DBMode) (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 dataplaneutil.DBMode) 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 024e3b3229..6579d45804 100644 --- a/internal/manager/utils/kongconfig/root.go +++ b/internal/manager/utils/kongconfig/root.go @@ -14,14 +14,14 @@ import ( "golang.org/x/sync/errgroup" "github.com/kong/kubernetes-ingress-controller/v3/internal/adminapi" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" + 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 dataplaneutil.DBMode - RouterFlavor dataplaneutil.RouterFlavor + DBMode dpconf.DBMode + RouterFlavor dpconf.RouterFlavor Version kong.Version } @@ -76,7 +76,7 @@ func extractConfigurationFromRoot(r Root) (map[string]any, error) { return rootConfig, nil } -func DBModeFromRoot(r Root) (dataplaneutil.DBMode, error) { +func DBModeFromRoot(r Root) (dpconf.DBMode, error) { rootConfig, err := extractConfigurationFromRoot(r) if err != nil { return "", err @@ -92,10 +92,10 @@ func DBModeFromRoot(r Root) (dataplaneutil.DBMode, error) { return "", fmt.Errorf("invalid %q type, expected a string, got %T", dbModeKey, dbMode) } - return dataplaneutil.NewDBMode(dbModeStr) + return dpconf.NewDBMode(dbModeStr) } -func RouterFlavorFromRoot(r Root) (dataplaneutil.RouterFlavor, error) { +func RouterFlavorFromRoot(r Root) (dpconf.RouterFlavor, error) { rootConfig, err := extractConfigurationFromRoot(r) if err != nil { return "", err @@ -110,7 +110,7 @@ func RouterFlavorFromRoot(r Root) (dataplaneutil.RouterFlavor, error) { if !ok { return "", fmt.Errorf("invalid %q type, expected a string, got %T", routerFlavorKey, routerFlavor) } - return dataplaneutil.RouterFlavor(routerFlavorStr), nil + return dpconf.RouterFlavor(routerFlavorStr), nil } func KongVersionFromRoot(r Root) (kong.Version, error) { @@ -171,13 +171,13 @@ func getRootKeyFunc(skipCACerts bool) func(Root) string { } // validateDBMode validates the provided dbMode string. -func validateDBMode(dbMode dataplaneutil.DBMode, skipCACerts bool) error { +func validateDBMode(dbMode dpconf.DBMode, skipCACerts bool) error { switch dbMode { - case "", dataplaneutil.DBModeOff: + case "", dpconf.DBModeOff: if skipCACerts { return fmt.Errorf("--skip-ca-certificates is not available for use with DB-less Kong instances") } - case dataplaneutil.DBModePostgres: + case dpconf.DBModePostgres: return nil 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 693d71971e..705623a662 100644 --- a/internal/manager/utils/kongconfig/root_test.go +++ b/internal/manager/utils/kongconfig/root_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" + dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config" "github.com/kong/kubernetes-ingress-controller/v3/internal/versions" ) @@ -22,15 +22,15 @@ func TestValidateRoots(t *testing.T) { testCases := []struct { name string configStr string - expectedDBMode dataplaneutil.DBMode - expectedRouterFlavor dataplaneutil.RouterFlavor + expectedDBMode dpconf.DBMode + expectedRouterFlavor dpconf.RouterFlavor expectedKongVersion string }{ { name: "dbless config with version 3.4.1", configStr: dblessConfigJSON3_4_1, - expectedDBMode: dataplaneutil.DBModeOff, - expectedRouterFlavor: dataplaneutil.RouterFlavorTraditionalCompatible, + expectedDBMode: dpconf.DBModeOff, + expectedRouterFlavor: dpconf.RouterFlavorTraditionalCompatible, expectedKongVersion: versions.KICv3VersionCutoff.String(), }, } diff --git a/test/integration/version_test.go b/test/integration/version_test.go index 7d71f93b84..81301c19f8 100644 --- a/test/integration/version_test.go +++ b/test/integration/version_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" + 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" @@ -39,7 +39,7 @@ func RunWhenKongVersion(t *testing.T, vRangeStr string, msg ...any) { } } -func RunWhenKongDBMode(t *testing.T, dbmode dataplaneutil.DBMode, msg ...any) { +func RunWhenKongDBMode(t *testing.T, dbmode dpconf.DBMode, msg ...any) { t.Helper() actual := eventuallyGetKongDBMode(t, proxyAdminURL) @@ -85,12 +85,12 @@ func eventuallyGetKongVersion(t *testing.T, adminURL *url.URL) kong.Version { return version } -func eventuallyGetKongDBMode(t *testing.T, adminURL *url.URL) dataplaneutil.DBMode { +func eventuallyGetKongDBMode(t *testing.T, adminURL *url.URL) dpconf.DBMode { t.Helper() var ( err error - dbmode dataplaneutil.DBMode + dbmode dpconf.DBMode ) require.EventuallyWithT(t, func(t *assert.CollectT) { @@ -102,12 +102,12 @@ func eventuallyGetKongDBMode(t *testing.T, adminURL *url.URL) dataplaneutil.DBMo return dbmode } -func eventuallyGetKongRouterFlavor(t *testing.T, adminURL *url.URL) dataplaneutil.RouterFlavor { +func eventuallyGetKongRouterFlavor(t *testing.T, adminURL *url.URL) dpconf.RouterFlavor { t.Helper() var ( err error - routerFlavor dataplaneutil.RouterFlavor + 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 f7b779f5cf..51b879dc73 100644 --- a/test/internal/helpers/kong.go +++ b/test/internal/helpers/kong.go @@ -11,8 +11,8 @@ 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" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" "github.com/kong/kubernetes-ingress-controller/v3/internal/versions" ) @@ -74,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) (dataplaneutil.DBMode, 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 @@ -87,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) (dataplaneutil.RouterFlavor, 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 From cd7f524a943a02d42b82e10fb9343ea5cb34c468 Mon Sep 17 00:00:00 2001 From: Jakub Warczarek Date: Tue, 7 Nov 2023 13:05:32 +0100 Subject: [PATCH 5/8] chore(refactor): rename parser to translator everywhere (#5095) --- internal/manager/run.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/manager/run.go b/internal/manager/run.go index 71920ab8d6..250be66ccb 100644 --- a/internal/manager/run.go +++ b/internal/manager/run.go @@ -160,7 +160,6 @@ func Run( } translatorFeatureFlags := translator.NewFeatureFlags( - logger, featureGates, routerFlavor, c.UpdateStatus, From 34a9633fe985c1077a8d6c18c06b1c3b2ab72a79 Mon Sep 17 00:00:00 2001 From: Yi Tao Date: Mon, 6 Nov 2023 17:26:58 +0800 Subject: [PATCH 6/8] define router flavor as a dedicated type --- internal/dataplane/translator/translator.go | 5 +++ .../dataplane/translator/translator_test.go | 14 ++++++++ internal/manager/run.go | 4 +++ internal/util/dataplane/mode.go | 32 +++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 internal/util/dataplane/mode.go diff --git a/internal/dataplane/translator/translator.go b/internal/dataplane/translator/translator.go index 623f6e7985..ce6f46cd69 100644 --- a/internal/dataplane/translator/translator.go +++ b/internal/dataplane/translator/translator.go @@ -27,6 +27,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/manager/featuregates" "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" kongv1alpha1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1alpha1" kongv1beta1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1beta1" ) @@ -61,7 +62,11 @@ type FeatureFlags struct { func NewFeatureFlags( featureGates featuregates.FeatureGates, +<<<<<<< HEAD routerFlavor dpconf.RouterFlavor, +======= + routerFlavor dataplaneutil.RouterFlavor, +>>>>>>> e629c903e (define router flavor as a dedicated type) updateStatusFlag bool, ) FeatureFlags { return FeatureFlags{ diff --git a/internal/dataplane/translator/translator_test.go b/internal/dataplane/translator/translator_test.go index 0774fc3340..a0bf4b12b8 100644 --- a/internal/dataplane/translator/translator_test.go +++ b/internal/dataplane/translator/translator_test.go @@ -29,6 +29,7 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/store" "github.com/kong/kubernetes-ingress-controller/v3/internal/util" "github.com/kong/kubernetes-ingress-controller/v3/internal/util/builder" + dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" kongv1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1" kongv1beta1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1beta1" "github.com/kong/kubernetes-ingress-controller/v3/test/helpers/certificate" @@ -4504,7 +4505,11 @@ func TestNewFeatureFlags(t *testing.T) { name string featureGates map[string]bool +<<<<<<< HEAD routerFlavor dpconf.RouterFlavor +======= + routerFlavor dataplaneutil.RouterFlavor +>>>>>>> e629c903e (define router flavor as a dedicated type) updateStatusFlag bool expectedFeatureFlags FeatureFlags @@ -4512,15 +4517,24 @@ func TestNewFeatureFlags(t *testing.T) { { name: "traditional compatible router and update status enabled", featureGates: map[string]bool{}, +<<<<<<< HEAD routerFlavor: dpconf.RouterFlavorTraditionalCompatible, +======= + routerFlavor: dataplaneutil.RouterFlavorExpressions, +>>>>>>> e629c903e (define router flavor as a dedicated type) updateStatusFlag: true, expectedFeatureFlags: FeatureFlags{ ReportConfiguredKubernetesObjects: true, }, }, { +<<<<<<< HEAD name: "expression router and update status disabled", routerFlavor: dpconf.RouterFlavorExpressions, +======= + name: "expression routes feature gate enabled and router flavor matches", + routerFlavor: dataplaneutil.RouterFlavorExpressions, +>>>>>>> e629c903e (define router flavor as a dedicated type) expectedFeatureFlags: FeatureFlags{ ExpressionRoutes: true, }, diff --git a/internal/manager/run.go b/internal/manager/run.go index 250be66ccb..2fa66301e1 100644 --- a/internal/manager/run.go +++ b/internal/manager/run.go @@ -120,7 +120,11 @@ func Run( FilterTags: c.FilterTags, SkipCACertificates: c.SkipCACertificates, EnableReverseSync: c.EnableReverseSync, +<<<<<<< HEAD ExpressionRoutes: dpconf.ShouldEnableExpressionRoutes(routerFlavor), +======= + ExpressionRoutes: dataplaneutil.ShouldEnableExpressionRoutes(routerFlavor), +>>>>>>> e629c903e (define router flavor as a dedicated type) } kongConfig.Init(ctx, setupLog, initialKongClients) diff --git a/internal/util/dataplane/mode.go b/internal/util/dataplane/mode.go new file mode 100644 index 0000000000..9571e67e8a --- /dev/null +++ b/internal/util/dataplane/mode.go @@ -0,0 +1,32 @@ +package dataplane + +type DBMode string + +const ( + DBModeOff = "off" + DBModePostgres = "postgres" + DBModeCassandra = "cassandra" +) + +// IsDBLessMode can be used to detect the proxy mode (db or dbless). +func IsDBLessMode(mode DBMode) bool { + return mode == "" || mode == DBModeOff +} + +// DBBacked returns true if the gateway is DB backed. +// reverse of IsDBLessMode for readability. +func DBBacked(mode DBMode) bool { + return !IsDBLessMode(mode) +} + +type RouterFlavor string + +const ( + RouterFlavorTraditional RouterFlavor = "traditional" + RouterFlavorTraditionalCompatible RouterFlavor = "traditional_compatible" + RouterFlavorExpressions RouterFlavor = "expressions" +) + +func ShouldEnableExpressionRoutes(rf RouterFlavor) bool { + return rf == RouterFlavorExpressions +} From 687e05e5dcb52c6049eb779660a49939228a7aea Mon Sep 17 00:00:00 2001 From: Yi Tao Date: Tue, 7 Nov 2023 11:17:58 +0800 Subject: [PATCH 7/8] add NewDBMode and fix unit tests --- internal/dataplane/translator/translator.go | 17 ----------------- .../dataplane/translator/translator_test.go | 19 +++---------------- internal/manager/run.go | 4 ---- internal/util/dataplane/mode.go | 17 ++++++++++++++--- 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/internal/dataplane/translator/translator.go b/internal/dataplane/translator/translator.go index ce6f46cd69..50b9e2b25c 100644 --- a/internal/dataplane/translator/translator.go +++ b/internal/dataplane/translator/translator.go @@ -27,7 +27,6 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/manager/featuregates" "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" kongv1alpha1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1alpha1" kongv1beta1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1beta1" ) @@ -62,11 +61,7 @@ type FeatureFlags struct { func NewFeatureFlags( featureGates featuregates.FeatureGates, -<<<<<<< HEAD routerFlavor dpconf.RouterFlavor, -======= - routerFlavor dataplaneutil.RouterFlavor, ->>>>>>> e629c903e (define router flavor as a dedicated type) updateStatusFlag bool, ) FeatureFlags { return FeatureFlags{ @@ -77,18 +72,6 @@ func NewFeatureFlags( } } -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 a0bf4b12b8..f344023a11 100644 --- a/internal/dataplane/translator/translator_test.go +++ b/internal/dataplane/translator/translator_test.go @@ -29,7 +29,6 @@ import ( "github.com/kong/kubernetes-ingress-controller/v3/internal/store" "github.com/kong/kubernetes-ingress-controller/v3/internal/util" "github.com/kong/kubernetes-ingress-controller/v3/internal/util/builder" - dataplaneutil "github.com/kong/kubernetes-ingress-controller/v3/internal/util/dataplane" kongv1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1" kongv1beta1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1beta1" "github.com/kong/kubernetes-ingress-controller/v3/test/helpers/certificate" @@ -4505,36 +4504,24 @@ func TestNewFeatureFlags(t *testing.T) { name string featureGates map[string]bool -<<<<<<< HEAD routerFlavor dpconf.RouterFlavor -======= - routerFlavor dataplaneutil.RouterFlavor ->>>>>>> e629c903e (define router flavor as a dedicated type) updateStatusFlag bool expectedFeatureFlags FeatureFlags }{ { - name: "traditional compatible router and update status enabled", - featureGates: map[string]bool{}, -<<<<<<< HEAD + name: "traditional compatible router and update status enabled", + featureGates: map[string]bool{}, + routerFlavor: dpconf.RouterFlavorTraditionalCompatible, -======= - routerFlavor: dataplaneutil.RouterFlavorExpressions, ->>>>>>> e629c903e (define router flavor as a dedicated type) updateStatusFlag: true, expectedFeatureFlags: FeatureFlags{ ReportConfiguredKubernetesObjects: true, }, }, { -<<<<<<< HEAD name: "expression router and update status disabled", routerFlavor: dpconf.RouterFlavorExpressions, -======= - name: "expression routes feature gate enabled and router flavor matches", - routerFlavor: dataplaneutil.RouterFlavorExpressions, ->>>>>>> e629c903e (define router flavor as a dedicated type) expectedFeatureFlags: FeatureFlags{ ExpressionRoutes: true, }, diff --git a/internal/manager/run.go b/internal/manager/run.go index 2fa66301e1..250be66ccb 100644 --- a/internal/manager/run.go +++ b/internal/manager/run.go @@ -120,11 +120,7 @@ func Run( FilterTags: c.FilterTags, SkipCACertificates: c.SkipCACertificates, EnableReverseSync: c.EnableReverseSync, -<<<<<<< HEAD ExpressionRoutes: dpconf.ShouldEnableExpressionRoutes(routerFlavor), -======= - ExpressionRoutes: dataplaneutil.ShouldEnableExpressionRoutes(routerFlavor), ->>>>>>> e629c903e (define router flavor as a dedicated type) } kongConfig.Init(ctx, setupLog, initialKongClients) diff --git a/internal/util/dataplane/mode.go b/internal/util/dataplane/mode.go index 9571e67e8a..69e48a3399 100644 --- a/internal/util/dataplane/mode.go +++ b/internal/util/dataplane/mode.go @@ -1,13 +1,24 @@ package dataplane +import "fmt" + type DBMode string const ( - DBModeOff = "off" - DBModePostgres = "postgres" - DBModeCassandra = "cassandra" + 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 IsDBLessMode(mode DBMode) bool { return mode == "" || mode == DBModeOff From f0a811c03280c1d292de210621b1a55eb890ab1c Mon Sep 17 00:00:00 2001 From: Yi Tao Date: Wed, 8 Nov 2023 17:56:38 +0800 Subject: [PATCH 8/8] rename DBBacked() and remove util/dataplane package --- internal/dataplane/config/dbmode.go | 4 +-- internal/dataplane/kong_client.go | 2 +- internal/util/dataplane/mode.go | 43 ----------------------------- 3 files changed, 3 insertions(+), 46 deletions(-) delete mode 100644 internal/util/dataplane/mode.go diff --git a/internal/dataplane/config/dbmode.go b/internal/dataplane/config/dbmode.go index 341d8d03a2..1ffbe5477e 100644 --- a/internal/dataplane/config/dbmode.go +++ b/internal/dataplane/config/dbmode.go @@ -24,8 +24,8 @@ func (m DBMode) IsDBLessMode() bool { return m == "" || m == DBModeOff } -// DBBacked returns true if the gateway is DB backed. +// IsDBBacked returns true if the gateway is DB backed. // reverse of IsDBLessMode for readability. -func (m DBMode) DBBacked() bool { +func (m DBMode) IsDBBacked() bool { return !m.IsDBLessMode() } diff --git a/internal/dataplane/kong_client.go b/internal/dataplane/kong_client.go index 5c827e69a2..ff40e345ad 100644 --- a/internal/dataplane/kong_client.go +++ b/internal/dataplane/kong_client.go @@ -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 c.dbmode.DBBacked() && + if c.dbmode.IsDBBacked() && len(gatewayClients) > 1 { for _, client := range gatewayClients { client.SetLastConfigSHA([]byte(shas[0])) diff --git a/internal/util/dataplane/mode.go b/internal/util/dataplane/mode.go deleted file mode 100644 index 69e48a3399..0000000000 --- a/internal/util/dataplane/mode.go +++ /dev/null @@ -1,43 +0,0 @@ -package dataplane - -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 IsDBLessMode(mode DBMode) bool { - return mode == "" || mode == DBModeOff -} - -// DBBacked returns true if the gateway is DB backed. -// reverse of IsDBLessMode for readability. -func DBBacked(mode DBMode) bool { - return !IsDBLessMode(mode) -} - -type RouterFlavor string - -const ( - RouterFlavorTraditional RouterFlavor = "traditional" - RouterFlavorTraditionalCompatible RouterFlavor = "traditional_compatible" - RouterFlavorExpressions RouterFlavor = "expressions" -) - -func ShouldEnableExpressionRoutes(rf RouterFlavor) bool { - return rf == RouterFlavorExpressions -}