Use protobuf with built-in Kube REST APIs
Signed-off-by: Monis Khan <mok@vmware.com>
This commit is contained in:
parent
e92bdbea64
commit
9118869d04
@ -22,6 +22,7 @@ import (
|
|||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/intstr"
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
"k8s.io/apiserver/pkg/authentication/authenticator"
|
"k8s.io/apiserver/pkg/authentication/authenticator"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
@ -73,19 +74,22 @@ credential from somewhere to an internal credential to be used for
|
|||||||
authenticating to the Kubernetes API.`,
|
authenticating to the Kubernetes API.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
// Load the Kubernetes client configuration (kubeconfig),
|
// Load the Kubernetes client configuration (kubeconfig),
|
||||||
kubeconfig, err := restclient.InClusterConfig()
|
kubeConfig, err := restclient.InClusterConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not load in-cluster configuration: %w", err)
|
return fmt.Errorf("could not load in-cluster configuration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// explicitly use protobuf when talking to built-in kube APIs
|
||||||
|
protoKubeConfig := createProtoKubeConfig(kubeConfig)
|
||||||
|
|
||||||
// Connect to the core Kubernetes API.
|
// Connect to the core Kubernetes API.
|
||||||
k8s, err := kubernetes.NewForConfig(kubeconfig)
|
k8s, err := kubernetes.NewForConfig(protoKubeConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not initialize Kubernetes client: %w", err)
|
return fmt.Errorf("could not initialize Kubernetes client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect to the Kubernetes aggregation API.
|
// Connect to the Kubernetes aggregation API.
|
||||||
aggregation, err := aggregationv1client.NewForConfig(kubeconfig)
|
aggregation, err := aggregationv1client.NewForConfig(protoKubeConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not initialize Kubernetes client: %w", err)
|
return fmt.Errorf("could not initialize Kubernetes client: %w", err)
|
||||||
}
|
}
|
||||||
@ -259,3 +263,13 @@ func runGracefully(ctx context.Context, srv *http.Server, eg *errgroup.Group, f
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
return srv.Shutdown(shutdownCtx)
|
return srv.Shutdown(shutdownCtx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createProtoKubeConfig returns a copy of the input config with the ContentConfig set to use protobuf.
|
||||||
|
// do not use this config to communicate with any CRD based APIs.
|
||||||
|
func createProtoKubeConfig(kubeConfig *restclient.Config) *restclient.Config {
|
||||||
|
protoKubeConfig := restclient.CopyConfig(kubeConfig)
|
||||||
|
const protoThenJSON = runtime.ContentTypeProtobuf + "," + runtime.ContentTypeJSON
|
||||||
|
protoKubeConfig.AcceptContentTypes = protoThenJSON
|
||||||
|
protoKubeConfig.ContentType = runtime.ContentTypeProtobuf
|
||||||
|
return protoKubeConfig
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user