Skip to content

Commit

Permalink
Controller: Use Protocol Buffers for API access.
Browse files Browse the repository at this point in the history
  • Loading branch information
tosi3k committed Dec 19, 2024
1 parent 57b4a14 commit 07a56c9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmd/nginx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions cmd/plugin/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion images/kube-webhook-certgen/rootfs/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}
Expand Down
2 changes: 2 additions & 0 deletions internal/ingress/controller/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Expand Down

0 comments on commit 07a56c9

Please sign in to comment.