generated from crossplane/function-template-go
-
Notifications
You must be signed in to change notification settings - Fork 37
/
function_maps.go
132 lines (107 loc) · 3.39 KB
/
function_maps.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
package main
import (
"fmt"
"math/rand"
"strings"
"text/template"
"time"
sprig "github.com/Masterminds/sprig/v3"
"github.com/crossplane-contrib/function-go-templating/input/v1beta1"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/crossplane/function-sdk-go/errors"
"gopkg.in/yaml.v3"
)
const recursionMaxNums = 1000
var funcMaps = []template.FuncMap{
{
"randomChoice": randomChoice,
"toYaml": toYaml,
"fromYaml": fromYaml,
"getResourceCondition": getResourceCondition,
"setResourceNameAnnotation": setResourceNameAnnotation,
"getComposedResource": getComposedResource,
"getCompositeResource": getCompositeResource,
},
}
func GetNewTemplateWithFunctionMaps(delims *v1beta1.Delims) *template.Template {
tpl := template.New("manifests")
if delims != nil {
if delims.Left != nil && delims.Right != nil {
tpl = tpl.Delims(*delims.Left, *delims.Right)
}
}
for _, f := range funcMaps {
tpl.Funcs(f)
}
tpl.Funcs(template.FuncMap{
"include": initInclude(tpl),
})
// Sprig's env and expandenv can lead to information leakage (injected tokens/passwords).
// Both Helm and ArgoCD remove these due to security implications.
// see: https://masterminds.github.io/sprig/os.html
sprigFuncs := sprig.FuncMap()
delete(sprigFuncs, "env")
delete(sprigFuncs, "expandenv")
tpl.Funcs(sprigFuncs)
return tpl
}
func randomChoice(choices ...string) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return choices[r.Intn(len(choices))]
}
func toYaml(val any) (string, error) {
res, err := yaml.Marshal(val)
if err != nil {
return "", err
}
return string(res), nil
}
func fromYaml(val string) (any, error) {
var res any
err := yaml.Unmarshal([]byte(val), &res)
return res, err
}
func getResourceCondition(ct string, res map[string]any) xpv1.Condition {
var conditioned xpv1.ConditionedStatus
if err := fieldpath.Pave(res).GetValueInto("resource.status", &conditioned); err != nil {
conditioned = xpv1.ConditionedStatus{}
}
// Return either found condition or empty one with "Unknown" status
return conditioned.GetCondition(xpv1.ConditionType(ct))
}
func setResourceNameAnnotation(name string) string {
return fmt.Sprintf("gotemplating.fn.crossplane.io/composition-resource-name: %s", name)
}
func initInclude(t *template.Template) func(string, interface{}) (string, error) {
includedNames := make(map[string]int)
return func(name string, data interface{}) (string, error) {
var buf strings.Builder
if v, ok := includedNames[name]; ok {
if v > recursionMaxNums {
return "", errors.Wrapf(fmt.Errorf("unable to execute template"), "rendering template has a nested reference name: %s", name)
}
includedNames[name]++
} else {
includedNames[name] = 1
}
err := t.ExecuteTemplate(&buf, name, data)
includedNames[name]--
return buf.String(), err
}
}
func getComposedResource(req map[string]any, name string) map[string]any {
var cr map[string]any
path := fmt.Sprintf("observed.resources[%s]resource", name)
if err := fieldpath.Pave(req).GetValueInto(path, &cr); err != nil {
return nil
}
return cr
}
func getCompositeResource(req map[string]any) map[string]any {
var cr map[string]any
if err := fieldpath.Pave(req).GetValueInto("observed.composite.resource", &cr); err != nil {
return nil
}
return cr
}