mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-10-30 21:30:16 +00:00 
			
		
		
		
	add flag --no-headers to kubectl top ...
				
					
				
			This commit is contained in:
		| @@ -40,6 +40,7 @@ import ( | |||||||
| type TopNodeOptions struct { | type TopNodeOptions struct { | ||||||
| 	ResourceName    string | 	ResourceName    string | ||||||
| 	Selector        string | 	Selector        string | ||||||
|  | 	NoHeaders       bool | ||||||
| 	NodeClient      corev1client.CoreV1Interface | 	NodeClient      corev1client.CoreV1Interface | ||||||
| 	HeapsterOptions HeapsterTopOptions | 	HeapsterOptions HeapsterTopOptions | ||||||
| 	Client          *metricsutil.HeapsterMetricsClient | 	Client          *metricsutil.HeapsterMetricsClient | ||||||
| @@ -118,6 +119,8 @@ func NewCmdTopNode(f cmdutil.Factory, o *TopNodeOptions, streams genericclioptio | |||||||
| 		Aliases: []string{"nodes", "no"}, | 		Aliases: []string{"nodes", "no"}, | ||||||
| 	} | 	} | ||||||
| 	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().StringVarP(&o.Selector, "selector", "l", o.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)") | ||||||
|  | 	cmd.Flags().BoolVar(&o.NoHeaders, "no-headers", o.NoHeaders, "If present, print output without headers") | ||||||
|  |  | ||||||
| 	o.HeapsterOptions.Bind(cmd.Flags()) | 	o.HeapsterOptions.Bind(cmd.Flags()) | ||||||
| 	return cmd | 	return cmd | ||||||
| } | } | ||||||
| @@ -216,7 +219,7 @@ func (o TopNodeOptions) RunTopNode() error { | |||||||
| 		allocatable[n.Name] = n.Status.Allocatable | 		allocatable[n.Name] = n.Status.Allocatable | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return o.Printer.PrintNodeMetrics(metrics.Items, allocatable) | 	return o.Printer.PrintNodeMetrics(metrics.Items, allocatable, o.NoHeaders) | ||||||
| } | } | ||||||
|  |  | ||||||
| func getNodeMetricsFromMetricsAPI(metricsClient metricsclientset.Interface, resourceName string, selector labels.Selector) (*metricsapi.NodeMetricsList, error) { | func getNodeMetricsFromMetricsAPI(metricsClient metricsclientset.Interface, resourceName string, selector labels.Selector) (*metricsapi.NodeMetricsList, error) { | ||||||
|   | |||||||
| @@ -81,6 +81,7 @@ func TestTopNodeAllMetrics(t *testing.T) { | |||||||
| 	streams, _, buf, _ := genericclioptions.NewTestIOStreams() | 	streams, _, buf, _ := genericclioptions.NewTestIOStreams() | ||||||
|  |  | ||||||
| 	cmd := NewCmdTopNode(tf, nil, streams) | 	cmd := NewCmdTopNode(tf, nil, streams) | ||||||
|  | 	cmd.Flags().Set("no-headers", "true") | ||||||
| 	cmd.Run(cmd, []string{}) | 	cmd.Run(cmd, []string{}) | ||||||
|  |  | ||||||
| 	// Check the presence of node names in the output. | 	// Check the presence of node names in the output. | ||||||
| @@ -90,6 +91,9 @@ func TestTopNodeAllMetrics(t *testing.T) { | |||||||
| 			t.Errorf("missing metrics for %s: \n%s", m.Name, result) | 			t.Errorf("missing metrics for %s: \n%s", m.Name, result) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | 	if strings.Contains(result, "MEMORY") { | ||||||
|  | 		t.Errorf("should not print headers with --no-headers option set:\n%s\n", result) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestTopNodeAllMetricsCustomDefaults(t *testing.T) { | func TestTopNodeAllMetricsCustomDefaults(t *testing.T) { | ||||||
|   | |||||||
| @@ -45,6 +45,7 @@ type TopPodOptions struct { | |||||||
| 	Selector        string | 	Selector        string | ||||||
| 	AllNamespaces   bool | 	AllNamespaces   bool | ||||||
| 	PrintContainers bool | 	PrintContainers bool | ||||||
|  | 	NoHeaders       bool | ||||||
| 	PodClient       corev1client.PodsGetter | 	PodClient       corev1client.PodsGetter | ||||||
| 	HeapsterOptions HeapsterTopOptions | 	HeapsterOptions HeapsterTopOptions | ||||||
| 	Client          *metricsutil.HeapsterMetricsClient | 	Client          *metricsutil.HeapsterMetricsClient | ||||||
| @@ -109,6 +110,7 @@ func NewCmdTopPod(f cmdutil.Factory, o *TopPodOptions, streams genericclioptions | |||||||
| 	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().StringVarP(&o.Selector, "selector", "l", o.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)") | ||||||
| 	cmd.Flags().BoolVar(&o.PrintContainers, "containers", o.PrintContainers, "If present, print usage of containers within a pod.") | 	cmd.Flags().BoolVar(&o.PrintContainers, "containers", o.PrintContainers, "If present, print usage of containers within a pod.") | ||||||
| 	cmd.Flags().BoolVar(&o.AllNamespaces, "all-namespaces", 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.AllNamespaces, "all-namespaces", 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.") | ||||||
| 	o.HeapsterOptions.Bind(cmd.Flags()) | 	o.HeapsterOptions.Bind(cmd.Flags()) | ||||||
| 	return cmd | 	return cmd | ||||||
| } | } | ||||||
| @@ -198,7 +200,7 @@ func (o TopPodOptions) RunTopPod() error { | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return o.Printer.PrintPodMetrics(metrics.Items, o.PrintContainers, o.AllNamespaces) | 	return o.Printer.PrintPodMetrics(metrics.Items, o.PrintContainers, o.AllNamespaces, o.NoHeaders) | ||||||
| } | } | ||||||
|  |  | ||||||
| func getMetricsFromMetricsAPI(metricsClient metricsclientset.Interface, namespace, resourceName string, allNamespaces bool, selector labels.Selector) (*metricsapi.PodMetricsList, error) { | func getMetricsFromMetricsAPI(metricsClient metricsclientset.Interface, namespace, resourceName string, allNamespaces bool, selector labels.Selector) (*metricsapi.PodMetricsList, error) { | ||||||
|   | |||||||
| @@ -37,6 +37,7 @@ import ( | |||||||
| 	"k8s.io/client-go/rest/fake" | 	"k8s.io/client-go/rest/fake" | ||||||
| 	core "k8s.io/client-go/testing" | 	core "k8s.io/client-go/testing" | ||||||
| 	cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing" | 	cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing" | ||||||
|  | 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||||
| 	metricsv1alpha1api "k8s.io/metrics/pkg/apis/metrics/v1alpha1" | 	metricsv1alpha1api "k8s.io/metrics/pkg/apis/metrics/v1alpha1" | ||||||
| 	metricsv1beta1api "k8s.io/metrics/pkg/apis/metrics/v1beta1" | 	metricsv1beta1api "k8s.io/metrics/pkg/apis/metrics/v1beta1" | ||||||
| @@ -132,6 +133,14 @@ func TestTopPod(t *testing.T) { | |||||||
| 			namespaces:   []string{testNS}, | 			namespaces:   []string{testNS}, | ||||||
| 			containers:   true, | 			containers:   true, | ||||||
| 		}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:         "no-headers set", | ||||||
|  | 			flags:        map[string]string{"containers": "true", "no-headers": "true"}, | ||||||
|  | 			args:         []string{"pod1"}, | ||||||
|  | 			expectedPath: topPathPrefix + "/namespaces/" + testNS + "/pods/pod1", | ||||||
|  | 			namespaces:   []string{testNS}, | ||||||
|  | 			containers:   true, | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
| 	initTestErrorHandler(t) | 	initTestErrorHandler(t) | ||||||
| 	for _, testCase := range testCases { | 	for _, testCase := range testCases { | ||||||
| @@ -221,6 +230,9 @@ func TestTopPod(t *testing.T) { | |||||||
| 					t.Errorf("%s: unexpected metrics for %s: \n%s", testCase.name, name, result) | 					t.Errorf("%s: unexpected metrics for %s: \n%s", testCase.name, name, result) | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|  | 			if cmdutil.GetFlagBool(cmd, "no-headers") && strings.Contains(result, "MEMORY") { | ||||||
|  | 				t.Errorf("%s: unexpected headers with no-headers option set: \n%s", testCase.name, result) | ||||||
|  | 			} | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -53,7 +53,7 @@ func NewTopCmdPrinter(out io.Writer) *TopCmdPrinter { | |||||||
| 	return &TopCmdPrinter{out: out} | 	return &TopCmdPrinter{out: out} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (printer *TopCmdPrinter) PrintNodeMetrics(metrics []metricsapi.NodeMetrics, availableResources map[string]v1.ResourceList) error { | func (printer *TopCmdPrinter) PrintNodeMetrics(metrics []metricsapi.NodeMetrics, availableResources map[string]v1.ResourceList, noHeaders bool) error { | ||||||
| 	if len(metrics) == 0 { | 	if len(metrics) == 0 { | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
| @@ -63,8 +63,9 @@ func (printer *TopCmdPrinter) PrintNodeMetrics(metrics []metricsapi.NodeMetrics, | |||||||
| 	sort.Slice(metrics, func(i, j int) bool { | 	sort.Slice(metrics, func(i, j int) bool { | ||||||
| 		return metrics[i].Name < metrics[j].Name | 		return metrics[i].Name < metrics[j].Name | ||||||
| 	}) | 	}) | ||||||
|  | 	if !noHeaders { | ||||||
| 		printColumnNames(w, NodeColumns) | 		printColumnNames(w, NodeColumns) | ||||||
|  | 	} | ||||||
| 	var usage v1.ResourceList | 	var usage v1.ResourceList | ||||||
| 	for _, m := range metrics { | 	for _, m := range metrics { | ||||||
| 		err := scheme.Scheme.Convert(&m.Usage, &usage, nil) | 		err := scheme.Scheme.Convert(&m.Usage, &usage, nil) | ||||||
| @@ -86,19 +87,21 @@ func (printer *TopCmdPrinter) PrintNodeMetrics(metrics []metricsapi.NodeMetrics, | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func (printer *TopCmdPrinter) PrintPodMetrics(metrics []metricsapi.PodMetrics, printContainers bool, withNamespace bool) error { | func (printer *TopCmdPrinter) PrintPodMetrics(metrics []metricsapi.PodMetrics, printContainers bool, withNamespace bool, noHeaders bool) error { | ||||||
| 	if len(metrics) == 0 { | 	if len(metrics) == 0 { | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
| 	w := printers.GetNewTabWriter(printer.out) | 	w := printers.GetNewTabWriter(printer.out) | ||||||
| 	defer w.Flush() | 	defer w.Flush() | ||||||
|  | 	if !noHeaders { | ||||||
| 		if withNamespace { | 		if withNamespace { | ||||||
| 			printValue(w, NamespaceColumn) | 			printValue(w, NamespaceColumn) | ||||||
| 		} | 		} | ||||||
| 		if printContainers { | 		if printContainers { | ||||||
| 			printValue(w, PodColumn) | 			printValue(w, PodColumn) | ||||||
| 		} | 		} | ||||||
|  | 		printColumnNames(w, PodColumns) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	sort.Slice(metrics, func(i, j int) bool { | 	sort.Slice(metrics, func(i, j int) bool { | ||||||
| 		if withNamespace && metrics[i].Namespace != metrics[j].Namespace { | 		if withNamespace && metrics[i].Namespace != metrics[j].Namespace { | ||||||
| @@ -106,8 +109,6 @@ func (printer *TopCmdPrinter) PrintPodMetrics(metrics []metricsapi.PodMetrics, p | |||||||
| 		} | 		} | ||||||
| 		return metrics[i].Name < metrics[j].Name | 		return metrics[i].Name < metrics[j].Name | ||||||
| 	}) | 	}) | ||||||
|  |  | ||||||
| 	printColumnNames(w, PodColumns) |  | ||||||
| 	for _, m := range metrics { | 	for _, m := range metrics { | ||||||
| 		err := printSinglePodMetrics(w, &m, printContainers, withNamespace) | 		err := printSinglePodMetrics(w, &m, printContainers, withNamespace) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user