From 21d43e3187a0bd56df6eaedd76fce4788b10c3f6 Mon Sep 17 00:00:00 2001 From: Alexander Brandstedt Date: Mon, 24 Jul 2017 14:55:58 +0200 Subject: [PATCH 1/5] MPV implementation of templating to generate cluster.yaml file: related to #2404 rename command templating to template --- cmd/kops/toolbox.go | 1 + cmd/kops/toolbox_template.go | 193 ++++++++++++++++++++++++++++ docs/cli/kops_toolbox.md | 9 ++ docs/cli/kops_toolbox_template.md | 81 ++++++++++++ docs/cli/kops_toolbox_templating.md | 81 ++++++++++++ 5 files changed, 365 insertions(+) create mode 100644 cmd/kops/toolbox_template.go create mode 100644 docs/cli/kops_toolbox_template.md create mode 100644 docs/cli/kops_toolbox_templating.md diff --git a/cmd/kops/toolbox.go b/cmd/kops/toolbox.go index 32b57e5707..5ad1099a1e 100644 --- a/cmd/kops/toolbox.go +++ b/cmd/kops/toolbox.go @@ -47,6 +47,7 @@ func NewCmdToolbox(f *util.Factory, out io.Writer) *cobra.Command { cmd.AddCommand(NewCmdToolboxConvertImported(f, out)) cmd.AddCommand(NewCmdToolboxDump(f, out)) + cmd.AddCommand(NewCmdToolboxTemplate(f, out)) return cmd } diff --git a/cmd/kops/toolbox_template.go b/cmd/kops/toolbox_template.go new file mode 100644 index 0000000000..c5acd0f6a0 --- /dev/null +++ b/cmd/kops/toolbox_template.go @@ -0,0 +1,193 @@ +/* +Copyright 2016 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "bufio" + "bytes" + "fmt" + "io" + "io/ioutil" + "os" + "text/template" + + yaml "gopkg.in/yaml.v2" + + "github.com/spf13/cobra" + "k8s.io/kops/cmd/kops/util" + "k8s.io/kops/upup/pkg/fi/utils" + "k8s.io/kubernetes/pkg/kubectl/cmd/templates" + "k8s.io/kubernetes/pkg/util/i18n" +) + +var ( + toolbox_templating_long = templates.LongDesc(i18n.T(` + Generate cluster.yaml from values input yaml file and apply template. + `)) + + toolbox_templating_example = templates.Examples(i18n.T(` + # generate cluster.yaml from template and input values + cat > values.yaml < cluster.tmpl.yaml <>>>>>> f1547da61... MPV implementation of templating to generate cluster.yaml file: related to #2404 +======= + kops toolbox template \ +>>>>>>> c06171ecf... rename command templating to template + --values values.yaml \ + --template cluster.tmpl.yaml \ + --output cluster.yaml + `)) + + toolbox_templating_short = i18n.T(`Generate cluster.yaml from template`) +) + +type ToolboxTemplateOption struct { + ClusterName string + ValuesFile string + TemplateFile string + OutPutFile string +} + +func NewCmdToolboxTemplate(f *util.Factory, out io.Writer) *cobra.Command { + options := &ToolboxTemplateOption{} + + cmd := &cobra.Command{ +<<<<<<< HEAD +<<<<<<< HEAD + Use: "template", +======= + Use: "templating", +>>>>>>> f1547da61... MPV implementation of templating to generate cluster.yaml file: related to #2404 +======= + Use: "template", +>>>>>>> c06171ecf... rename command templating to template + Short: toolbox_templating_short, + Long: toolbox_templating_long, + Example: toolbox_templating_example, + Run: func(cmd *cobra.Command, args []string) { + err := rootCommand.ProcessArgs(args) + if err != nil { + exitWithError(err) + } + + options.ClusterName = rootCommand.ClusterName() + + err = RunToolBoxTemplate(f, os.Stdout, options) + if err != nil { + exitWithError(err) + } + }, + } + + cmd.Flags().StringVar(&options.ValuesFile, "values", options.ValuesFile, "Path to values yaml file, default: values.yaml") + cmd.Flags().StringVar(&options.TemplateFile, "template", options.TemplateFile, "Path to template file, default: cluster.tmpl.yaml") + cmd.Flags().StringVar(&options.OutPutFile, "output", options.OutPutFile, "Path to output file, default: cluster.yaml") + + return cmd +} + +func RunToolBoxTemplate(f *util.Factory, out io.Writer, options *ToolboxTemplateOption) error { + if options.ValuesFile == "" { + options.ValuesFile = "values.yaml" + } + if options.TemplateFile == "" { + options.TemplateFile = "cluster.tmpl.yaml" + } + if options.OutPutFile == "" { + options.OutPutFile = "cluster.yaml" + } + + options.ValuesFile = utils.ExpandPath(options.ValuesFile) + options.TemplateFile = utils.ExpandPath(options.TemplateFile) + options.OutPutFile = utils.ExpandPath(options.OutPutFile) + + err := ExecTemplate(options, out) + if err != nil { + exitWithError(err) + } + + return nil +} + +func ExecTemplate(options *ToolboxTemplateOption, out io.Writer) error { + valuesByteArr, err := ioutil.ReadFile(options.ValuesFile) + if err != nil { + return fmt.Errorf("failed to read values file: %v :%v", options.ValuesFile, err) + } + + tmpl, err := template.ParseFiles(options.TemplateFile) + if err != nil { + return fmt.Errorf("failed to read template file: %v :%v", options.TemplateFile, err) + } + + var values map[string]interface{} + err = yaml.Unmarshal(valuesByteArr, &values) + if err != nil { + return fmt.Errorf("failed to unmarshal valuesfile: %v :%v", options.ValuesFile, err) + } + + var buff bytes.Buffer + writer := bufio.NewWriter(&buff) + + err = tmpl.Execute(writer, values) + if err != nil { + return fmt.Errorf("failed to execute template: %v", err) + } + + err = writer.Flush() + if err != nil { + exitWithError(err) + } + + err = ioutil.WriteFile(options.OutPutFile, buff.Bytes(), 0644) + if err != nil { + exitWithError(err) + } + return nil +} diff --git a/docs/cli/kops_toolbox.md b/docs/cli/kops_toolbox.md index b35910ae5c..f03f6ca795 100644 --- a/docs/cli/kops_toolbox.md +++ b/docs/cli/kops_toolbox.md @@ -36,4 +36,13 @@ Misc infrequently used commands. * [kops](kops.md) - kops is Kubernetes ops. * [kops toolbox convert-imported](kops_toolbox_convert-imported.md) - Convert an imported cluster into a kops cluster. * [kops toolbox dump](kops_toolbox_dump.md) - Dump cluster information +<<<<<<< HEAD +<<<<<<< HEAD +* [kops toolbox template](kops_toolbox_template.md) - Generate cluster.yaml from template +======= +* [kops toolbox templating](kops_toolbox_templating.md) - Generate cluster.yaml from template +>>>>>>> f1547da61... MPV implementation of templating to generate cluster.yaml file: related to #2404 +======= +* [kops toolbox template](kops_toolbox_template.md) - Generate cluster.yaml from template +>>>>>>> c06171ecf... rename command templating to template diff --git a/docs/cli/kops_toolbox_template.md b/docs/cli/kops_toolbox_template.md new file mode 100644 index 0000000000..897a949ed2 --- /dev/null +++ b/docs/cli/kops_toolbox_template.md @@ -0,0 +1,81 @@ + + + +## kops toolbox template + +Generate cluster.yaml from template + +### Synopsis + + +Generate cluster.yaml from values input yaml file and apply template. + +``` +kops toolbox template +``` + +### Examples + +``` + # generate cluster.yaml from template and input values + cat > values.yaml < cluster.tmpl.yaml < + +## kops toolbox templating + +Generate cluster.yaml from template + +### Synopsis + + +Generate cluster.yaml from values input yaml file and apply template. + +``` +kops toolbox templating +``` + +### Examples + +``` + # generate cluster.yaml from template and input values + cat > values.yaml < cluster.tmpl.yaml < Date: Mon, 24 Jul 2017 16:25:24 +0200 Subject: [PATCH 2/5] correct squash --- cmd/kops/toolbox_template.go | 16 ---------------- docs/cli/kops_toolbox.md | 8 -------- 2 files changed, 24 deletions(-) diff --git a/cmd/kops/toolbox_template.go b/cmd/kops/toolbox_template.go index c5acd0f6a0..d09ff8445d 100644 --- a/cmd/kops/toolbox_template.go +++ b/cmd/kops/toolbox_template.go @@ -70,15 +70,7 @@ var ( EOF -<<<<<<< HEAD -<<<<<<< HEAD kops toolbox template \ -======= - kops toolbox templating \ ->>>>>>> f1547da61... MPV implementation of templating to generate cluster.yaml file: related to #2404 -======= - kops toolbox template \ ->>>>>>> c06171ecf... rename command templating to template --values values.yaml \ --template cluster.tmpl.yaml \ --output cluster.yaml @@ -98,15 +90,7 @@ func NewCmdToolboxTemplate(f *util.Factory, out io.Writer) *cobra.Command { options := &ToolboxTemplateOption{} cmd := &cobra.Command{ -<<<<<<< HEAD -<<<<<<< HEAD Use: "template", -======= - Use: "templating", ->>>>>>> f1547da61... MPV implementation of templating to generate cluster.yaml file: related to #2404 -======= - Use: "template", ->>>>>>> c06171ecf... rename command templating to template Short: toolbox_templating_short, Long: toolbox_templating_long, Example: toolbox_templating_example, diff --git a/docs/cli/kops_toolbox.md b/docs/cli/kops_toolbox.md index f03f6ca795..f92a15b054 100644 --- a/docs/cli/kops_toolbox.md +++ b/docs/cli/kops_toolbox.md @@ -36,13 +36,5 @@ Misc infrequently used commands. * [kops](kops.md) - kops is Kubernetes ops. * [kops toolbox convert-imported](kops_toolbox_convert-imported.md) - Convert an imported cluster into a kops cluster. * [kops toolbox dump](kops_toolbox_dump.md) - Dump cluster information -<<<<<<< HEAD -<<<<<<< HEAD * [kops toolbox template](kops_toolbox_template.md) - Generate cluster.yaml from template -======= -* [kops toolbox templating](kops_toolbox_templating.md) - Generate cluster.yaml from template ->>>>>>> f1547da61... MPV implementation of templating to generate cluster.yaml file: related to #2404 -======= -* [kops toolbox template](kops_toolbox_template.md) - Generate cluster.yaml from template ->>>>>>> c06171ecf... rename command templating to template From 89150391c1db2264a237776f62ebe987ea50d3af Mon Sep 17 00:00:00 2001 From: Alexander Brandstedt Date: Tue, 25 Jul 2017 10:21:14 +0200 Subject: [PATCH 3/5] remove inline example file with eof --- cmd/kops/toolbox_template.go | 28 ---------------------------- docs/cli/kops_toolbox_template.md | 28 ---------------------------- 2 files changed, 56 deletions(-) diff --git a/cmd/kops/toolbox_template.go b/cmd/kops/toolbox_template.go index d09ff8445d..5476e6cf6d 100644 --- a/cmd/kops/toolbox_template.go +++ b/cmd/kops/toolbox_template.go @@ -41,34 +41,6 @@ var ( toolbox_templating_example = templates.Examples(i18n.T(` # generate cluster.yaml from template and input values - cat > values.yaml < cluster.tmpl.yaml < values.yaml < cluster.tmpl.yaml < Date: Tue, 25 Jul 2017 10:36:29 +0200 Subject: [PATCH 4/5] remove old docs file --- docs/cli/kops_toolbox_templating.md | 81 ----------------------------- 1 file changed, 81 deletions(-) delete mode 100644 docs/cli/kops_toolbox_templating.md diff --git a/docs/cli/kops_toolbox_templating.md b/docs/cli/kops_toolbox_templating.md deleted file mode 100644 index e41c9fcf2f..0000000000 --- a/docs/cli/kops_toolbox_templating.md +++ /dev/null @@ -1,81 +0,0 @@ - - - -## kops toolbox templating - -Generate cluster.yaml from template - -### Synopsis - - -Generate cluster.yaml from values input yaml file and apply template. - -``` -kops toolbox templating -``` - -### Examples - -``` - # generate cluster.yaml from template and input values - cat > values.yaml < cluster.tmpl.yaml < Date: Wed, 26 Jul 2017 11:19:25 +0200 Subject: [PATCH 5/5] change template func to take out io.Writer from function not directly use os.Stdout --- cmd/kops/toolbox_template.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/kops/toolbox_template.go b/cmd/kops/toolbox_template.go index 5476e6cf6d..267e6ae993 100644 --- a/cmd/kops/toolbox_template.go +++ b/cmd/kops/toolbox_template.go @@ -22,7 +22,6 @@ import ( "fmt" "io" "io/ioutil" - "os" "text/template" yaml "gopkg.in/yaml.v2" @@ -74,7 +73,7 @@ func NewCmdToolboxTemplate(f *util.Factory, out io.Writer) *cobra.Command { options.ClusterName = rootCommand.ClusterName() - err = RunToolBoxTemplate(f, os.Stdout, options) + err = RunToolBoxTemplate(f, out, options) if err != nil { exitWithError(err) }