From 230251b030fa2511a3bf12adb187afc6daff3c39 Mon Sep 17 00:00:00 2001 From: Avesh Agarwal Date: Wed, 10 Aug 2016 13:39:11 -0400 Subject: [PATCH] Display pod tolerations with kubectl describe pod. --- pkg/kubectl/describe.go | 47 ++++++++++++++++++++++++++++++++++++ pkg/kubectl/describe_test.go | 26 ++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/pkg/kubectl/describe.go b/pkg/kubectl/describe.go index 96ec6868435..5b6aebb4118 100644 --- a/pkg/kubectl/describe.go +++ b/pkg/kubectl/describe.go @@ -542,6 +542,7 @@ func describePod(pod *api.Pod, events *api.EventList) (string, error) { } describeVolumes(pod.Spec.Volumes, out, "") fmt.Fprintf(out, "QoS Class:\t%s\n", qos.GetPodQOS(pod)) + printTolerationsInAnnotationMultiline(out, "Tolerations", pod.Annotations) if events != nil { DescribeEvents(events, out) } @@ -2474,3 +2475,49 @@ func printTaintsMultilineWithIndent(out io.Writer, initialIndent, title, innerIn } } } + +// printTolerationsMultiline prints multiple tolerations with a proper alignment. +func printTolerationsInAnnotationMultiline(out io.Writer, title string, annotations map[string]string) { + tolerations, err := api.GetTolerationsFromPodAnnotations(annotations) + if err != nil { + tolerations = []api.Toleration{} + } + printTolerationsMultilineWithIndent(out, "", title, "\t", tolerations) +} + +// printTolerationsMultilineWithIndent prints multiple tolerations with a user-defined alignment. +func printTolerationsMultilineWithIndent(out io.Writer, initialIndent, title, innerIndent string, tolerations []api.Toleration) { + fmt.Fprintf(out, "%s%s:%s", initialIndent, title, innerIndent) + + if tolerations == nil || len(tolerations) == 0 { + fmt.Fprintln(out, "") + return + } + + // to print tolerations in the sorted order + keys := make([]string, 0, len(tolerations)) + for _, toleration := range tolerations { + keys = append(keys, toleration.Key) + } + sort.Strings(keys) + + for i, key := range keys { + for _, toleration := range tolerations { + if toleration.Key == key { + if i != 0 { + fmt.Fprint(out, initialIndent) + fmt.Fprint(out, innerIndent) + } + fmt.Fprintf(out, "%s=%s", toleration.Key, toleration.Value) + if len(toleration.Operator) != 0 { + fmt.Fprintf(out, ":%s", toleration.Operator) + } + if len(toleration.Effect) != 0 { + fmt.Fprintf(out, ":%s", toleration.Effect) + } + fmt.Fprintf(out, "\n") + i++ + } + } + } +} diff --git a/pkg/kubectl/describe_test.go b/pkg/kubectl/describe_test.go index 2d0e9d698ba..fab6ccf6c4f 100644 --- a/pkg/kubectl/describe_test.go +++ b/pkg/kubectl/describe_test.go @@ -18,6 +18,7 @@ package kubectl import ( "bytes" + "encoding/json" "fmt" "reflect" "strings" @@ -60,6 +61,31 @@ func TestDescribePod(t *testing.T) { } } +func TestDescribePodTolerations(t *testing.T) { + + podTolerations := []api.Toleration{{Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}} + pt, _ := json.Marshal(podTolerations) + fake := testclient.NewSimpleFake(&api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "bar", + Namespace: "foo", + Annotations: map[string]string{ + api.TolerationsAnnotationKey: string(pt), + }, + }, + }) + c := &describeClient{T: t, Namespace: "foo", Interface: fake} + d := PodDescriber{c} + out, err := d.Describe("foo", "bar", DescriberSettings{}) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if !strings.Contains(out, "key1=value1") || !strings.Contains(out, "key2=value2") || !strings.Contains(out, "Tolerations:") { + t.Errorf("unexpected out: %s", out) + } +} + func TestDescribeService(t *testing.T) { fake := testclient.NewSimpleFake(&api.Service{ ObjectMeta: api.ObjectMeta{