-
Notifications
You must be signed in to change notification settings - Fork 51
/
templater.go
102 lines (88 loc) · 2.91 KB
/
templater.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
package templater
import (
"bytes"
"io/ioutil"
"text/template"
"github.com/Masterminds/sprig"
jxcore "github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/yamls"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"helm.sh/helm/v3/pkg/chartutil"
)
// Templater a templater of values yaml
type Templater struct {
Requirements *jxcore.RequirementsConfig
ValuesFiles []string
}
// NewTemplater creates a new templater
func NewTemplater(requirements *jxcore.RequirementsConfig, valuesFiles []string) *Templater {
return &Templater{
Requirements: requirements,
ValuesFiles: valuesFiles,
}
}
func (o *Templater) createFuncMap() template.FuncMap {
funcMap := NewFunctionMap()
return funcMap
}
// Generate generates the destination file from the given source template
func (o *Templater) Generate(sourceFile, destFile string) error {
funcMap := o.createFuncMap()
data, err := o.renderTemplate(sourceFile, funcMap)
if err != nil {
return errors.Wrapf(err, "failed to render template file %s", sourceFile)
}
err = ioutil.WriteFile(destFile, data, files.DefaultFileWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to save output of template %s to %s", sourceFile, destFile)
}
return nil
}
// NewFunctionMap creates a new function map for values.tmpl.yaml templating
func NewFunctionMap() template.FuncMap {
funcMap := sprig.TxtFuncMap()
funcMap["basicAuth"] = BasicAuth
funcMap["hashPassword"] = HashPassword
funcMap["removeScheme"] = RemoveScheme
return funcMap
}
// RenderTemplate evaluates the given values.yaml file as a go template and returns the output data
func (o *Templater) renderTemplate(templateFile string, funcMap template.FuncMap) ([]byte, error) {
requirements := o.Requirements
tmpl, err := template.New("values.yaml.gotmpl").Option("missingkey=error").Funcs(funcMap).ParseFiles(templateFile)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse go template: %s", templateFile)
}
requirementsMap, err := requirements.ToMap()
if err != nil {
return nil, errors.Wrapf(err, "failed turn requirements into a map: %v", requirements)
}
valuesMap := map[string]interface{}{
"jxRequirements": requirementsMap,
}
for _, valuesFile := range o.ValuesFiles {
values := map[string]interface{}{}
err = yamls.LoadFile(valuesFile, &values)
if err != nil {
return nil, errors.Wrapf(err, "failed to load values file %s", valuesFile)
}
for k, v := range values {
valuesMap[k] = v
}
}
for k, v := range valuesMap {
log.Logger().Debugf("loaded value %s = %#v", k, v)
}
templateData := map[string]interface{}{
"Values": chartutil.Values(valuesMap),
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, templateData)
if err != nil {
return nil, errors.Wrapf(err, "failed to execute Secrets template: %s", templateFile)
}
data := buf.Bytes()
return data, nil
}