Merge pull request #30381 from aveshagarwal/kubectl-describe-pod-display-tolerations

Automatic merge from submit-queue

Display pod tolerations with kubectl describe pod

@kubernetes/rh-cluster-infra @kubernetes/kubectl

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.kubernetes.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.kubernetes.io/reviews/kubernetes/kubernetes/30381)
<!-- Reviewable:end -->
pull/6/head
Kubernetes Submit Queue 2016-08-11 19:45:15 -07:00 committed by GitHub
commit 922477f987
2 changed files with 73 additions and 0 deletions

View File

@ -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, "<none>")
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++
}
}
}
}

View File

@ -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{