From c85f0e438ec3c41048739e794f27f3a33e853df0 Mon Sep 17 00:00:00 2001 From: Xunzhuo Date: Sat, 11 Nov 2023 04:10:59 +0800 Subject: [PATCH] opt: move gateway namespace mode to common helper (#2129) opt: codes Signed-off-by: bitliu --- api/v1alpha1/envoygateway_helpers.go | 9 +++++++++ internal/provider/kubernetes/kubernetes.go | 9 +-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/api/v1alpha1/envoygateway_helpers.go b/api/v1alpha1/envoygateway_helpers.go index ef9ab69706b..25866ed1203 100644 --- a/api/v1alpha1/envoygateway_helpers.go +++ b/api/v1alpha1/envoygateway_helpers.go @@ -76,6 +76,15 @@ func (e *EnvoyGateway) GetEnvoyGatewayAdminAddress() string { return "" } +// NamespaceMode returns if uses namespace mode. +func (e *EnvoyGateway) NamespaceMode() bool { + return e.Provider != nil && + e.Provider.Kubernetes != nil && + e.Provider.Kubernetes.Watch != nil && + e.Provider.Kubernetes.Watch.Type == KubernetesWatchModeTypeNamespaces && + len(e.Provider.Kubernetes.Watch.Namespaces) > 0 +} + // DefaultGateway returns a new Gateway with default configuration parameters. func DefaultGateway() *Gateway { return &Gateway{ diff --git a/internal/provider/kubernetes/kubernetes.go b/internal/provider/kubernetes/kubernetes.go index f9bfc6d73cf..104400f2b05 100644 --- a/internal/provider/kubernetes/kubernetes.go +++ b/internal/provider/kubernetes/kubernetes.go @@ -16,7 +16,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/manager" - "github.com/envoyproxy/gateway/api/v1alpha1" "github.com/envoyproxy/gateway/internal/envoygateway" "github.com/envoyproxy/gateway/internal/envoygateway/config" "github.com/envoyproxy/gateway/internal/message" @@ -43,13 +42,7 @@ func New(cfg *rest.Config, svr *config.Server, resources *message.ProviderResour } // TODO: implement config validation on the watch mode config - namespacedMode := - svr.EnvoyGateway.Provider != nil && - svr.EnvoyGateway.Provider.Kubernetes != nil && - (svr.EnvoyGateway.Provider.Kubernetes.Watch != nil) && - (svr.EnvoyGateway.Provider.Kubernetes.Watch.Type == v1alpha1.KubernetesWatchModeTypeNamespaces) && - (len(svr.EnvoyGateway.Provider.Kubernetes.Watch.Namespaces) > 0) - if namespacedMode { + if svr.EnvoyGateway.NamespaceMode() { mgrOpts.Cache.DefaultNamespaces = make(map[string]cache.Config) for _, watchNS := range svr.EnvoyGateway.Provider.Kubernetes.Watch.Namespaces { mgrOpts.Cache.DefaultNamespaces[watchNS] = cache.Config{}