From 862937bf1c7975d3f54ae47a2958e47f2c50150f Mon Sep 17 00:00:00 2001 From: Daein Park Date: Wed, 16 Jun 2021 21:25:52 +0900 Subject: [PATCH] Add a flag to enable "Capacity" instead of "Allocatable" for an actual node memory total usage. If "Allocatable" is used to a node total memory size, under high memory pressure or pre-reserved memory value is bigger, the "MEMORY%" can be bigger than 100%. For suppressing the confusing, add a option to show node real memory usage based on "Capacity". * Reference: https://github.com/kubernetes/kubernetes/issues/86499 --- staging/src/k8s.io/kubectl/pkg/cmd/top/top_node.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 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 2f530d78f51..667fa30c728 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 @@ -43,6 +43,7 @@ type TopNodeOptions struct { SortBy string NoHeaders bool UseProtocolBuffers bool + ShowCapacity bool NodeClient corev1client.CoreV1Interface Printer *metricsutil.TopCmdPrinter @@ -91,6 +92,7 @@ func NewCmdTopNode(f cmdutil.Factory, o *TopNodeOptions, streams genericclioptio cmd.Flags().StringVar(&o.SortBy, "sort-by", o.SortBy, "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, "Enables using protocol-buffers to access Metrics API.") + cmd.Flags().BoolVar(&o.ShowCapacity, "show-capacity", o.ShowCapacity, "Print node resources based on Capacity instead of Allocatable(default) of the nodes.") return cmd } @@ -186,13 +188,17 @@ func (o TopNodeOptions) RunTopNode() error { nodes = append(nodes, nodeList.Items...) } - allocatable := make(map[string]v1.ResourceList) + availableResources := make(map[string]v1.ResourceList) for _, n := range nodes { - allocatable[n.Name] = n.Status.Allocatable + if !o.ShowCapacity { + availableResources[n.Name] = n.Status.Allocatable + } else { + availableResources[n.Name] = n.Status.Capacity + } } - return o.Printer.PrintNodeMetrics(metrics.Items, allocatable, o.NoHeaders, o.SortBy) + return o.Printer.PrintNodeMetrics(metrics.Items, availableResources, o.NoHeaders, o.SortBy) } func getNodeMetricsFromMetricsAPI(metricsClient metricsclientset.Interface, resourceName string, selector labels.Selector) (*metricsapi.NodeMetricsList, error) {