Skip to content

Commit

Permalink
Kops Template YAML Formatting
Browse files Browse the repository at this point in the history
Adding an extra option to the toolbox templating to format the YAML before writing out; which is usefull to cleanup formating issues and as detecting errors in the template

- added a formating options --format-yaml to the toolbox template
- updated the cli documentation
  • Loading branch information
gambol99 committed Oct 26, 2017
1 parent f3454f9 commit 347a349
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
48 changes: 35 additions & 13 deletions cmd/kops/toolbox_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ limitations under the License.
package main

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/util/templater"
"k8s.io/kops/upup/pkg/fi/utils"

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

"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/util/templater"
"k8s.io/kops/upup/pkg/fi/utils"
)

var (
Expand All @@ -55,6 +57,7 @@ type toolboxTemplateOption struct {
clusterName string
configPath []string
failOnMissing bool
formatYAML bool
outputPath string
snippetsPath []string
templatePath []string
Expand Down Expand Up @@ -86,6 +89,7 @@ func NewCmdToolboxTemplate(f *util.Factory, out io.Writer) *cobra.Command {
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")
cmd.Flags().BoolVar(&options.failOnMissing, "fail-on-missing", true, "Fail on referencing unset variables in templates")
cmd.Flags().BoolVar(&options.formatYAML, "format-yaml", false, "Attempt to format the generated yaml content before output")

return cmd
}
Expand Down Expand Up @@ -144,14 +148,7 @@ func runToolBoxTemplate(f *util.Factory, out io.Writer, options *toolboxTemplate
}

// @step: get the output io.Writer
writer := out
if options.outputPath != "" {
w, err := os.OpenFile(utils.ExpandPath(options.outputPath), os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0660)
if err != nil {
return fmt.Errorf("unable to open file: %s, error: %v", options.outputPath, err)
}
writer = w
}
writer := new(bytes.Buffer)

// @step: render each of the template and write to location
r := templater.NewTemplater()
Expand All @@ -166,14 +163,39 @@ func runToolBoxTemplate(f *util.Factory, out io.Writer, options *toolboxTemplate
if err != nil {
return fmt.Errorf("unable to render template: %s, error: %s", x, err)
}

if len(rendered) <= 0 {
continue
}
io.WriteString(writer, rendered)

// @check if we should need to add document separator
if i < size {
io.WriteString(writer, "---\n")
}
}
content := writer.Bytes()

if options.formatYAML {
var data interface{}
err := yaml.Unmarshal(content, &data)
if err != nil {
return fmt.Errorf("encountered errors generating templates: %s", err)
}
if content, err = yaml.Marshal(data); err != nil {
return fmt.Errorf("encountered error formating the template: %s", err)
}
}

if options.outputPath != "" {
w, err := os.OpenFile(utils.ExpandPath(options.outputPath), os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0660)
if err != nil {
return fmt.Errorf("unable to open file: %s, error: %v", options.outputPath, err)
}
defer w.Close()
w.Write(content)
} else {
out.Write(content)
}

return nil
}
Expand Down
1 change: 1 addition & 0 deletions docs/cli/kops_toolbox_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ kops toolbox template

```
--fail-on-missing Fail on referencing unset variables in templates (default true)
--format-yaml Attempt to format the generated yaml content before output
--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
Expand Down

0 comments on commit 347a349

Please sign in to comment.