-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.go
178 lines (157 loc) · 4.56 KB
/
template.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package template
import (
"bytes"
"context"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
templ "text/template"
opsv1alpha1 "github.com/nokia/gitops-conductor/pkg/apis/ops/v1alpha1"
yaml "gopkg.in/yaml.v2"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)
type templater struct {
baseDir string
}
var log = logf.Log.WithName("controller_template")
//getTemplatingSource fetches template source files and adds them to a data set to be used when running
//the template function
func getTemplatingSource(spec *opsv1alpha1.GitOps) (map[string]interface{}, error) {
if spec.Spec.Templating != nil {
sourceDir := spec.Status.RootFolder + "/" + spec.Spec.RootFolder + "/" + spec.Spec.Templating.SourceFolder
if spec.Spec.Templating.SourceFolder != "" {
sourceDir += "/"
}
t := spec.Spec.Templating
if t.Enabled {
if t.Source != nil {
data := make(map[string]interface{})
for _, f := range t.Source.TemplateDataFile {
temp := make(map[string]interface{})
fData, err := ioutil.ReadFile(sourceDir + f)
if err != nil {
log.Error(err, "Failed to read file")
return nil, err
}
yaml.Unmarshal(fData, temp)
for k, v := range temp {
data[k] = v
}
}
return data, nil
}
}
return nil, nil
}
return nil, nil
}
//RunGoTemplate executes template
func RunGoTemplate(spec *opsv1alpha1.GitOps) error {
log.Info("Running go templates")
outDir := spec.Status.RootFolder + "/_output"
workDir := GetGitRootDir(spec)
t := templater{baseDir: workDir}
d, err := getTemplatingSource(spec)
if err != nil {
return err
}
err = os.Mkdir(outDir, 0755)
if err != nil && !os.IsExist(err) {
log.Error(err, "Failed to create output dir")
return err
}
files, err := ioutil.ReadDir(workDir)
if err != nil {
log.Error(err, "Failed to read dir")
}
for _, file := range files {
if file.IsDir() {
if file.Name() == spec.Spec.Templating.SourceFolder {
continue
}
t.templateDir(workDir+"/"+file.Name(), outDir+"/"+file.Name(), d)
} else {
t.templateFile(workDir, outDir, file, d)
}
}
return nil
}
//templateFile takes one yaml file as input and runs Go template engine on the file. Output is written to output directory
func (t *templater) templateFile(workDir string, outDir string, file os.FileInfo, d map[string]interface{}) {
if strings.Contains(file.Name(), "yaml") {
filePath := workDir + "/" + file.Name()
tEx := templ.New(file.Name())
tEx.Funcs(templateFuncs(workDir))
tEx.ParseFiles(filePath)
b := bytes.NewBuffer([]byte{})
err := tEx.Execute(b, d)
if err != nil {
log.Error(err, "Failed to execute template")
}
newF, err := os.Create(outDir + "/" + file.Name())
if err != nil {
log.Error(err, "Failed to create file", "file", file.Name())
return
}
newF.Write(b.Bytes())
newF.Close()
}
}
//templateDir runs through a directory recursively templating every file on the way down in the tree
func (t *templater) templateDir(workDir string, outDir string, d map[string]interface{}) {
log.Info("templating", "dir", workDir)
err := os.Mkdir(outDir, 0755)
if err != nil && !os.IsExist(err) {
log.Error(err, "Failed to create output dir")
return
}
files, err := ioutil.ReadDir(workDir)
if err != nil {
log.Error(err, "Failed to read dir")
}
for _, file := range files {
if file.IsDir() {
t.templateDir(workDir+"/"+file.Name(), outDir+"/"+file.Name(), d)
} else {
t.templateFile(workDir, outDir, file, d)
}
}
}
//RunPreExecutor runs an arbitary pre-executor to template the yaml files
func RunPreExecutor(spec *opsv1alpha1.GitOps, dir string) error {
if spec.Spec.Templating != nil {
t := spec.Spec.Templating
if t.Executor != nil {
// Create a new context and add a timeout to it
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel() // The cancel should be deferred so resources are cleaned up
cmd := exec.CommandContext(ctx, t.Executor.Exec)
cmd.Dir = GetGitRootDir(spec)
if spec.Spec.Templating.SourceFolder != "" {
cmd.Dir += "/" + spec.Spec.Templating.SourceFolder
}
if len(t.Executor.Args) >= 1 {
a := []string{t.Executor.Exec}
for _, add := range t.Executor.Args {
a = append(a, add)
}
cmd.Args = a
}
out, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
log.Error(err, "Command timed out")
return err
}
if err != nil {
log.Error(err, "Command failed", "output", string(out))
}
return err
}
}
return nil
}
func GetGitRootDir(spec *opsv1alpha1.GitOps) string {
return spec.Status.RootFolder + "/" + spec.Spec.RootFolder
}