Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --sum flag to kubectl top pod #105100

Merged
merged 1 commit into from May 4, 2022

Conversation

lauchokyip
Copy link
Member

@lauchokyip lauchokyip commented Sep 17, 2021

What type of PR is this?

/kind feature

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #kubernetes/kubectl#1061

Special notes for your reviewer:

 $ ./kubectl top pods --sum -A
NAMESPACE        NAME                                          CPU(cores)   MEMORY(bytes)   
cert-manager     cert-manager-7998c69865-m5pjn                 11m          14Mi            
cert-manager     cert-manager-cainjector-7b744d56fb-jd66q      12m          19Mi            
cert-manager     cert-manager-webhook-97f8b47bc-4smlq          14m          8Mi             
default          personal-website-deployment-b6699dcfb-bl6cp   0m           2Mi             
default          personal-website-deployment-b6699dcfb-rnv7t   0m           2Mi             
default          personal-website-deployment-b6699dcfb-z5lm2   0m           2Mi             
gallery          gallery-deployment-8db9454f9-6rlkk            1m           8Mi             
kube-system      coredns-7448499f4d-5p4ht                      9m           9Mi             
kube-system      local-path-provisioner-5ff76fc89d-8qct7       4m           5Mi             
kube-system      metrics-server-6995b8fd7d-hczx7               16m          12Mi            
kube-system      nginx-ingress-controller-699bf59c5-j6dfw      6m           74Mi            
metallb-system   metallb-controller-df647b67b-q84kg            1m           3Mi             
metallb-system   metallb-speaker-6sk9z                         2m           6Mi             
metallb-system   metallb-speaker-qxbrs                         3m           4Mi             
web-analytics    mysql-bb9597d6f-5czf9                         2m           64Mi            
web-analytics    web-analytics-deployment-75d65dd4f8-5zrwq     1m           31Mi            
web-analytics    web-analytics-deployment-75d65dd4f8-fv2s2     1m           29Mi            
web-analytics    web-analytics-deployment-75d65dd4f8-xs4l5     1m           32Mi            
                                                               ________     ________        
                                                               74m          333Mi     

Does this PR introduce a user-facing change?

Added sum feature to `kubectl top pod`

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Sep 17, 2021
@k8s-ci-robot k8s-ci-robot added area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Sep 17, 2021
@@ -52,114 +52,6 @@ func NewTopCmdPrinter(out io.Writer) *TopCmdPrinter {
return &TopCmdPrinter{out: out}
}

type NodeMetricsSorter struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move sorter out of metrics_printer to metrics_sorter because it clutters the printer file

if len(metrics) == 0 {
return nil
}
w := printers.GetNewTabWriter(printer.out)
defer w.Flush()
if !noHeaders {
if withNamespace {
printValue(w, NamespaceColumn)
PodColumns = append([]string{NamespaceColumn}, PodColumns...)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used append instead so we can know how many columns to skip

// skipping len(PodCoumns)-2 columns
for i := 0; i < len(PodColumns)-2; i++ {
		printValue(out, "")
	}

@lauchokyip
Copy link
Member Author

/assign @eddiezane

@lauchokyip
Copy link
Member Author

/triage accepted
/priority backlog

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/backlog Higher priority than priority/awaiting-more-evidence. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Sep 17, 2021
@lauchokyip
Copy link
Member Author

/retest

@pacoxu pacoxu added this to Needs review in SIG CLI Sep 27, 2021
if len(metrics) == 0 {
return nil
}
w := printers.GetNewTabWriter(printer.out)
defer w.Flush()
if !noHeaders {
if withNamespace {
printValue(w, NamespaceColumn)
PodColumns = append([]string{NamespaceColumn}, PodColumns...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we do this without mutating the globally scoped variable here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can keep track of how many PodColums will be outputted using a local variable I think. Will try that approach

Copy link
Member

@eddiezane eddiezane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

I am assuming this a quirk of how we round somewhere?

NAME                            CPU(cores)   MEMORY(bytes)
hass-597bc475f5-knvr2           50m          276Mi
mosquitto-564d776dcd-2vdbw      1m           2Mi
zwavejs2mqtt-76c5fbb78c-6mr26   2m           74Mi
                                ________     ________
                                52m          353Mi

@@ -115,6 +116,7 @@ func NewCmdTopPod(f cmdutil.Factory, o *TopPodOptions, streams genericclioptions
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, "Enables using protocol-buffers to access Metrics API.")
cmd.Flags().BoolVar(&o.Sum, "sum", o.Sum, "If present, print the sum of the resource usages")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cmd.Flags().BoolVar(&o.Sum, "sum", o.Sum, "If present, print the sum of the resource usages")
cmd.Flags().BoolVar(&o.Sum, "sum", o.Sum, "Print the sum of the resource usages")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -21,7 +21,7 @@ import (
"io"
"sort"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
v1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"

func (adder *ResourceAdder) AddPodMetrics(m *metricsapi.PodMetrics) {
for _, c := range m.Containers {
for _, res := range adder.resources {
total_val := adder.total[res]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: camelCase

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}
}

// AddSinglePodMetrics will add each pod metrics to the total
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// AddSinglePodMetrics will add each pod metrics to the total
// AddPodMetrics will add each pod metric to the total

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -310,3 +216,21 @@ func printSingleResourceUsage(out io.Writer, resourceType v1.ResourceName, quant
fmt.Fprintf(out, "%v", quantity.Value())
}
}

func printPodResourcesSum(out io.Writer, total v1.ResourceList, withNamespace, printContainers bool, columnWidth int) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withNamespace and printContainers are unused.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

func getResourceQuantity(t *testing.T, quantityStr string) resource.Quantity {
t.Helper()
var err error
quantity, _ := resource.ParseQuantity("0")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future sanity let's check this error.

@@ -0,0 +1,130 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you make any changes aside from moving this file out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I didn't

@lauchokyip
Copy link
Member Author

Looking good!

I am assuming this a quirk of how we round somewhere?

NAME                            CPU(cores)   MEMORY(bytes)
hass-597bc475f5-knvr2           50m          276Mi
mosquitto-564d776dcd-2vdbw      1m           2Mi
zwavejs2mqtt-76c5fbb78c-6mr26   2m           74Mi
                                ________     ________
                                52m          353Mi

@eddiezane yup, I believe we are just getting the estimate value over here

@dims
Copy link
Member

dims commented Jan 5, 2022

/assign @KnVerey

Copy link
Member

@eddiezane eddiezane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 30, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: eddiezane, lauchokyip

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 30, 2022
@eddiezane
Copy link
Member

Flake is #109133

/retest

@k8s-ci-robot k8s-ci-robot merged commit 0401cc2 into kubernetes:master May 4, 2022
SIG CLI automation moved this from Needs review to Done May 4, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.25 milestone May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants