Skip to content

Commit

Permalink
Merge pull request kubernetes#5108 from usabilla/set-values
Browse files Browse the repository at this point in the history
Adds ability to set template context values on command line
  • Loading branch information
k8s-ci-robot authored Jul 19, 2018
2 parents a9de76a + 2f03684 commit 9d7b2d1
Show file tree
Hide file tree
Showing 18 changed files with 672 additions and 10 deletions.
6 changes: 6 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/kops/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ go_library(
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
"//vendor/k8s.io/client-go/tools/clientcmd:go_default_library",
"//vendor/k8s.io/client-go/util/homedir:go_default_library",
"//vendor/k8s.io/helm/pkg/strvals:go_default_library",
"//vendor/k8s.io/kubernetes/pkg/kubectl/cmd/templates:go_default_library",
"//vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util:go_default_library",
"//vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor:go_default_library",
Expand Down
24 changes: 22 additions & 2 deletions cmd/kops/toolbox_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/ghodss/yaml"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/strvals"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"

Expand All @@ -45,6 +46,7 @@ var (
kops toolbox template \
--values values.yaml --values=another.yaml \
--set var=value --set-string othervar=true \
--snippets file_or_directory --snippets=another.dir \
--template file_or_directory --template=directory \
--output cluster.yaml
Expand All @@ -63,6 +65,8 @@ type toolboxTemplateOption struct {
outputPath string
snippetsPath []string
templatePath []string
values []string
stringValues []string
}

// NewCmdToolboxTemplate returns a new templating command
Expand All @@ -87,6 +91,8 @@ func NewCmdToolboxTemplate(f *util.Factory, out io.Writer) *cobra.Command {
}

cmd.Flags().StringSliceVar(&options.configPath, "values", options.configPath, "Path to a configuration file containing values to include in template")
cmd.Flags().StringArrayVar(&options.values, "set", options.values, "Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
cmd.Flags().StringArrayVar(&options.stringValues, "set-string", options.stringValues, "Set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
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 @@ -100,7 +106,7 @@ func NewCmdToolboxTemplate(f *util.Factory, out io.Writer) *cobra.Command {
// runToolBoxTemplate is the action for the command
func runToolBoxTemplate(f *util.Factory, out io.Writer, options *toolboxTemplateOption) error {
// @step: read in the configuration if any
context, err := newTemplateContext(options.configPath)
context, err := newTemplateContext(options.configPath, options.values, options.stringValues)
if err != nil {
return err
}
Expand Down Expand Up @@ -204,7 +210,7 @@ func runToolBoxTemplate(f *util.Factory, out io.Writer, options *toolboxTemplate
}

// newTemplateContext is responsible for loadding the --values and build a context for the template
func newTemplateContext(files []string) (map[string]interface{}, error) {
func newTemplateContext(files []string, values []string, stringValues []string) (map[string]interface{}, error) {
context := make(map[string]interface{}, 0)

for _, x := range files {
Expand All @@ -227,6 +233,20 @@ func newTemplateContext(files []string) (map[string]interface{}, error) {
}
}

// User specified a value via --set
for _, value := range values {
if err := strvals.ParseInto(value, context); err != nil {
return nil, fmt.Errorf("failed parsing --set data: %s", err)
}
}

// User specified a value via --set-string
for _, value := range stringValues {
if err := strvals.ParseIntoString(value, context); err != nil {
return nil, fmt.Errorf("failed parsing --set-string data: %s", err)
}
}

return context, nil
}

Expand Down
19 changes: 11 additions & 8 deletions docs/cli/kops_toolbox_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ kops toolbox template [flags]
kops toolbox template \
--values values.yaml --values=another.yaml \
--set var=value --set-string othervar=true \
--snippets file_or_directory --snippets=another.dir \
--template file_or_directory --template=directory \
--output cluster.yaml
Expand All @@ -28,14 +29,16 @@ kops toolbox template [flags]
### Options

```
--config-value string Show the value of a specific configuration value
--fail-on-missing Fail on referencing unset variables in templates (default true)
--format-yaml Attempt to format the generated yaml content before output
-h, --help help for template
--output string Path to output file, otherwise defaults to stdout
--snippets strings Path to directory containing snippets used for templating
--template strings Path to template file or directory of templates to render
--values strings Path to a configuration file containing values to include in template
--config-value string Show the value of a specific configuration value
--fail-on-missing Fail on referencing unset variables in templates (default true)
--format-yaml Attempt to format the generated yaml content before output
-h, --help help for template
--output string Path to output file, otherwise defaults to stdout
--set stringArray Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--set-string stringArray Set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--snippets strings Path to directory containing snippets used for templating
--template strings Path to template file or directory of templates to render
--values strings Path to a configuration file containing values to include in template
```

### Options inherited from parent commands
Expand Down
36 changes: 36 additions & 0 deletions docs/cluster_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,42 @@ dnsZone: k8s.example.com
awsRegion: eu-west-1
```

When multiple environment files are passed using `--values` Kops performs a deep merge, for example given the following two files:
```yaml
# File values-a.yaml
instanceGroups:
foo:
ami: ami-1234567
type: m4.large
# File values-b.yaml
instanceGroups:
foo:
type: t2.large
```

Would result in the `instanceGroups.foo` object having two properties: `{"ami": "ami-1234567", "type": "t2.large"}`.

Besides specifying values through an environment file it is also possible to pass variables directly on the command line using the `--set` and `--set-string` command line options. The difference between the two options is that `--set-string` will always yield a string value while `--set` will cause the value to be parsed as a YAML value, for example the value `true` would turn into a boolean with `--set` while with `--set-string` it will be the literal string `"true"`. The format for specifying a variable is as follows:

```shell
kops toolbox template --template mytemplate.tpl --set 'version=1.0,foo.bar=baz' --set-string 'foo.myArray={1,2,3}' --set 'foo.myArray[1]=false,foo.myArray[3]=4'
```

which would yield the same values as using the `--values` option with the following file

```yaml
version: 1.0
foo:
bar: baz
myArray:
- "1"
- false
- "3"
- 4
```


Running `kops toolbox template` replaces the placeholders in the template by values and generates the file output.yaml, which can then be used to replace the desired cluster configuration with `kops replace -f cluster.yaml`.

Note: when creating a cluster desired configuration template, you can
Expand Down
202 changes: 202 additions & 0 deletions vendor/k8s.io/helm/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/k8s.io/helm/pkg/chartutil/testdata/frobnitz/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9d7b2d1

Please sign in to comment.