kubectl config get-contexts: sort output

This commit is contained in:
Kelly Campbell 2017-06-04 18:06:04 -04:00
parent 6000712803
commit 9f7468599e
2 changed files with 23 additions and 0 deletions

View File

@ -19,6 +19,7 @@ package config
import ( import (
"fmt" "fmt"
"io" "io"
"sort"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
@ -138,6 +139,7 @@ func (o GetContextsOptions) RunGetContexts() error {
} }
} }
sort.Strings(toPrint)
for _, name := range toPrint { for _, name := range toPrint {
err = printContext(name, config.Contexts[name], out, o.nameOnly, config.CurrentContext == name) err = printContext(name, config.Contexts[name], out, o.nameOnly, config.CurrentContext == name)
if err != nil { if err != nil {

View File

@ -66,6 +66,27 @@ func TestGetContextsAllNoHeader(t *testing.T) {
test.run(t) test.run(t)
} }
func TestGetContextsAllSorted(t *testing.T) {
tconf := clientcmdapi.Config{
CurrentContext: "shaker-context",
Contexts: map[string]*clientcmdapi.Context{
"shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
"abc": {AuthInfo: "blue-user", Cluster: "abc-cluster", Namespace: "kube-system"},
"xyz": {AuthInfo: "blue-user", Cluster: "xyz-cluster", Namespace: "default"}}}
test := getContextsTest{
startingConfig: tconf,
names: []string{},
noHeader: false,
nameOnly: false,
expectedOut: `CURRENT NAME CLUSTER AUTHINFO NAMESPACE
abc abc-cluster blue-user kube-system
* shaker-context big-cluster blue-user saw-ns
xyz xyz-cluster blue-user default
`,
}
test.run(t)
}
func TestGetContextsAllName(t *testing.T) { func TestGetContextsAllName(t *testing.T) {
tconf := clientcmdapi.Config{ tconf := clientcmdapi.Config{
Contexts: map[string]*clientcmdapi.Context{ Contexts: map[string]*clientcmdapi.Context{