generated from crossplane/function-template-go
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathfunction_maps.go
More file actions
192 lines (159 loc) · 5.26 KB
/
function_maps.go
File metadata and controls
192 lines (159 loc) · 5.26 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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/v2/apis/common/v1"
"github.com/crossplane/crossplane-runtime/v2/pkg/fieldpath"
"google.golang.org/protobuf/encoding/protojson"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/util/json"
"github.com/crossplane/function-sdk-go/errors"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
)
const recursionMaxNums = 1000
func getFunctions() []template.FuncMap {
return []template.FuncMap{
{
"randomChoice": randomChoice,
"toYaml": toYaml,
"fromYaml": fromYaml,
"getResourceCondition": getResourceCondition,
"setResourceNameAnnotation": setResourceNameAnnotation,
"getComposedResource": getComposedResource,
"getCompositeResource": getCompositeResource,
"getExtraResources": getExtraResources,
"getExtraResourcesFromContext": getExtraResourcesFromContext,
"getCredentialData": getCredentialData,
},
}
}
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 getFunctions() {
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())) //nolint:gosec // strong random number generation is not required
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 {
if err := fieldpath.Pave(res).GetValueInto("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, any) (string, error) {
includedNames := make(map[string]int)
return func(name string, data any) (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
}
func getExtraResources(req map[string]any, name string) []any {
var ers []any
path := fmt.Sprintf("requiredResources[%s].items", name)
if err := fieldpath.Pave(req).GetValueInto(path, &ers); err != nil {
path := fmt.Sprintf("extraResources[%s].items", name)
if err := fieldpath.Pave(req).GetValueInto(path, &ers); err != nil {
return nil
}
}
return ers
}
func getExtraResourcesFromContext(req map[string]any, name string) []any {
var ers []any
path := fmt.Sprintf("context[%s][%s].items", extraResourcesContextKey, name)
if err := fieldpath.Pave(req).GetValueInto(path, &ers); err != nil {
return nil
}
return ers
}
func getCredentialData(mReq map[string]any, credName string) map[string][]byte {
req, err := convertFromMap(mReq)
if err != nil {
return nil
}
switch req.GetCredentials()[credName].GetSource().(type) {
case *fnv1.Credentials_CredentialData:
return req.GetCredentials()[credName].GetCredentialData().GetData()
default:
return nil
}
}
func convertFromMap(mReq map[string]any) (*fnv1.RunFunctionRequest, error) {
jReq, err := json.Marshal(&mReq)
if err != nil {
return nil, errors.Wrap(err, "cannot marshal map[string]any to json")
}
req := &fnv1.RunFunctionRequest{}
if err := protojson.Unmarshal(jReq, req); err != nil {
return nil, errors.Wrap(err, "cannot unmarshal request from json to proto")
}
return req, nil
}