mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-19 17:16:12 +00:00
Remove an empty line being output when exposing annotations and
labels via downward api volume
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/meta"
|
||||
@@ -27,10 +28,11 @@ import (
|
||||
)
|
||||
|
||||
// formatMap formats map[string]string to a string.
|
||||
func formatMap(m map[string]string) (fmtStr string) {
|
||||
func FormatMap(m map[string]string) (fmtStr string) {
|
||||
for key, value := range m {
|
||||
fmtStr += fmt.Sprintf("%v=%q\n", key, value)
|
||||
}
|
||||
fmtStr = strings.TrimSuffix(fmtStr, "\n")
|
||||
|
||||
return
|
||||
}
|
||||
@@ -51,9 +53,9 @@ func ExtractFieldPathAsString(obj interface{}, fieldPath string) (string, error)
|
||||
|
||||
switch fieldPath {
|
||||
case "metadata.annotations":
|
||||
return formatMap(accessor.GetAnnotations()), nil
|
||||
return FormatMap(accessor.GetAnnotations()), nil
|
||||
case "metadata.labels":
|
||||
return formatMap(accessor.GetLabels()), nil
|
||||
return FormatMap(accessor.GetLabels()), nil
|
||||
case "metadata.name":
|
||||
return accessor.GetName(), nil
|
||||
case "metadata.namespace":
|
||||
|
@@ -65,7 +65,7 @@ func TestExtractFieldPathAsString(t *testing.T) {
|
||||
Labels: map[string]string{"key": "value"},
|
||||
},
|
||||
},
|
||||
expectedValue: "key=\"value\"\n",
|
||||
expectedValue: "key=\"value\"",
|
||||
},
|
||||
{
|
||||
name: "ok - labels bslash n",
|
||||
@@ -75,7 +75,7 @@ func TestExtractFieldPathAsString(t *testing.T) {
|
||||
Labels: map[string]string{"key": "value\n"},
|
||||
},
|
||||
},
|
||||
expectedValue: "key=\"value\\n\"\n",
|
||||
expectedValue: "key=\"value\\n\"",
|
||||
},
|
||||
{
|
||||
name: "ok - annotations",
|
||||
@@ -85,7 +85,7 @@ func TestExtractFieldPathAsString(t *testing.T) {
|
||||
Annotations: map[string]string{"builder": "john-doe"},
|
||||
},
|
||||
},
|
||||
expectedValue: "builder=\"john-doe\"\n",
|
||||
expectedValue: "builder=\"john-doe\"",
|
||||
},
|
||||
|
||||
{
|
||||
|
Reference in New Issue
Block a user