mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
Display pod tolerations with kubectl describe pod.
This commit is contained in:
parent
b159e29da2
commit
230251b030
@ -542,6 +542,7 @@ func describePod(pod *api.Pod, events *api.EventList) (string, error) {
|
|||||||
}
|
}
|
||||||
describeVolumes(pod.Spec.Volumes, out, "")
|
describeVolumes(pod.Spec.Volumes, out, "")
|
||||||
fmt.Fprintf(out, "QoS Class:\t%s\n", qos.GetPodQOS(pod))
|
fmt.Fprintf(out, "QoS Class:\t%s\n", qos.GetPodQOS(pod))
|
||||||
|
printTolerationsInAnnotationMultiline(out, "Tolerations", pod.Annotations)
|
||||||
if events != nil {
|
if events != nil {
|
||||||
DescribeEvents(events, out)
|
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++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -18,6 +18,7 @@ package kubectl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"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) {
|
func TestDescribeService(t *testing.T) {
|
||||||
fake := testclient.NewSimpleFake(&api.Service{
|
fake := testclient.NewSimpleFake(&api.Service{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Loading…
Reference in New Issue
Block a user