-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathfunction_maps.go
More file actions
106 lines (84 loc) · 2.54 KB
/
function_maps.go
File metadata and controls
106 lines (84 loc) · 2.54 KB
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
package fn
import (
"fmt"
"math/rand"
"strings"
"text/template"
"time"
"gopkg.in/yaml.v3"
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"
)
const recursionMaxNums = 1000
var funcMaps = []template.FuncMap{
{
"randomChoice": randomChoice,
"toYaml": toYaml,
"fromYaml": fromYaml,
"getResourceCondition": getResourceCondition,
"setResourceNameAnnotation": setResourceNameAnnotation,
},
}
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),
})
tpl.Funcs(sprig.FuncMap())
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
}
}