util: use protobuf encoding for core k8s apis

For core K8s API objects like Pods, Nodes, etc., we
can use protobuf encoding which reduces CPU consumption
related to (de)serialization, reduces overall latency
of the API call, reduces memory footprint, reduces the
amount of work performed by the GC and results in quicker
propagation of objects to event handlers of shared informers.

Signed-off-by: Nikhil-Ladha <nikhilladha1999@gmail.com>
This commit is contained in:
Nikhil-Ladha 2024-09-25 15:45:28 +05:30 committed by mergify[bot]
parent 8c252d58ea
commit 01a0ec2d8c
2 changed files with 9 additions and 1 deletions

View File

@ -20,6 +20,8 @@ import (
"github.com/ceph/ceph-csi/internal/util/log" "github.com/ceph/ceph-csi/internal/util/log"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/leaderelection/resourcelock" "k8s.io/client-go/tools/leaderelection/resourcelock"
clientConfig "sigs.k8s.io/controller-runtime/pkg/client/config" clientConfig "sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/manager"
@ -69,7 +71,11 @@ func Start(config Config) error {
LeaderElectionResourceLock: resourcelock.LeasesResourceLock, LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
LeaderElectionID: electionID, LeaderElectionID: electionID,
} }
mgr, err := manager.New(clientConfig.GetConfigOrDie(), opts)
kubeConfig := clientConfig.GetConfigOrDie()
coreKubeConfig := rest.CopyConfig(kubeConfig)
coreKubeConfig.ContentType = runtime.ContentTypeProtobuf
mgr, err := manager.New(coreKubeConfig, opts)
if err != nil { if err != nil {
log.ErrorLogMsg("failed to create manager %s", err) log.ErrorLogMsg("failed to create manager %s", err)

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"os" "os"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd"
@ -47,6 +48,7 @@ func NewK8sClient() (*kubernetes.Clientset, error) {
return nil, fmt.Errorf("failed to get cluster config: %w", err) return nil, fmt.Errorf("failed to get cluster config: %w", err)
} }
} }
cfg.ContentType = runtime.ContentTypeProtobuf
client, err := kubernetes.NewForConfig(cfg) client, err := kubernetes.NewForConfig(cfg)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create client: %w", err) return nil, fmt.Errorf("failed to create client: %w", err)