Skip to content

Commit

Permalink
Use protobuf if possible for core kubernetes clients
Browse files Browse the repository at this point in the history
Protobuf is more efficient comparing to JSON, but it cannot yet be used for custom types
  • Loading branch information
vrutkovs committed Oct 23, 2024
1 parent 7a40c9c commit d6bb142
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func NewForConfig(cfg *rest.Config, version string, namespace, userWorkloadNames
client := New(version, namespace, userWorkloadNamespace, options...)

if client.kclient == nil {
cfg = rest.CopyConfig(cfg)
cfg.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
cfg.ContentType = "application/vnd.kubernetes.protobuf"
kclient, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, fmt.Errorf("creating kubernetes clientset client: %w", err)
Expand Down Expand Up @@ -185,6 +188,9 @@ func NewForConfig(cfg *rest.Config, version string, namespace, userWorkloadNames
}

if client.mdataclient == nil {
cfg = rest.CopyConfig(cfg)
cfg.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
cfg.ContentType = "application/vnd.kubernetes.protobuf"
mdataclient, err := metadata.NewForConfig(cfg)
if err != nil {
return nil, fmt.Errorf("creating metadata clientset client: %w", err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Server struct {

// NewServer returns a functional Server.
func NewServer(name string, config *rest.Config, kubeConfig, certFile, keyFile string) (*Server, error) {
config = rest.CopyConfig(config)
config.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
config.ContentType = "application/vnd.kubernetes.protobuf"
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ func New(
telemetryMatches []string,
a *manifests.Assets,
) (*Operator, error) {
config = rest.CopyConfig(config)
config.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
config.ContentType = "application/vnd.kubernetes.protobuf"
kclient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("creating kubernetes clientset client: %w", err)
Expand Down

0 comments on commit d6bb142

Please # to comment.