Skip to content

Commit

Permalink
- include the ability to specify multiple configuration files, useful…
Browse files Browse the repository at this point in the history
… for common.yaml and prod.yaml etc
  • Loading branch information
gambol99 committed Aug 26, 2017
1 parent 4c5215e commit e1ebdc6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
32 changes: 22 additions & 10 deletions cmd/kops/toolbox_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ var (
# generate cluster.yaml from template and input values
kops toolbox template \
--values values.yaml \
--snippets file_or_directory \
--template file_or_directory \
--values values.yaml --values=another.yaml \
--snippets file_or_directory --snippets=another.dir \
--template file_or_directory --template=directory \
--output cluster.yaml
`))

Expand All @@ -52,7 +52,7 @@ var (
// the options for the command
type toolboxTemplateOption struct {
clusterName string
configPath string
configPath []string
outputPath string
snippetsPath []string
templatePath []string
Expand All @@ -79,7 +79,7 @@ func NewCmdToolboxTemplate(f *util.Factory, out io.Writer) *cobra.Command {
},
}

cmd.Flags().StringVar(&options.configPath, "values", options.configPath, "Path to a configuration file containing values to include in template")
cmd.Flags().StringSliceVar(&options.configPath, "values", options.configPath, "Path to a configuration file containing values to include in template")
cmd.Flags().StringSliceVar(&options.templatePath, "template", options.templatePath, "Path to template file or directory of templates to render")
cmd.Flags().StringSliceVar(&options.snippetsPath, "snippets", options.snippetsPath, "Path to directory containing snippets used for templating")
cmd.Flags().StringVar(&options.outputPath, "output", options.outputPath, "Path to output file, otherwise defaults to stdout")
Expand All @@ -91,13 +91,25 @@ func NewCmdToolboxTemplate(f *util.Factory, out io.Writer) *cobra.Command {
func runToolBoxTemplate(f *util.Factory, out io.Writer, options *toolboxTemplateOption) error {
// @step: read in the configuration if any
context := make(map[string]interface{}, 0)
if options.configPath != "" {
content, err := ioutil.ReadFile(utils.ExpandPath(options.configPath))
for _, x := range options.configPath {
list, err := putil.ExpandFiles(utils.ExpandPath(x))
if err != nil {
return fmt.Errorf("unable to configuration file: %s, error: %s", options.configPath, err)
return err
}
if err := utils.YamlUnmarshal(content, &context); err != nil {
return fmt.Errorf("unable decode the configuration file: %s, error: %v", options.configPath, err)
for _, j := range list {
content, err := ioutil.ReadFile(j)
if err != nil {
return fmt.Errorf("unable to configuration file: %s, error: %s", j, err)
}

ctx := make(map[string]interface{}, 0)
if err := utils.YamlUnmarshal(content, &ctx); err != nil {
return fmt.Errorf("unable decode the configuration file: %s, error: %v", j, err)
}
// @step: merge the maps together
for k, v := range ctx {
context[k] = v
}
}
}
context["clusterName"] = options.clusterName
Expand Down
12 changes: 7 additions & 5 deletions docs/cli/kops_toolbox_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ kops toolbox template
# generate cluster.yaml from template and input values
kops toolbox template \
--values values.yaml \
--template cluster.tmpl.yaml \
--values values.yaml --values=another.yaml \
--snippets file_or_directory --snippets=another.dir \
--template file_or_directory --template=directory \
--output cluster.yaml
```

### Options

```
--output string Path to output file, default: cluster.yaml
--template string Path to template file, default: cluster.tmpl.yaml
--values string Path to values yaml file, default: values.yaml
--output string Path to output file, otherwise defaults to stdout
--snippets stringSlice Path to directory containing snippets used for templating
--template stringSlice Path to template file or directory of templates to render
--values stringSlice Path to a configuration file containing values to include in template
```

### Options inherited from parent commands
Expand Down
1 change: 1 addition & 0 deletions hack/.packages
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ k8s.io/kops/pkg/resources/digitalocean/dns
k8s.io/kops/pkg/systemd
k8s.io/kops/pkg/templates
k8s.io/kops/pkg/testutils
k8s.io/kops/pkg/util
k8s.io/kops/pkg/util/stringorslice
k8s.io/kops/pkg/validation
k8s.io/kops/protokube/cmd/protokube
Expand Down

0 comments on commit e1ebdc6

Please sign in to comment.