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" "fmt"
"math" "math"
"strconv" "strconv"
"strings"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/meta"
@ -27,10 +28,11 @@ import (
) )
// formatMap formats map[string]string to a string. // 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 { for key, value := range m {
fmtStr += fmt.Sprintf("%v=%q\n", key, value) fmtStr += fmt.Sprintf("%v=%q\n", key, value)
} }
fmtStr = strings.TrimSuffix(fmtStr, "\n")
return return
} }
@ -51,9 +53,9 @@ func ExtractFieldPathAsString(obj interface{}, fieldPath string) (string, error)
switch fieldPath { switch fieldPath {
case "metadata.annotations": case "metadata.annotations":
return formatMap(accessor.GetAnnotations()), nil return FormatMap(accessor.GetAnnotations()), nil
case "metadata.labels": case "metadata.labels":
return formatMap(accessor.GetLabels()), nil return FormatMap(accessor.GetLabels()), nil
case "metadata.name": case "metadata.name":
return accessor.GetName(), nil return accessor.GetName(), nil
case "metadata.namespace": case "metadata.namespace":

View File

@ -65,7 +65,7 @@ func TestExtractFieldPathAsString(t *testing.T) {
Labels: map[string]string{"key": "value"}, Labels: map[string]string{"key": "value"},
}, },
}, },
expectedValue: "key=\"value\"\n", expectedValue: "key=\"value\"",
}, },
{ {
name: "ok - labels bslash n", name: "ok - labels bslash n",
@ -75,7 +75,7 @@ func TestExtractFieldPathAsString(t *testing.T) {
Labels: map[string]string{"key": "value\n"}, Labels: map[string]string{"key": "value\n"},
}, },
}, },
expectedValue: "key=\"value\\n\"\n", expectedValue: "key=\"value\\n\"",
}, },
{ {
name: "ok - annotations", name: "ok - annotations",
@ -85,7 +85,7 @@ func TestExtractFieldPathAsString(t *testing.T) {
Annotations: map[string]string{"builder": "john-doe"}, Annotations: map[string]string{"builder": "john-doe"},
}, },
}, },
expectedValue: "builder=\"john-doe\"\n", expectedValue: "builder=\"john-doe\"",
}, },
{ {

View File

@ -26,6 +26,7 @@ import (
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake" "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
"k8s.io/kubernetes/pkg/fieldpath"
"k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/types"
utiltesting "k8s.io/kubernetes/pkg/util/testing" utiltesting "k8s.io/kubernetes/pkg/util/testing"
"k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume"
@ -35,14 +36,6 @@ import (
const downwardAPIDir = "..data" const downwardAPIDir = "..data"
func formatMap(m map[string]string) (fmtstr string) {
for key, value := range m {
fmtstr += fmt.Sprintf("%v=%q\n", key, value)
}
return
}
func newTestHost(t *testing.T, clientset clientset.Interface) (string, volume.VolumeHost) { func newTestHost(t *testing.T, clientset clientset.Interface) (string, volume.VolumeHost) {
tempDir, err := utiltesting.MkTmpdir("downwardApi_volume_test.") tempDir, err := utiltesting.MkTmpdir("downwardApi_volume_test.")
if err != nil { if err != nil {
@ -155,8 +148,8 @@ func TestLabels(t *testing.T) {
if err != nil { if err != nil {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if sortLines(string(data)) != sortLines(formatMap(labels)) { if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels)) t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
} }
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t) CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
@ -222,8 +215,8 @@ func TestAnnotations(t *testing.T) {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if sortLines(string(data)) != sortLines(formatMap(annotations)) { if sortLines(string(data)) != sortLines(fieldpath.FormatMap(annotations)) {
t.Errorf("Found `%s` expected %s", data, formatMap(annotations)) t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(annotations))
} }
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t) CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
@ -433,8 +426,8 @@ func TestWriteTwiceNoUpdate(t *testing.T) {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if sortLines(string(data)) != sortLines(formatMap(labels)) { if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels)) t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
} }
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t) CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
@ -503,8 +496,8 @@ func TestWriteTwiceWithUpdate(t *testing.T) {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if sortLines(string(data)) != sortLines(formatMap(labels)) { if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels)) t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
} }
newLabels := map[string]string{ newLabels := map[string]string{
@ -534,8 +527,8 @@ func TestWriteTwiceWithUpdate(t *testing.T) {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if sortLines(string(data)) != sortLines(formatMap(newLabels)) { if sortLines(string(data)) != sortLines(fieldpath.FormatMap(newLabels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(newLabels)) t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(newLabels))
} }
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t) CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
} }
@ -606,16 +599,16 @@ func TestWriteWithUnixPath(t *testing.T) {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if sortLines(string(data)) != sortLines(formatMap(labels)) { if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels)) t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
} }
data, err = ioutil.ReadFile(path.Join(volumePath, "this/is/yours/annotations")) data, err = ioutil.ReadFile(path.Join(volumePath, "this/is/yours/annotations"))
if err != nil { if err != nil {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if sortLines(string(data)) != sortLines(formatMap(annotations)) { if sortLines(string(data)) != sortLines(fieldpath.FormatMap(annotations)) {
t.Errorf("Found `%s` expected %s", data, formatMap(annotations)) t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(annotations))
} }
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t) CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
} }
@ -687,7 +680,7 @@ func TestWriteWithUnixPathBadPath(t *testing.T) {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
if sortLines(string(data)) != sortLines(formatMap(labels)) { if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels)) t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
} }
} }