From 4d59146e48305d2de677ffb7100a5dc621953d39 Mon Sep 17 00:00:00 2001 From: natasha41575 Date: Thu, 14 Oct 2021 18:13:49 -0700 Subject: [PATCH 1/2] test for dropped quote in configmap generation --- api/krusty/configmaps_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/api/krusty/configmaps_test.go b/api/krusty/configmaps_test.go index 024d8ffb84..f2333b889a 100644 --- a/api/krusty/configmaps_test.go +++ b/api/krusty/configmaps_test.go @@ -549,3 +549,27 @@ metadata: name: testing-tt4769fb52 `) } + +func TestDataEndsWithQuotes(t *testing.T) { + th := kusttest_test.MakeHarness(t) + th.WriteK(".", ` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +configMapGenerator: + - name: test + literals: + - TEST=this is a 'test' +`) + + m := th.Run(".", th.MakeDefaultOptions()) + // The annotations are sorted by key, hence the order change. + // Quotes are added intentionally. + th.AssertActualEqualsExpected( + m, `apiVersion: v1 +data: + TEST: this is a 'test +kind: ConfigMap +metadata: + name: test-k7hhfb697g +`) +} From ba051c863bdc2cecd60160addf3aefb097fc528a Mon Sep 17 00:00:00 2001 From: natasha41575 Date: Thu, 14 Oct 2021 18:30:28 -0700 Subject: [PATCH 2/2] fix issue with quote being dropped in configmap generation --- api/krusty/configmaps_test.go | 7 +++---- api/kv/kv.go | 14 +++++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/api/krusty/configmaps_test.go b/api/krusty/configmaps_test.go index f2333b889a..7219799831 100644 --- a/api/krusty/configmaps_test.go +++ b/api/krusty/configmaps_test.go @@ -550,6 +550,7 @@ metadata: `) } +// regression test for https://github.com/kubernetes-sigs/kustomize/issues/4233 func TestDataEndsWithQuotes(t *testing.T) { th := kusttest_test.MakeHarness(t) th.WriteK(".", ` @@ -562,14 +563,12 @@ configMapGenerator: `) m := th.Run(".", th.MakeDefaultOptions()) - // The annotations are sorted by key, hence the order change. - // Quotes are added intentionally. th.AssertActualEqualsExpected( m, `apiVersion: v1 data: - TEST: this is a 'test + TEST: this is a 'test' kind: ConfigMap metadata: - name: test-k7hhfb697g + name: test-k9cc55dfm5 `) } diff --git a/api/kv/kv.go b/api/kv/kv.go index b887e583c8..719b761c94 100644 --- a/api/kv/kv.go +++ b/api/kv/kv.go @@ -209,5 +209,17 @@ func parseLiteralSource(source string) (keyName, value string, err error) { if len(items) != 2 { return "", "", fmt.Errorf("invalid literal source %v, expected key=value", source) } - return items[0], strings.Trim(items[1], "\"'"), nil + return items[0], removeQuotes(items[1]), nil +} + +// removeQuotes removes the surrounding quotes from the provided string only if it is surrounded on both sides +// rather than blindly trimming all quotation marks on either side. +func removeQuotes(str string) string { + if len(str) == 0 || str[0] != str[len(str)-1] { + return str + } + if str[0] == '"' || str[0] == '\'' { + return str[1 : len(str)-1] + } + return str }