mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Break annotations with newlines and shorten length
Make annotations with newlines display a more consistent left edge, and indent the value when the annotation is too long to give the value more space. Shorten the width of the trimmed annotation to a value more consistent with our `-o wide` value. Instead of putting the key and value flush with a `=` separator, make annotations closer to fields than to labels by using `: ` as a separator.
This commit is contained in:
parent
b8809dcd67
commit
a23465426d
@ -4078,7 +4078,7 @@ func (list SortableVolumeDevices) Less(i, j int) bool {
|
|||||||
return list[i].DevicePath < list[j].DevicePath
|
return list[i].DevicePath < list[j].DevicePath
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxAnnotationLen = 200
|
var maxAnnotationLen = 140
|
||||||
|
|
||||||
// printAnnotationsMultilineWithFilter prints filtered multiple annotations with a proper alignment.
|
// printAnnotationsMultilineWithFilter prints filtered multiple annotations with a proper alignment.
|
||||||
func printAnnotationsMultilineWithFilter(w PrefixWriter, title string, annotations map[string]string, skip sets.String) {
|
func printAnnotationsMultilineWithFilter(w PrefixWriter, title string, annotations map[string]string, skip sets.String) {
|
||||||
@ -4114,18 +4114,27 @@ func printAnnotationsMultilineWithIndent(w PrefixWriter, initialIndent, title, i
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
sort.Strings(keys)
|
sort.Strings(keys)
|
||||||
|
indent := initialIndent + innerIndent
|
||||||
for i, key := range keys {
|
for i, key := range keys {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
w.Write(LEVEL_0, initialIndent)
|
w.Write(LEVEL_0, indent)
|
||||||
w.Write(LEVEL_0, innerIndent)
|
}
|
||||||
|
value := strings.TrimSuffix(annotations[key], "\n")
|
||||||
|
if (len(value)+len(key)+2) > maxAnnotationLen || strings.Contains(value, "\n") {
|
||||||
|
w.Write(LEVEL_0, "%s:\n", key)
|
||||||
|
for _, s := range strings.Split(value, "\n") {
|
||||||
|
w.Write(LEVEL_0, "%s %s\n", indent, shorten(s, maxAnnotationLen-2))
|
||||||
}
|
}
|
||||||
line := fmt.Sprintf("%s=%s", key, annotations[key])
|
|
||||||
if len(line) > maxAnnotationLen {
|
|
||||||
w.Write(LEVEL_0, "%s...\n", line[:maxAnnotationLen])
|
|
||||||
} else {
|
} else {
|
||||||
w.Write(LEVEL_0, "%s\n", line)
|
w.Write(LEVEL_0, "%s: %s\n", key, value)
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func shorten(s string, maxLength int) string {
|
||||||
|
if len(s) > maxLength {
|
||||||
|
return s[:maxLength] + "..."
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
@ -2144,22 +2144,31 @@ func TestDescribeEvents(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPrintLabelsMultiline(t *testing.T) {
|
func TestPrintLabelsMultiline(t *testing.T) {
|
||||||
var maxLenAnnotationStr string = "MaxLenAnnotation=Multicast addressing can be used in the link layer (Layer 2 in the OSI model), such as Ethernet multicast, and at the internet layer (Layer 3 for OSI) for Internet Protocol Version 4 "
|
key := "MaxLenAnnotation"
|
||||||
|
value := strings.Repeat("a", maxAnnotationLen-len(key)-2)
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
annotations map[string]string
|
annotations map[string]string
|
||||||
expectPrint string
|
expectPrint string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
annotations: map[string]string{"col1": "asd", "COL2": "zxc"},
|
annotations: map[string]string{"col1": "asd", "COL2": "zxc"},
|
||||||
expectPrint: "Annotations:\tCOL2=zxc\n\tcol1=asd\n",
|
expectPrint: "Annotations:\tCOL2: zxc\n\tcol1: asd\n",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
annotations: map[string]string{"MaxLenAnnotation": maxLenAnnotationStr[17:]},
|
annotations: map[string]string{"MaxLenAnnotation": value},
|
||||||
expectPrint: "Annotations:\t" + maxLenAnnotationStr + "\n",
|
expectPrint: fmt.Sprintf("Annotations:\t%s: %s\n", key, value),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
annotations: map[string]string{"MaxLenAnnotation": maxLenAnnotationStr[17:] + "1"},
|
annotations: map[string]string{"MaxLenAnnotation": value + "1"},
|
||||||
expectPrint: "Annotations:\t" + maxLenAnnotationStr + "...\n",
|
expectPrint: fmt.Sprintf("Annotations:\t%s:\n\t %s\n", key, value+"1"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
annotations: map[string]string{"MaxLenAnnotation": value + value},
|
||||||
|
expectPrint: fmt.Sprintf("Annotations:\t%s:\n\t %s\n", key, strings.Repeat("a", maxAnnotationLen-2)+"..."),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
annotations: map[string]string{"key": "value\nwith\nnewlines\n"},
|
||||||
|
expectPrint: "Annotations:\tkey:\n\t value\n\t with\n\t newlines\n",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
annotations: map[string]string{},
|
annotations: map[string]string{},
|
||||||
@ -2167,13 +2176,13 @@ func TestPrintLabelsMultiline(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for i, testCase := range testCases {
|
for i, testCase := range testCases {
|
||||||
t.Run(testCase.expectPrint, func(t *testing.T) {
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||||
out := new(bytes.Buffer)
|
out := new(bytes.Buffer)
|
||||||
writer := NewPrefixWriter(out)
|
writer := NewPrefixWriter(out)
|
||||||
printAnnotationsMultiline(writer, "Annotations", testCase.annotations)
|
printAnnotationsMultiline(writer, "Annotations", testCase.annotations)
|
||||||
output := out.String()
|
output := out.String()
|
||||||
if output != testCase.expectPrint {
|
if output != testCase.expectPrint {
|
||||||
t.Errorf("Test case %d: expected to find %q in output: %q", i, testCase.expectPrint, output)
|
t.Errorf("Test case %d: expected to match:\n%q\nin output:\n%q", i, testCase.expectPrint, output)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user