From 560394da38422cf106917b7addc3fa828e746b6b Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Wed, 18 Nov 2020 00:14:20 +0100 Subject: [PATCH] Use protobuf for kubectl top --- .../k8s.io/kubectl/pkg/cmd/top/top_node.go | 17 ++++++++++---- .../src/k8s.io/kubectl/pkg/cmd/top/top_pod.go | 22 +++++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node.go b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node.go index 1a7386623cd..8f47fd08f9f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node.go @@ -27,6 +27,7 @@ import ( "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/discovery" corev1client "k8s.io/client-go/kubernetes/typed/core/v1" + "k8s.io/klog/v2" cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/metricsutil" "k8s.io/kubectl/pkg/util/i18n" @@ -38,10 +39,12 @@ import ( // TopNodeOptions contains all the options for running the top-node cli command. type TopNodeOptions struct { - ResourceName string - Selector string - SortBy string - NoHeaders bool + ResourceName string + Selector string + SortBy string + NoHeaders bool + UseProtocolBuffers bool + NodeClient corev1client.CoreV1Interface Printer *metricsutil.TopCmdPrinter DiscoveryClient discovery.DiscoveryInterface @@ -87,6 +90,7 @@ func NewCmdTopNode(f cmdutil.Factory, o *TopNodeOptions, streams genericclioptio cmd.Flags().StringVarP(&o.Selector, "selector", "l", o.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)") cmd.Flags().StringVar(&o.SortBy, "sort-by", o.Selector, "If non-empty, sort nodes list using specified field. The field can be either 'cpu' or 'memory'.") cmd.Flags().BoolVar(&o.NoHeaders, "no-headers", o.NoHeaders, "If present, print output without headers") + cmd.Flags().BoolVar(&o.UseProtocolBuffers, "use-protocol-buffers", o.UseProtocolBuffers, "If present, protocol-buffers will be used to request metrics.") return cmd } @@ -109,6 +113,11 @@ func (o *TopNodeOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [] if err != nil { return err } + if o.UseProtocolBuffers { + config.ContentType = "application/vnd.kubernetes.protobuf" + } else { + klog.Warning("Using json format to get metrics. Next release will switch to protocol-buffers, switch early by passing --use-protocol-buffers flag") + } o.MetricsClient, err = metricsclientset.NewForConfig(config) if err != nil { return err diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod.go b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod.go index 3b9bd1296da..9921fefe2d6 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod.go @@ -41,13 +41,15 @@ import ( ) type TopPodOptions struct { - ResourceName string - Namespace string - Selector string - SortBy string - AllNamespaces bool - PrintContainers bool - NoHeaders bool + ResourceName string + Namespace string + Selector string + SortBy string + AllNamespaces bool + PrintContainers bool + NoHeaders bool + UseProtocolBuffers bool + PodClient corev1client.PodsGetter Printer *metricsutil.TopCmdPrinter DiscoveryClient discovery.DiscoveryInterface @@ -106,6 +108,7 @@ func NewCmdTopPod(f cmdutil.Factory, o *TopPodOptions, streams genericclioptions cmd.Flags().BoolVar(&o.PrintContainers, "containers", o.PrintContainers, "If present, print usage of containers within a pod.") cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", o.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.") cmd.Flags().BoolVar(&o.NoHeaders, "no-headers", o.NoHeaders, "If present, print output without headers.") + cmd.Flags().BoolVar(&o.UseProtocolBuffers, "use-protocol-buffers", o.UseProtocolBuffers, "If present, protocol-buffers will be used to request metrics.") return cmd } @@ -131,6 +134,11 @@ func (o *TopPodOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []s if err != nil { return err } + if o.UseProtocolBuffers { + config.ContentType = "application/vnd.kubernetes.protobuf" + } else { + klog.Warning("Using json format to get metrics. Next release will switch to protocol-buffers, switch early by passing --use-protocol-buffers flag") + } o.MetricsClient, err = metricsclientset.NewForConfig(config) if err != nil { return err