robscott / kube-capacity

A simple CLI that provides an overview of the resource requests, limits, and utilization in a Kubernetes cluster
Apache License 2.0
2.15k stars 115 forks source link

--namespace and --namespace-labels do not filter --util #52

Closed mbarbero closed 2 years ago

mbarbero commented 3 years ago

I was expecting to see cpu/memory util only for pods in the given namespace, but it seems to include all pods, e.g.:

$ kube-capacity -u -n my-namespace
NODE      CPU REQUESTS   CPU LIMITS    CPU UTIL      MEMORY REQUESTS   MEMORY LIMITS   MEMORY UTIL
*         8250m (1%)     20000m (4%)   26768m (5%)   36508Mi (1%)      36508Mi (1%)    434069Mi (17%)
node-1    0Mi (0%)       0Mi (0%)      1124m (32%)   0Mi (0%)          0Mi (0%)        6105Mi (40%)
node-2    0Mi (0%)       0Mi (0%)      1957m (55%)   0Mi (0%)          0Mi (0%)        9312Mi (61%)
node-3    0Mi (0%)       0Mi (0%)      843m (24%)    0Mi (0%)          0Mi (0%)        4347Mi (28%)
node-4    0Mi (0%)       0Mi (0%)      910m (3%)     0Mi (0%)          0Mi (0%)        21097Mi (15%)
node-5    0Mi (0%)       0Mi (0%)      3766m (50%)   0Mi (0%)          0Mi (0%)        8770Mi (27%)

Similar output when filtering with --namespace-labels.

WDYT?

robscott commented 2 years ago

Thanks for the bug report! Sorry for the late response to this one, I've been pretty underwater. This is a surprisingly tough one to get right. The per-node metrics shown here are not actually a sum of pod metrics, they represent the result of a NodeMetrics API call (https://github.com/robscott/kube-capacity/blob/bb976088847ed85e4e9694a24aabe896a5ee4276/pkg/capacity/capacity.go#L135). My understanding of this is that it's a better representation of the full utilization of the node.

When filtering is applied, it likely would make more sense to fall back to a "sum of pod metrics" as the approach here, but that would require some restructuring. Don't have enough time to take this on short term, but I'm always open to PRs if someone does have time.

mbarbero commented 2 years ago

Thanks for the fix. It works very well!