Skip to content

Commit

Permalink
Use protobuf encoding for core K8s APIs in ingress-nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
tosi3k committed Dec 10, 2024
1 parent c6a2d9a commit 4255f92
Show file tree
Hide file tree
Showing 6 changed files with 22 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
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7
github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A=
github.com/envoyproxy/protoc-gen-validate v1.0.4/go.mod h1:qys6tmnRsYrQqIhm2bvKZH4Blx/1gTIZ2UKVY1M+Yew=
github.com/envoyproxy/protoc-gen-validate v1.1.0/go.mod h1:sXRDRVmzEbkM7CVcM06s9shE/m23dg3wzjl0UWqJ2q4=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
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 4255f92

Please sign in to comment.