Skip to content

Commit

Permalink
Remove an empty line being output when exposing annotations and
Browse files Browse the repository at this point in the history
labels via downward api volume
  • Loading branch information
Avesh Agarwal committed Jun 8, 2016
1 parent d93ebd0 commit 3c865e4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
8 changes: 5 additions & 3 deletions pkg/fieldpath/fieldpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ import (
"fmt"
"math"
"strconv"
"strings"

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/resource"
)

// 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
}
Expand All @@ -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":
Expand Down
6 changes: 3 additions & 3 deletions pkg/fieldpath/fieldpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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\"",
},

{
Expand Down
41 changes: 17 additions & 24 deletions pkg/volume/downwardapi/downwardapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/kubernetes/pkg/api"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
"k8s.io/kubernetes/pkg/fieldpath"
"k8s.io/kubernetes/pkg/types"
utiltesting "k8s.io/kubernetes/pkg/util/testing"
"k8s.io/kubernetes/pkg/volume"
Expand All @@ -35,14 +36,6 @@ import (

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) {
tempDir, err := utiltesting.MkTmpdir("downwardApi_volume_test.")
if err != nil {
Expand Down Expand Up @@ -155,8 +148,8 @@ func TestLabels(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}
if sortLines(string(data)) != sortLines(formatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels))
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
}

CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
Expand Down Expand Up @@ -222,8 +215,8 @@ func TestAnnotations(t *testing.T) {
t.Errorf(err.Error())
}

if sortLines(string(data)) != sortLines(formatMap(annotations)) {
t.Errorf("Found `%s` expected %s", data, formatMap(annotations))
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(annotations)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(annotations))
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)

Expand Down Expand Up @@ -433,8 +426,8 @@ func TestWriteTwiceNoUpdate(t *testing.T) {
t.Errorf(err.Error())
}

if sortLines(string(data)) != sortLines(formatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels))
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)

Expand Down Expand Up @@ -503,8 +496,8 @@ func TestWriteTwiceWithUpdate(t *testing.T) {
t.Errorf(err.Error())
}

if sortLines(string(data)) != sortLines(formatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels))
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
}

newLabels := map[string]string{
Expand Down Expand Up @@ -534,8 +527,8 @@ func TestWriteTwiceWithUpdate(t *testing.T) {
t.Errorf(err.Error())
}

if sortLines(string(data)) != sortLines(formatMap(newLabels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(newLabels))
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(newLabels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(newLabels))
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
}
Expand Down Expand Up @@ -606,16 +599,16 @@ func TestWriteWithUnixPath(t *testing.T) {
t.Errorf(err.Error())
}

if sortLines(string(data)) != sortLines(formatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels))
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
}

data, err = ioutil.ReadFile(path.Join(volumePath, "this/is/yours/annotations"))
if err != nil {
t.Errorf(err.Error())
}
if sortLines(string(data)) != sortLines(formatMap(annotations)) {
t.Errorf("Found `%s` expected %s", data, formatMap(annotations))
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(annotations)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(annotations))
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
}
Expand Down Expand Up @@ -687,7 +680,7 @@ func TestWriteWithUnixPathBadPath(t *testing.T) {
t.Fatalf(err.Error())
}

if sortLines(string(data)) != sortLines(formatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels))
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
}
}

0 comments on commit 3c865e4

Please sign in to comment.