-
Notifications
You must be signed in to change notification settings - Fork 131
/
server_generate_template.go
73 lines (60 loc) · 1.89 KB
/
server_generate_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
package runner
import (
"context"
"encoding/json"
"github.com/Masterminds/sprig/v3"
"io/ioutil"
"os"
"path/filepath"
"sigs.k8s.io/controller-runtime"
"text/template"
)
func (r *TerraformRunnerServer) GenerateTemplate(ctx context.Context, req *GenerateTemplateRequest) (*GenerateTemplateReply, error) {
log := controllerruntime.LoggerFrom(ctx).WithName(loggerName)
log.Info("generating the template founds")
workDir := req.WorkingDir
// find main.tf.tpl file
mainTfTplPath := filepath.Join(workDir, "main.tf.tpl")
if _, err := os.Stat(mainTfTplPath); os.IsNotExist(err) {
log.Info("main.tf.tpl not found, skipping")
return &GenerateTemplateReply{Message: "ok"}, nil
}
// marshal the vars
vars := make(map[string]interface{})
varFilePath := filepath.Join(req.WorkingDir, "generated.auto.tfvars.json")
jsonBytes, err := ioutil.ReadFile(varFilePath)
if err != nil {
log.Error(err, "unable to read the file", "filePath", varFilePath)
return nil, err
}
if err := json.Unmarshal(jsonBytes, &vars); err != nil {
log.Error(err, "unable to unmarshal the data")
return nil, err
}
// render the template
// we use Helm-like syntax for the template
tmpl, parseErr := template.New("main.tf.tpl").
Delims("{{", "}}").
Funcs(sprig.TxtFuncMap()).
ParseFiles(mainTfTplPath)
if parseErr != nil {
log.Error(parseErr, "unable to parse the template", "filePath", mainTfTplPath)
return nil, parseErr
}
mainTfPath := filepath.Join(workDir, "main.tf")
f, fileErr := os.Create(mainTfPath)
if fileErr != nil {
log.Error(fileErr, "unable to create the file", "filePath", mainTfPath)
return nil, fileErr
}
// make it Helm compatible
vars["Values"] = vars["values"]
if err := tmpl.Execute(f, vars); err != nil {
log.Error(err, "unable to execute the template")
return nil, err
}
if err := f.Close(); err != nil {
return nil, err
}
return &GenerateTemplateReply{Message: "ok"}, nil
}