/
main.go
86 lines (72 loc) · 2.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright Contributors to the Open Cluster Management project
//Package main: This program shows how to create resources based on yamls template located in
//the same directory or bindata or array of string.
package main
import (
"flag"
"os"
// "github.com/stolostron/library-go/examples/applier/bindata"
"github.com/stolostron/library-go/pkg/templateprocessor"
"k8s.io/klog"
)
func usage() {
klog.Info("Usage: render-yaml-in-dir\n")
flag.PrintDefaults()
}
func showUsageAndExit(exitcode int) {
usage()
os.Exit(exitcode)
}
func main() {
klog.InitFlags(nil)
var showHelp = flag.Bool("h", false, "Show help message")
flag.Usage = usage
flag.Parse()
if *showHelp {
showUsageAndExit(0)
}
err := renderYamlFile()
if err != nil {
klog.Fatal(err)
}
}
func renderYamlFile() error {
const directory = "../resources"
//Create a reader on "../resources" directory
klog.Infof("Creating the file reader %s", directory)
yamlReader := templateprocessor.NewYamlFileReader(directory)
//Other readers can be used
//yamlReader := bindata.NewBindataReader()
//yamlReader := templateprocessor.NewYamlStringReader(yamls,"---")
//Create a templateProcessor with that reader
klog.Infof("Creating TemplateProcessor...")
tp, err := templateprocessor.NewTemplateProcessor(yamlReader, &templateprocessor.Options{})
if err != nil {
return err
}
//Defines the values
values := struct {
ManagedClusterName string
ManagedClusterNamespace string
BootstrapServiceAccountName string
}{
ManagedClusterName: "mycluster",
ManagedClusterNamespace: "mycluster",
BootstrapServiceAccountName: "mybootstrapserviceaccount",
}
//Render the resources starting with path "yamlfilereader" in the reader
//excluding "clusterrolebinding.yaml"
//in a non-recursive way
//and passing the values to replace
//The output is NOT sorted, the order is defined by the templateNames parameter
klog.Info("Render resources\n")
out, err := tp.TemplateResources(
[]string{"yamlfilereader/serviceaccount.yaml",
"yamlfilereader/namespace.yaml"},
values)
if err != nil {
return err
}
klog.Infof("Generated resources yamls\n%s", templateprocessor.ConvertArrayOfBytesToString(out))
return nil
}