Skip to content

Commit

Permalink
support merge of values into same namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Sucharevich committed Aug 21, 2019
1 parent c72b273 commit e737d76
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.0
0.8.0
18 changes: 13 additions & 5 deletions cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var renderCmd = &cobra.Command{
Use: "render",
Run: func(cmd *cobra.Command, args []string) {
templateReaders := make(map[string]io.Reader)
valueReaders := make(map[string]io.Reader)
valueReaders := make(map[string][]io.Reader)
log := logger.New(nil)
if len(renderCmdOptions.templates) == 0 {
dieOnError(fmt.Errorf("No tempalte given"), log)
Expand All @@ -51,7 +51,11 @@ var renderCmd = &cobra.Command{
} else {
valueReader = file
}
valueReaders[renderCmdOptions.rootContext] = valueReader
if _, ok := valueReaders[renderCmdOptions.rootContext]; !ok {
valueReaders[renderCmdOptions.rootContext] = []io.Reader{valueReader}
} else {
valueReaders[renderCmdOptions.rootContext] = append(valueReaders[renderCmdOptions.rootContext], valueReader)
}
} else {
file, err = os.Open(values[1])
dieOnError(err, log)
Expand All @@ -61,7 +65,11 @@ var renderCmd = &cobra.Command{
} else {
valueReader = file
}
valueReaders[values[0]] = valueReader
if _, ok := valueReaders[values[0]]; !ok {
valueReaders[values[0]] = []io.Reader{valueReader}
} else {
valueReaders[values[0]] = append(valueReaders[values[0]], valueReader)
}
}
defer file.Close()
}
Expand All @@ -84,6 +92,6 @@ func init() {
renderCmd.Flags().StringArrayVar(&renderCmdOptions.templates, "template", []string{}, "Path to template file")
renderCmd.Flags().StringArrayVar(&renderCmdOptions.values, "value", []string{}, "Path to value file")
renderCmd.Flags().StringVar(&renderCmdOptions.rootContext, "root-namespace", "Values", "Name of the root namespace (default: Values)")
renderCmd.Flags().StringVar(&renderCmdOptions.leftDelim, "left-delim", "<<", "Left delimiter (default: <<)")
renderCmd.Flags().StringVar(&renderCmdOptions.rightDelim, "right-delim", ">>", "Right delimiter (default: >>)")
renderCmd.Flags().StringVar(&renderCmdOptions.leftDelim, "left-delim", "{{", "Left delimiter (default: <<)")
renderCmd.Flags().StringVar(&renderCmdOptions.rightDelim, "right-delim", "}}", "Right delimiter (default: >>)")
}
1 change: 1 addition & 0 deletions examples/multiple-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Run: `pikolo render --template examples/with-namespace/template --value Context=examples/with-namespace/values.yaml`
6 changes: 6 additions & 0 deletions examples/multiple-files/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
pikolo render \
--template examples/multiple-files/templates/t1 \
--template examples/multiple-files/templates/t2 \
--value examples/multiple-files/values/values.yaml \
--value examples/multiple-files/values/values_2.json
3 changes: 3 additions & 0 deletions examples/multiple-files/templates/t1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
steps:
step_1:
image: {{ .Values.image }}
3 changes: 3 additions & 0 deletions examples/multiple-files/templates/t2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
steps:
step_1:
image: {{ .Values.key }}
1 change: 1 addition & 0 deletions examples/multiple-files/values/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
image: alpine:3.8
3 changes: 3 additions & 0 deletions examples/multiple-files/values/values_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key": "value"
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/hairyhenderson/toml v0.3.0 // indirect
github.com/hashicorp/consul/api v1.1.0 // indirect
github.com/hashicorp/vault/api v1.0.4 // indirect
github.com/imdario/mergo v0.3.7
github.com/inconshreveable/log15 v0.0.0-20180818164646-67afb5ed74ec
github.com/joho/godotenv v1.3.0 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ github.com/hashicorp/vault/sdk v0.1.13 h1:mOEPeOhT7jl0J4AMl1E705+BcmeRs1VmKNb9F0
github.com/hashicorp/vault/sdk v0.1.13/go.mod h1:B+hVj7TpuQY1Y/GPbCpffmgd+tSEwvhkWnjtSYCaS2M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/log15 v0.0.0-20180818164646-67afb5ed74ec h1:CGkYB1Q7DSsH/ku+to+foV4agt2F2miquaLUgF6L178=
github.com/inconshreveable/log15 v0.0.0-20180818164646-67afb5ed74ec/go.mod h1:cOaXtrgN4ScfRrD9Bre7U1thNq5RtJ8ZoP4iXVGRj6o=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand Down
25 changes: 15 additions & 10 deletions pkg/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ghodss/yaml"
"github.com/hairyhenderson/gomplate"
"github.com/imdario/mergo"
)

type (
Expand All @@ -18,7 +19,7 @@ type (

Options struct {
TemplateReaders map[string]io.Reader
ValueReaders map[string]io.Reader
ValueReaders map[string][]io.Reader
RootNamespace string
LeftDelim string
RightDelim string
Expand All @@ -29,7 +30,7 @@ type (

renderer struct {
templateReaders map[string]io.Reader
valueReaders map[string]io.Reader
valueReaders map[string][]io.Reader
leftDelim string
rightDelim string
name string
Expand Down Expand Up @@ -59,15 +60,19 @@ func (r *renderer) Render() (*bytes.Buffer, error) {
content = append(content, string(res))
}

for name, reader := range r.valueReaders {
for name, readers := range r.valueReaders {
vals := Values{}
res, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
err = yaml.Unmarshal([]byte(string(res)), &vals)
if err != nil {
return nil, err
for _, reader := range readers {
v := Values{}
res, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
err = yaml.Unmarshal([]byte(string(res)), &v)
if err != nil {
return nil, err
}
mergo.Merge(&vals, v)
}
root[name] = vals

Expand Down
6 changes: 3 additions & 3 deletions pkg/renderer/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Test_renderer_Render(t *testing.T) {
tests := []struct {
name string
templateReaders map[string]io.Reader
valueReaders map[string]io.Reader
valueReaders map[string][]io.Reader
wantErr bool
result *bytes.Buffer
}{
Expand All @@ -27,8 +27,8 @@ func Test_renderer_Render(t *testing.T) {
templateReaders: map[string]io.Reader{
"key": strings.NewReader("{{ .Values.key }}\n"),
},
valueReaders: map[string]io.Reader{
"Values": strings.NewReader("key: value"),
valueReaders: map[string][]io.Reader{
"Values": []io.Reader{strings.NewReader("key: value")},
},
name: "Render",
wantErr: false,
Expand Down

0 comments on commit e737d76

Please sign in to comment.