From 07a56c94d4482e16b6bfb7309d85ca0fa385960c Mon Sep 17 00:00:00 2001 From: Antoni Zawodny Date: Thu, 19 Dec 2024 15:57:42 +0100 Subject: [PATCH] Controller: Use Protocol Buffers for API access. --- cmd/nginx/main.go | 3 +++ cmd/plugin/request/request.go | 7 +++++++ images/kube-webhook-certgen/rootfs/cmd/root.go | 6 +++++- internal/ingress/controller/store/store_test.go | 2 ++ test/e2e/framework/framework.go | 5 ++++- 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/nginx/main.go b/cmd/nginx/main.go index 781f3a8eb4..0473ec9dc5 100644 --- a/cmd/nginx/main.go +++ b/cmd/nginx/main.go @@ -29,6 +29,7 @@ import ( "github.com/prometheus/client_golang/prometheus/collectors" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kuberuntime "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/wait" discovery "k8s.io/apimachinery/pkg/version" "k8s.io/client-go/kubernetes" @@ -202,6 +203,8 @@ func createApiserverClient(apiserverHost, rootCAFile, kubeConfig string) (*kuber cfg.TLSClientConfig = tlsClientConfig } + cfg.ContentType = kuberuntime.ContentTypeProtobuf + klog.InfoS("Creating API client", "host", cfg.Host) client, err := kubernetes.NewForConfig(cfg) diff --git a/cmd/plugin/request/request.go b/cmd/plugin/request/request.go index 57b02827e9..19db354c33 100644 --- a/cmd/plugin/request/request.go +++ b/cmd/plugin/request/request.go @@ -25,6 +25,7 @@ import ( discoveryv1 "k8s.io/api/discovery/v1" networking "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/cli-runtime/pkg/genericclioptions" appsv1client "k8s.io/client-go/kubernetes/typed/apps/v1" corev1 "k8s.io/client-go/kubernetes/typed/core/v1" @@ -97,6 +98,7 @@ func GetDeployments(flags *genericclioptions.ConfigFlags, namespace string) ([]a if err != nil { return make([]appsv1.Deployment, 0), err } + rawConfig.ContentType = runtime.ContentTypeProtobuf api, err := appsv1client.NewForConfig(rawConfig) if err != nil { @@ -117,6 +119,7 @@ func GetIngressDefinitions(flags *genericclioptions.ConfigFlags, namespace strin if err != nil { return make([]networking.Ingress, 0), err } + rawConfig.ContentType = runtime.ContentTypeProtobuf api, err := typednetworking.NewForConfig(rawConfig) if err != nil { @@ -193,6 +196,7 @@ func getEndpointSlices(flags *genericclioptions.ConfigFlags, namespace string) ( if err != nil { return nil, err } + rawConfig.ContentType = runtime.ContentTypeProtobuf api, err := discoveryv1client.NewForConfig(rawConfig) if err != nil { @@ -259,6 +263,7 @@ func getPods(flags *genericclioptions.ConfigFlags) ([]apiv1.Pod, error) { if err != nil { return make([]apiv1.Pod, 0), err } + rawConfig.ContentType = runtime.ContentTypeProtobuf api, err := corev1.NewForConfig(rawConfig) if err != nil { @@ -280,6 +285,7 @@ func getLabeledPods(flags *genericclioptions.ConfigFlags, label string) ([]apiv1 if err != nil { return make([]apiv1.Pod, 0), err } + rawConfig.ContentType = runtime.ContentTypeProtobuf api, err := corev1.NewForConfig(rawConfig) if err != nil { @@ -319,6 +325,7 @@ func getServices(flags *genericclioptions.ConfigFlags) ([]apiv1.Service, error) if err != nil { return make([]apiv1.Service, 0), err } + rawConfig.ContentType = runtime.ContentTypeProtobuf api, err := corev1.NewForConfig(rawConfig) if err != nil { diff --git a/images/kube-webhook-certgen/rootfs/cmd/root.go b/images/kube-webhook-certgen/rootfs/cmd/root.go index 7db870d15f..6e8a038927 100644 --- a/images/kube-webhook-certgen/rootfs/cmd/root.go +++ b/images/kube-webhook-certgen/rootfs/cmd/root.go @@ -6,7 +6,9 @@ import ( "github.com/onrik/logrus/filename" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset" ) @@ -88,8 +90,10 @@ func newKubernetesClients(kubeconfig string) (kubernetes.Interface, clientset.In if err != nil { log.WithError(err).Fatal("error building kubernetes config") } + coreConfig := rest.CopyConfig(config) + coreConfig.ContentType = runtime.ContentTypeProtobuf - c, err := kubernetes.NewForConfig(config) + c, err := kubernetes.NewForConfig(coreConfig) if err != nil { log.WithError(err).Fatal("error creating kubernetes client") } diff --git a/internal/ingress/controller/store/store_test.go b/internal/ingress/controller/store/store_test.go index 9c719af3bd..0c0c2a9ccf 100644 --- a/internal/ingress/controller/store/store_test.go +++ b/internal/ingress/controller/store/store_test.go @@ -32,6 +32,7 @@ import ( k8sErrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/cache" "sigs.k8s.io/controller-runtime/pkg/envtest" @@ -98,6 +99,7 @@ func TestStore(t *testing.T) { defer te.Stop() //nolint:errcheck // Ignore the error + cfg.ContentType = runtime.ContentTypeProtobuf clientSet, err := kubernetes.NewForConfig(cfg) if err != nil { t.Fatalf("error: %v", err) diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index 204da7df05..29ec862d57 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -35,6 +35,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" @@ -129,7 +130,9 @@ func (f *Framework) CreateEnvironment() { // TODO: remove after k8s v1.22 f.KubeConfig.WarningHandler = rest.NoWarnings{} - f.KubeClientSet, err = kubernetes.NewForConfig(f.KubeConfig) + coreConfig := rest.CopyConfig(f.KubeConfig) + coreConfig.ContentType = runtime.ContentTypeProtobuf + f.KubeClientSet, err = kubernetes.NewForConfig(coreConfig) assert.Nil(ginkgo.GinkgoT(), err, "creating a kubernetes client") }