Remove an empty line being output when exposing annotations and

labels via downward api volume
This commit is contained in:
Avesh Agarwal
2016-06-03 10:03:01 -04:00
parent d93ebd0e9f
commit 3c865e45a0
3 changed files with 25 additions and 30 deletions

View File

@@ -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":

View File

@@ -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\"",
},
{