MPV implementation of templating to generate cluster.yaml file: related to #2404

rename command templating to template
This commit is contained in:
Alexander Brandstedt 2017-07-24 14:55:58 +02:00
parent 83fb549a9d
commit 21d43e3187
5 changed files with 365 additions and 0 deletions

View File

@ -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
}

View File

@ -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 <<EOF
clusterName: eu1
kubernetesVersion: 1.7.1
dnsZone: k8s.example.com
awsRegion: eu-west-1
EOF
cat > cluster.tmpl.yaml <<EOF
apiVersion: kops/v1alpha2
kind: InstanceGroup
metadata:
labels:
kops.k8s.io/cluster: {{.clusterName}}.{{.dnsZone}}
name: nodes
spec:
image: coreos.com/CoreOS-stable-1409.6.0-hvm
machineType: m4.large
maxPrice: "0.5"
maxSize: 2
minSize: 15
role: Node
rootVolumeSize: 100
subnets:
- {{.awsRegion}}a
- {{.awsRegion}}b
- {{.awsRegion}}c
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
`))
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
}

View File

@ -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

View File

@ -0,0 +1,81 @@
<!--- This file is automatically generated by make gen-cli-docs; changes should be made in the go CLI command code (under cmd/kops) -->
## 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 <<EOF
clusterName: eu1
kubernetesVersion: 1.7.1
dnsZone: k8s.example.com
awsRegion: eu-west-1
EOF
cat > cluster.tmpl.yaml <<EOF
apiVersion: kops/v1alpha2
kind: InstanceGroup
metadata:
labels:
kops.k8s.io/cluster: {{.clusterName}}.{{.dnsZone}}
name: nodes
spec:
image: coreos.com/CoreOS-stable-1409.6.0-hvm
machineType: m4.large
maxPrice: "0.5"
maxSize: 2
minSize: 15
role: Node
rootVolumeSize: 100
subnets:
- {{.awsRegion}}a
- {{.awsRegion}}b
- {{.awsRegion}}c
EOF
kops toolbox template \
--values values.yaml \
--template cluster.tmpl.yaml \
--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
```
### Options inherited from parent commands
```
--alsologtostderr log to standard error as well as files
--config string config file (default is $HOME/.kops.yaml)
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log_dir string If non-empty, write log files in this directory
--logtostderr log to standard error instead of files (default false)
--name string Name of cluster
--state string Location of state storage
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
-v, --v Level log level for V logs
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
```
### SEE ALSO
* [kops toolbox](kops_toolbox.md) - Misc infrequently used commands.

View File

@ -0,0 +1,81 @@
<!--- This file is automatically generated by make gen-cli-docs; changes should be made in the go CLI command code (under cmd/kops) -->
## 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 <<EOF
clusterName: eu1
kubernetesVersion: 1.7.1
dnsZone: k8s.example.com
awsRegion: eu-west-1
EOF
cat > cluster.tmpl.yaml <<EOF
apiVersion: kops/v1alpha2
kind: InstanceGroup
metadata:
labels:
kops.k8s.io/cluster: {{.clusterName}}.{{.dnsZone}}
name: nodes
spec:
image: coreos.com/CoreOS-stable-1409.6.0-hvm
machineType: m4.large
maxPrice: "0.5"
maxSize: 2
minSize: 15
role: Node
rootVolumeSize: 100
subnets:
- {{.awsRegion}}a
- {{.awsRegion}}b
- {{.awsRegion}}c
EOF
kops toolbox templating \
--values values.yaml \
--template cluster.tmpl.yaml \
--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
```
### Options inherited from parent commands
```
--alsologtostderr log to standard error as well as files
--config string config file (default is $HOME/.kops.yaml)
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log_dir string If non-empty, write log files in this directory
--logtostderr log to standard error instead of files (default false)
--name string Name of cluster
--state string Location of state storage
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
-v, --v Level log level for V logs
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
```
### SEE ALSO
* [kops toolbox](kops_toolbox.md) - Misc infrequently used commands.