From 0420aba7bb6b51969fc7bd7833bc098ada2fc33f Mon Sep 17 00:00:00 2001 From: lobkovilya Date: Tue, 17 Mar 2020 17:21:55 +0700 Subject: [PATCH 1/2] fix(kubectl) support dots in variables name --- app/kumactl/cmd/apply/apply.go | 27 ++++++++++++++++++- app/kumactl/cmd/apply/apply_test.go | 23 ++++++++++++++++ .../apply-dataplane-template-dots.yaml | 5 ++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 app/kumactl/cmd/apply/testdata/apply-dataplane-template-dots.yaml diff --git a/app/kumactl/cmd/apply/apply.go b/app/kumactl/cmd/apply/apply.go index 4c22f3259173..594d396ffb3d 100644 --- a/app/kumactl/cmd/apply/apply.go +++ b/app/kumactl/cmd/apply/apply.go @@ -120,10 +120,35 @@ func NewApplyCmd(pctx *kumactl_cmd.RootContext) *cobra.Command { return cmd } +type contextMap map[string]interface{} + +func (cm contextMap) merge(other contextMap) { + for k, v := range other { + cm[k] = v + } +} + +func newContextMap(key, value string) contextMap { + if !strings.Contains(key, ".") { + return map[string]interface{}{ + key: value, + } + } + + parts := strings.SplitAfterN(key, ".", 2) + return map[string]interface{}{ + parts[0][:len(parts[0])-1]: newContextMap(parts[1], value), + } +} + func processConfigTemplate(config string, values map[string]string) ([]byte, error) { // TODO error checking -- match number of placeholders with number of // passed values - data := mustache.Render(config, values) + ctx := contextMap{} + for k, v := range values { + ctx.merge(newContextMap(k, v)) + } + data := mustache.Render(config, ctx) return []byte(data), nil } diff --git a/app/kumactl/cmd/apply/apply_test.go b/app/kumactl/cmd/apply/apply_test.go index 50b84a11adfe..4a065254bfa7 100644 --- a/app/kumactl/cmd/apply/apply_test.go +++ b/app/kumactl/cmd/apply/apply_test.go @@ -341,6 +341,29 @@ type: Dataplane `)) }) + It("should support variable names that include dot character", func() { + // given + rootCmd.SetArgs([]string{ + "apply", "-f", filepath.Join("testdata", "apply-dataplane-template-dots.yaml"), + "-v", "var.with.dots.in.the.name=2.2.2.2"}, + ) + + // when + err := rootCmd.Execute() + // then + Expect(err).ToNot(HaveOccurred()) + + // when + resource := mesh.DataplaneResource{} + err = store.Get(context.Background(), &resource, core_store.GetByKey("sample", "default")) + Expect(err).ToNot(HaveOccurred()) + + // then + Expect(resource.Meta.GetName()).To(Equal("sample")) + Expect(resource.Meta.GetMesh()).To(Equal("default")) + Expect(resource.Spec.Networking.Address).To(Equal("2.2.2.2")) + }) + type testCase struct { resource string err string diff --git a/app/kumactl/cmd/apply/testdata/apply-dataplane-template-dots.yaml b/app/kumactl/cmd/apply/testdata/apply-dataplane-template-dots.yaml new file mode 100644 index 000000000000..f4fbc8fad690 --- /dev/null +++ b/app/kumactl/cmd/apply/testdata/apply-dataplane-template-dots.yaml @@ -0,0 +1,5 @@ +name: sample +mesh: default +type: Dataplane +networking: + address: "{{ var.with.dots.in.the.name }}" From 5d86427d848540d2eeffe232ac83360b1abba5b4 Mon Sep 17 00:00:00 2001 From: lobkovilya Date: Tue, 17 Mar 2020 20:54:31 +0700 Subject: [PATCH 2/2] fix(kubectl) update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fd9841755bb..8d38bd934902 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # CHANGELOG ## master +* fix: `kumactl apply -v ...` support dots in variables name + [#636](https://github.com/Kong/kuma/pull/636) * fix: explicitly set parameters in securityContext of kuma-init [#631](https://github.com/Kong/kuma/pull/631) * feature: log requests to external services