From d6bb142f69163600c3919514ff0b97fc13e54df0 Mon Sep 17 00:00:00 2001 From: Vadim Rutkovsky Date: Wed, 23 Oct 2024 09:57:47 +0200 Subject: [PATCH] Use protobuf if possible for core kubernetes clients Protobuf is more efficient comparing to JSON, but it cannot yet be used for custom types --- pkg/client/client.go | 6 ++++++ pkg/metrics/server.go | 3 +++ pkg/operator/operator.go | 3 +++ 3 files changed, 12 insertions(+) diff --git a/pkg/client/client.go b/pkg/client/client.go index aaaa640275..d8a6d1f9d6 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -100,6 +100,9 @@ func NewForConfig(cfg *rest.Config, version string, namespace, userWorkloadNames client := New(version, namespace, userWorkloadNamespace, options...) if client.kclient == nil { + cfg = rest.CopyConfig(cfg) + cfg.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json" + cfg.ContentType = "application/vnd.kubernetes.protobuf" kclient, err := kubernetes.NewForConfig(cfg) if err != nil { return nil, fmt.Errorf("creating kubernetes clientset client: %w", err) @@ -185,6 +188,9 @@ func NewForConfig(cfg *rest.Config, version string, namespace, userWorkloadNames } if client.mdataclient == nil { + cfg = rest.CopyConfig(cfg) + cfg.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json" + cfg.ContentType = "application/vnd.kubernetes.protobuf" mdataclient, err := metadata.NewForConfig(cfg) if err != nil { return nil, fmt.Errorf("creating metadata clientset client: %w", err) diff --git a/pkg/metrics/server.go b/pkg/metrics/server.go index f7e655e570..c318275add 100644 --- a/pkg/metrics/server.go +++ b/pkg/metrics/server.go @@ -39,6 +39,9 @@ type Server struct { // NewServer returns a functional Server. func NewServer(name string, config *rest.Config, kubeConfig, certFile, keyFile string) (*Server, error) { + config = rest.CopyConfig(config) + config.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json" + config.ContentType = "application/vnd.kubernetes.protobuf" kubeClient, err := kubernetes.NewForConfig(config) if err != nil { return nil, err diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index f119d568d1..e1a1b035e7 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -224,6 +224,9 @@ func New( telemetryMatches []string, a *manifests.Assets, ) (*Operator, error) { + config = rest.CopyConfig(config) + config.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json" + config.ContentType = "application/vnd.kubernetes.protobuf" kclient, err := kubernetes.NewForConfig(config) if err != nil { return nil, fmt.Errorf("creating kubernetes clientset client: %w", err)