From d76d9c54ce34b75af954136d4459579ed41e804d Mon Sep 17 00:00:00 2001 From: qicz Date: Thu, 19 Sep 2024 14:46:25 +0800 Subject: [PATCH 1/3] performance: optimize by protobuf context-type. Signed-off-by: qicz --- internal/infrastructure/manager.go | 4 ++- internal/kubernetes/client.go | 28 ------------------- internal/kubernetes/config.go | 43 ++++++++++++++++++++++++++++++ internal/provider/runner/runner.go | 3 ++- 4 files changed, 48 insertions(+), 30 deletions(-) create mode 100644 internal/kubernetes/config.go diff --git a/internal/infrastructure/manager.go b/internal/infrastructure/manager.go index 198acef8708..373910ada37 100644 --- a/internal/infrastructure/manager.go +++ b/internal/infrastructure/manager.go @@ -17,6 +17,8 @@ import ( "github.com/envoyproxy/gateway/internal/envoygateway/config" "github.com/envoyproxy/gateway/internal/infrastructure/kubernetes" "github.com/envoyproxy/gateway/internal/ir" + + kube "github.com/envoyproxy/gateway/internal/kubernetes" ) var _ Manager = (*kubernetes.Infra)(nil) @@ -39,7 +41,7 @@ func NewManager(cfg *config.Server) (Manager, error) { switch cfg.EnvoyGateway.Provider.Type { case egv1a1.ProviderTypeKubernetes: - cli, err := client.New(clicfg.GetConfigOrDie(), client.Options{Scheme: envoygateway.GetScheme()}) + cli, err := client.New(kube.ProtobufConfig(clicfg.GetConfigOrDie()), client.Options{Scheme: envoygateway.GetScheme()}) if err != nil { return nil, err } diff --git a/internal/kubernetes/client.go b/internal/kubernetes/client.go index 5c58242bab6..5bc9a4d0356 100644 --- a/internal/kubernetes/client.go +++ b/internal/kubernetes/client.go @@ -13,16 +13,12 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/serializer" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes" kubescheme "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/remotecommand" - - "github.com/envoyproxy/gateway/internal/envoygateway" ) type CLIClient interface { @@ -84,30 +80,6 @@ func newClientInternal(restCfg *rest.Config) (*client, error) { return &c, err } -func setRestDefaults(config *rest.Config) *rest.Config { - if config.GroupVersion == nil || config.GroupVersion.Empty() { - config.GroupVersion = &corev1.SchemeGroupVersion - } - if len(config.APIPath) == 0 { - if len(config.GroupVersion.Group) == 0 { - config.APIPath = "/api" - } else { - config.APIPath = "/apis" - } - } - if len(config.ContentType) == 0 { - config.ContentType = runtime.ContentTypeJSON - } - if config.NegotiatedSerializer == nil { - // This codec factory ensures the resources are not converted. Therefore, resources - // will not be round-tripped through internal versions. Defaulting does not happen - // on the client. - config.NegotiatedSerializer = serializer.NewCodecFactory(envoygateway.GetScheme()).WithoutConversion() - } - - return config -} - func (c *client) RESTConfig() *rest.Config { if c.config == nil { return nil diff --git a/internal/kubernetes/config.go b/internal/kubernetes/config.go new file mode 100644 index 00000000000..c03f7037bdf --- /dev/null +++ b/internal/kubernetes/config.go @@ -0,0 +1,43 @@ +// Copyright Envoy Gateway Authors +// SPDX-License-Identifier: Apache-2.0 +// The full text of the Apache license is available in the LICENSE file at +// the root of the repo. + +package kubernetes + +import ( + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/serializer" + "k8s.io/client-go/rest" + + "github.com/envoyproxy/gateway/internal/envoygateway" +) + +func ProtobufConfig(restCfg *rest.Config) *rest.Config { + return setRestDefaults(restCfg) +} + +func setRestDefaults(config *rest.Config) *rest.Config { + if config.GroupVersion == nil || config.GroupVersion.Empty() { + config.GroupVersion = &corev1.SchemeGroupVersion + } + if len(config.APIPath) == 0 { + if len(config.GroupVersion.Group) == 0 { + config.APIPath = "/api" + } else { + config.APIPath = "/apis" + } + } + if len(config.ContentType) == 0 { + config.ContentType = runtime.ContentTypeProtobuf + } + if config.NegotiatedSerializer == nil { + // This codec factory ensures the resources are not converted. Therefore, resources + // will not be round-tripped through internal versions. Defaulting does not happen + // on the client. + config.NegotiatedSerializer = serializer.NewCodecFactory(envoygateway.GetScheme()).WithoutConversion() + } + + return config +} diff --git a/internal/provider/runner/runner.go b/internal/provider/runner/runner.go index 94488489376..1057ee034fb 100644 --- a/internal/provider/runner/runner.go +++ b/internal/provider/runner/runner.go @@ -13,6 +13,7 @@ import ( egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" "github.com/envoyproxy/gateway/internal/envoygateway/config" + kube "github.com/envoyproxy/gateway/internal/kubernetes" "github.com/envoyproxy/gateway/internal/message" "github.com/envoyproxy/gateway/internal/provider" "github.com/envoyproxy/gateway/internal/provider/file" @@ -75,7 +76,7 @@ func (r *Runner) createKubernetesProvider() (*kubernetes.Provider, error) { return nil, fmt.Errorf("failed to get kubeconfig: %w", err) } - p, err := kubernetes.New(cfg, &r.Config.Server, r.ProviderResources) + p, err := kubernetes.New(kube.ProtobufConfig(cfg), &r.Config.Server, r.ProviderResources) if err != nil { return nil, fmt.Errorf("failed to create provider %s: %w", egv1a1.ProviderTypeKubernetes, err) } From 03f02329a9a59ba504bc0713dd27e8592f24d080 Mon Sep 17 00:00:00 2001 From: qicz Date: Thu, 19 Sep 2024 15:08:43 +0800 Subject: [PATCH 2/3] fix lint Signed-off-by: qicz --- internal/infrastructure/manager.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/infrastructure/manager.go b/internal/infrastructure/manager.go index 373910ada37..6f61fa14c0a 100644 --- a/internal/infrastructure/manager.go +++ b/internal/infrastructure/manager.go @@ -17,7 +17,6 @@ import ( "github.com/envoyproxy/gateway/internal/envoygateway/config" "github.com/envoyproxy/gateway/internal/infrastructure/kubernetes" "github.com/envoyproxy/gateway/internal/ir" - kube "github.com/envoyproxy/gateway/internal/kubernetes" ) From 8832ae18cd1c71dd281c22ab73506a0877f1fee3 Mon Sep 17 00:00:00 2001 From: qicz Date: Fri, 20 Sep 2024 11:06:46 +0800 Subject: [PATCH 3/3] using client-go configfor Signed-off-by: qicz --- internal/kubernetes/config.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/internal/kubernetes/config.go b/internal/kubernetes/config.go index c03f7037bdf..f80cc4dfa7c 100644 --- a/internal/kubernetes/config.go +++ b/internal/kubernetes/config.go @@ -7,8 +7,8 @@ package kubernetes import ( corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/serializer" + "k8s.io/client-go/metadata" "k8s.io/client-go/rest" "github.com/envoyproxy/gateway/internal/envoygateway" @@ -19,6 +19,7 @@ func ProtobufConfig(restCfg *rest.Config) *rest.Config { } func setRestDefaults(config *rest.Config) *rest.Config { + config = metadata.ConfigFor(config) if config.GroupVersion == nil || config.GroupVersion.Empty() { config.GroupVersion = &corev1.SchemeGroupVersion } @@ -29,15 +30,11 @@ func setRestDefaults(config *rest.Config) *rest.Config { config.APIPath = "/apis" } } - if len(config.ContentType) == 0 { - config.ContentType = runtime.ContentTypeProtobuf - } - if config.NegotiatedSerializer == nil { - // This codec factory ensures the resources are not converted. Therefore, resources - // will not be round-tripped through internal versions. Defaulting does not happen - // on the client. - config.NegotiatedSerializer = serializer.NewCodecFactory(envoygateway.GetScheme()).WithoutConversion() - } + + // This codec factory ensures the resources are not converted. Therefore, resources + // will not be round-tripped through internal versions. Defaulting does not happen + // on the client. + config.NegotiatedSerializer = serializer.NewCodecFactory(envoygateway.GetScheme()).WithoutConversion() return config }