-
Notifications
You must be signed in to change notification settings - Fork 23
/
templater.go
77 lines (68 loc) · 2.77 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
package populate
import (
"bytes"
"text/template"
"github.com/Masterminds/sprig"
"github.com/jenkins-x/jx-api/pkg/config"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// EvaluateTemplate evaluates the go template to create the value
func (o *Options) EvaluateTemplate(secretName, property, templateText string) (string, error) {
funcMap := sprig.TxtFuncMap()
// template function to lookup a value in a secret:
//
// use like this: `{{ secret "my-secret-name" "key-name" }}
funcMap["secret"] = func(lookupSecret, lookupKey string) string {
secret, err := o.KubeClient.CoreV1().Secrets(o.Namespace).Get(lookupSecret, metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
log.Logger().Warnf("failed to find secret %s in namespace %s so cannot resolve secret %s property %s from template", lookupSecret, o.Namespace, secretName, property)
return ""
}
answer := ""
if secret != nil && secret.Data != nil {
return string(secret.Data[lookupKey])
}
return answer
}
// template function to lookup a user + password in a secret and concatenate in a string like `"username:password"`.
//
// use like this: `{{ auth "my-secret-name" "username-key" "password-key }}
funcMap["auth"] = func(lookupSecret, userKey, passwordKey string) string {
secret, err := o.KubeClient.CoreV1().Secrets(o.Namespace).Get(lookupSecret, metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
log.Logger().Warnf("failed to find secret %s in namespace %s so cannot resolve secret %s property %s from template", lookupSecret, o.Namespace, secretName, property)
return ""
}
answer := ""
if secret != nil && secret.Data != nil {
return string(secret.Data[userKey]) + ":" + string(secret.Data[passwordKey])
}
return answer
}
tmpl, err := template.New("value.gotmpl").Option("missingkey=error").Funcs(funcMap).Parse(templateText)
if err != nil {
return "", errors.Wrapf(err, "failed to parse Secret %s property %s with template: %s", secretName, property, templateText)
}
if o.Requirements == nil {
o.Requirements, _, err = config.LoadRequirementsConfig(o.Dir, false)
if err != nil {
return "", errors.Wrapf(err, "failed to load jx-requirements.yml in dir %s", o.Dir)
}
}
requirementsMap, err := o.Requirements.ToMap()
if err != nil {
return "", errors.Wrapf(err, "failed turn requirements into a map: %v", o.Requirements)
}
templateData := map[string]interface{}{
"Requirements": requirementsMap,
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, templateData)
if err != nil {
return "", errors.Wrapf(err, "failed to evaluate template to create value of Secret %s property %s", secretName, property)
}
return buf.String(), nil
}