Merge pull request #53560 from dixudx/sort_kubectl_top_metrics

Automatic merge from submit-queue (batch tested with PRs 53565, 53560). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

sort top pod and top node output by namespace/name

**What this PR does / why we need it**:

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #53513 

**Special notes for your reviewer**:
/assign @smarterclayton 

**Release note**:

```release-note
kubectl top pod and node should sort by namespace / name so that results don't jump around.
```
This commit is contained in:
Kubernetes Submit Queue 2017-10-08 20:45:13 -07:00 committed by GitHub
commit 50377e88ea

View File

@ -19,6 +19,7 @@ package metricsutil
import (
"fmt"
"io"
"sort"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/kubernetes/pkg/api"
@ -58,6 +59,10 @@ func (printer *TopCmdPrinter) PrintNodeMetrics(metrics []metricsapi.NodeMetrics,
w := printers.GetNewTabWriter(printer.out)
defer w.Flush()
sort.Slice(metrics, func(i, j int) bool {
return metrics[i].Name < metrics[j].Name
})
printColumnNames(w, NodeColumns)
var usage api.ResourceList
for _, m := range metrics {
@ -87,6 +92,14 @@ func (printer *TopCmdPrinter) PrintPodMetrics(metrics []metricsapi.PodMetrics, p
if printContainers {
printValue(w, PodColumn)
}
sort.Slice(metrics, func(i, j int) bool {
if withNamespace && metrics[i].Namespace != metrics[j].Namespace {
return metrics[i].Namespace < metrics[j].Namespace
}
return metrics[i].Name < metrics[j].Name
})
printColumnNames(w, PodColumns)
for _, m := range metrics {
err := printSinglePodMetrics(w, &m, printContainers, withNamespace)