Merge pull request #96655 from serathius/kubectl-top-protobuf

Use protobuf for kubectl top
This commit is contained in:
Kubernetes Prow Robot 2021-03-06 05:33:41 -08:00 committed by GitHub
commit a54414e343
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 11 deletions

View File

@ -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

View File

@ -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