From fa8f504ff43551a1f0fe5d7c74b450553e652b15 Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Thu, 2 Jan 2020 10:28:03 -0800 Subject: [PATCH 1/7] Trim trailing spaces and tabs from config map files --- api/krusty/baseandoverlaymedium_test.go | 14 ++++++++------ api/kv/kv.go | 9 ++++++++- api/kv/kv_test.go | 10 ++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/api/krusty/baseandoverlaymedium_test.go b/api/krusty/baseandoverlaymedium_test.go index 19bd3d8da..c207a18d2 100644 --- a/api/krusty/baseandoverlaymedium_test.go +++ b/api/krusty/baseandoverlaymedium_test.go @@ -153,9 +153,9 @@ FRUIT=banana LEGUME=chickpea `) th.WriteF("/app/overlay/configmap/dummy.txt", - `Lorem ipsum dolor sit amet, consectetur -adipiscing elit, sed do eiusmod tempor -incididunt ut labore et dolore magna aliqua. + `Lorem ipsum dolor sit amet, consectetur +adipiscing elit, sed do eiusmod tempor +incididunt ut labore et dolore magna aliqua. `) th.WriteF("/app/overlay/deployment/deployment.yaml", ` apiVersion: apps/v1 @@ -292,8 +292,10 @@ metadata: --- apiVersion: v1 data: - nonsense: "Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod - tempor\nincididunt ut labore et dolore magna aliqua. \n" + nonsense: | + Lorem ipsum dolor sit amet, consectetur + adipiscing elit, sed do eiusmod tempor + incididunt ut labore et dolore magna aliqua. kind: ConfigMap metadata: annotations: @@ -302,6 +304,6 @@ metadata: app: mungebot org: kubernetes repo: test-infra - name: test-infra-app-config-f462h769f9 + name: test-infra-app-config-4mt28b5bg2 `) } diff --git a/api/kv/kv.go b/api/kv/kv.go index b887e583c..e424fc38b 100644 --- a/api/kv/kv.go +++ b/api/kv/kv.go @@ -9,6 +9,7 @@ import ( "fmt" "os" "path" + "regexp" "strings" "unicode" "unicode/utf8" @@ -85,11 +86,17 @@ func (kvl *loader) keyValuesFromFileSources(sources []string) ([]types.Pair, err if err != nil { return nil, err } - kvs = append(kvs, types.Pair{Key: k, Value: string(content)}) + kvs = append(kvs, types.Pair{Key: k, Value: kvl.trimTrailingSpacesInLines(string(content))}) } return kvs, nil } +// Takes string with multiple lines and trims the trailing white spaces from each line. +func (kvl *loader) trimTrailingSpacesInLines(str string) string { + re := regexp.MustCompile(`\s*\n`) + return re.ReplaceAllString(str, "\n") +} + func (kvl *loader) keyValuesFromEnvFiles(paths []string) ([]types.Pair, error) { var kvs []types.Pair for _, p := range paths { diff --git a/api/kv/kv_test.go b/api/kv/kv_test.go index bc362adce..d82c4e81c 100644 --- a/api/kv/kv_test.go +++ b/api/kv/kv_test.go @@ -95,3 +95,13 @@ func TestKeyValuesFromFileSources(t *testing.T) { } } } + +func TestTrimTrailingSpacesInLines(t *testing.T) { + kvl := makeKvLoader(filesys.MakeFsInMemory()) + input := "\"fooKey\": \"fooValue\" \t\n\t\"barKey\": \"barValue\"" + expected := "\"fooKey\": \"fooValue\"\n\t\"barKey\": \"barValue\"" + res := kvl.trimTrailingSpacesInLines(input) + if !reflect.DeepEqual(res, expected) { + t.Errorf("Trim trailing spaces in lines should succeed, got: %s exptected: %s", res, expected) + } +} From 011804e14da1a915aea97983917bac3a63243786 Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Thu, 2 Jan 2020 13:06:14 -0800 Subject: [PATCH 3/7] Make suggested changes --- api/kv/kv.go | 6 +++--- api/kv/kv_test.go | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/api/kv/kv.go b/api/kv/kv.go index e424fc38b..25a2e8476 100644 --- a/api/kv/kv.go +++ b/api/kv/kv.go @@ -86,13 +86,13 @@ func (kvl *loader) keyValuesFromFileSources(sources []string) ([]types.Pair, err if err != nil { return nil, err } - kvs = append(kvs, types.Pair{Key: k, Value: kvl.trimTrailingSpacesInLines(string(content))}) + kvs = append(kvs, types.Pair{Key: k, Value: trimTrailingSpacesInLines(string(content))}) } return kvs, nil } -// Takes string with multiple lines and trims the trailing white spaces from each line. -func (kvl *loader) trimTrailingSpacesInLines(str string) string { +// trimTrailingSpacesInLines takes string with multiple lines and trims the trailing white spaces and tabs from each line. +func trimTrailingSpacesInLines(str string) string { re := regexp.MustCompile(`\s*\n`) return re.ReplaceAllString(str, "\n") } diff --git a/api/kv/kv_test.go b/api/kv/kv_test.go index d82c4e81c..25b628197 100644 --- a/api/kv/kv_test.go +++ b/api/kv/kv_test.go @@ -97,10 +97,9 @@ func TestKeyValuesFromFileSources(t *testing.T) { } func TestTrimTrailingSpacesInLines(t *testing.T) { - kvl := makeKvLoader(filesys.MakeFsInMemory()) input := "\"fooKey\": \"fooValue\" \t\n\t\"barKey\": \"barValue\"" expected := "\"fooKey\": \"fooValue\"\n\t\"barKey\": \"barValue\"" - res := kvl.trimTrailingSpacesInLines(input) + res := trimTrailingSpacesInLines(input) if !reflect.DeepEqual(res, expected) { t.Errorf("Trim trailing spaces in lines should succeed, got: %s exptected: %s", res, expected) } From 2ab884c8797904a84a7a4e2da7cc9ec9fd841a7b Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Thu, 2 Jan 2020 15:06:54 -0800 Subject: [PATCH 5/7] Push commit to point to changes --- kustomize/go.mod | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kustomize/go.mod b/kustomize/go.mod index 3ff28c01a..8d634e062 100644 --- a/kustomize/go.mod +++ b/kustomize/go.mod @@ -17,3 +17,5 @@ exclude ( github.com/russross/blackfriday v2.0.0+incompatible sigs.k8s.io/kustomize/api v0.2.0 ) + +replace sigs.k8s.io/kustomize/api => ../api From 62964bfcb49b81cc42da75dc69ce3ac83446bc05 Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Tue, 7 Jan 2020 14:42:43 -0800 Subject: [PATCH 6/7] Remove replace from kustomize/go.mod --- kustomize/go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/kustomize/go.mod b/kustomize/go.mod index 8d634e062..3ff28c01a 100644 --- a/kustomize/go.mod +++ b/kustomize/go.mod @@ -17,5 +17,3 @@ exclude ( github.com/russross/blackfriday v2.0.0+incompatible sigs.k8s.io/kustomize/api v0.2.0 ) - -replace sigs.k8s.io/kustomize/api => ../api