Added the ability to set individual values for the toolbox template command

This commit is contained in:
Gijs Kunze 2018-05-05 10:51:02 +02:00
parent 617ce047e6
commit 381c30ea4a
2 changed files with 25 additions and 2 deletions

View File

@ -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"
@ -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
@ -63,6 +65,8 @@ type toolboxTemplateOption struct {
outputPath string
snippetsPath []string
templatePath []string
values []string
stringValues []string
}
// NewCmdToolboxTemplate returns a new templating command
@ -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().StringSliceVar(&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().StringSliceVar(&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")
@ -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
}
@ -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 {
@ -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
}

View File

@ -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
@ -33,6 +34,8 @@ kops toolbox template [flags]
--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 strings Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--set-string strings 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