diff --git a/internal/infrastructure/manager.go b/internal/infrastructure/manager.go index 198acef8708..6f61fa14c0a 100644 --- a/internal/infrastructure/manager.go +++ b/internal/infrastructure/manager.go @@ -17,6 +17,7 @@ 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 +40,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..f80cc4dfa7c --- /dev/null +++ b/internal/kubernetes/config.go @@ -0,0 +1,40 @@ +// 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/serializer" + "k8s.io/client-go/metadata" + "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 { + config = metadata.ConfigFor(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" + } + } + + // 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) }