Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kubectl) support dots in variables name #636

Merged
merged 3 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
* fix: explicitly set parameters in securityContext of kuma-init
[#631](https://github.com/Kong/kuma/pull/631)
* feature: log requests to external services
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 }}"