Skip to content

Commit

Permalink
fix(kubectl) support dots in variables name (#636)
Browse files Browse the repository at this point in the history
* fix(kubectl) support dots in variables name

* fix(kubectl) update CHANGELOG.md
  • Loading branch information
lobkovilya authored Mar 18, 2020
1 parent 81b2616 commit 97cd767
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG

## master
* fix: `kumactl apply -v ...` support dots in variables name
[#636](https://github.com/Kong/kuma/pull/636)
* feat: read only cached manager
[#634](https://github.com/Kong/kuma/pull/634)
* fix: explicitly set parameters in securityContext of kuma-init
Expand Down
27 changes: 26 additions & 1 deletion app/kumactl/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
23 changes: 23 additions & 0 deletions app/kumactl/cmd/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: sample
mesh: default
type: Dataplane
networking:
address: "{{ var.with.dots.in.the.name }}"

0 comments on commit 97cd767

Please sign in to comment.