generated from crossplane/function-template-go
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fn.go
281 lines (257 loc) · 9.37 KB
/
fn.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"context"
"encoding/json"
"reflect"
"sort"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/pkg/logging"
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/resource"
"github.com/crossplane/function-sdk-go/response"
"github.com/crossplane-contrib/function-extra-resources/input/v1beta1"
)
// Key to retrieve extras at.
const (
FunctionContextKeyExtraResources = "apiextensions.crossplane.io/extra-resources"
)
// Function returns whatever response you ask it to.
type Function struct {
fnv1beta1.UnimplementedFunctionRunnerServiceServer
log logging.Logger
}
// RunFunction runs the Function.
func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequest) (*fnv1beta1.RunFunctionResponse, error) {
f.log.Info("Running function", "tag", req.GetMeta().GetTag())
rsp := response.To(req, response.DefaultTTL)
// Get function input.
in := &v1beta1.Input{}
if err := request.GetInput(req, in); err != nil {
response.Fatal(rsp, errors.Errorf("cannot get Function input from %T: %w", req, err))
return rsp, nil
}
// Get XR the pipeline targets.
oxr, err := request.GetObservedCompositeResource(req)
if err != nil {
response.Fatal(rsp, errors.Errorf("cannot get observed composite resource: %w", err))
return rsp, nil
}
// Build extraResource Requests.
requirements, err := buildRequirements(in, oxr)
if err != nil {
response.Fatal(rsp, errors.Errorf("could not build extra resource requirements: %w", err))
return rsp, nil
}
rsp.Requirements = requirements
// The request response cycle for the Crossplane ExtraResources API requires that function-extra-resources
// tells Crossplane what it wants.
// Then a new rquest is sent to function-extra-resources with those resources present at the ExtraResources field.
//
// function-extra-resources does not know if it has requested the resources already or not.
//
// If it has and these resources are now present, proceed with verification and conversion.
if req.ExtraResources == nil {
f.log.Debug("No extra resources present, exiting", "requirements", rsp.GetRequirements())
return rsp, nil
}
// Pull extra resources from the ExtraResources request field.
extraResources, err := request.GetExtraResources(req)
if err != nil {
response.Fatal(rsp, errors.Errorf("fetching extra resources %T: %w", req, err))
return rsp, nil
}
// Sort and verify min/max selected.
// Sorting is required for determinism.
verifiedExtras, err := verifyAndSortExtras(in, extraResources)
if err != nil {
response.Fatal(rsp, errors.Errorf("verifying and sorting extra resources: %w", err))
return rsp, nil
}
// For now cheaply convert to JSON for serializing.
//
// TODO(reedjosh): look into resources.AsStruct or simlar since unsturctured k8s objects are already almost json.
// structpb.NewList(v []interface{}) should create an array like.
// Combining this and similar structures from the structpb lib should should be done to create
// a map[string][object] container into which the found extra resources can be dumped.
//
// The found extra resources should then be directly marhsal-able via:
// obj := &unstructured.Unstructured{}
// obj.MarshalJSON()
b, err := json.Marshal(verifiedExtras)
if err != nil {
response.Fatal(rsp, errors.Errorf("cannot marshal %T: %w", verifiedExtras, err))
return rsp, nil
}
s := &structpb.Struct{}
err = protojson.Unmarshal(b, s)
if err != nil {
response.Fatal(rsp, errors.Errorf("cannot unmarshal %T into %T: %w", extraResources, s, err))
return rsp, nil
}
response.SetContextKey(rsp, FunctionContextKeyExtraResources, structpb.NewStructValue(s))
return rsp, nil
}
// Build requirements takes input and outputs an array of external resoruce requirements to request
// from Crossplane's external resource API.
func buildRequirements(in *v1beta1.Input, xr *resource.Composite) (*fnv1beta1.Requirements, error) {
extraResources := make(map[string]*fnv1beta1.ResourceSelector, len(in.Spec.ExtraResources))
for _, extraResource := range in.Spec.ExtraResources {
extraResName := extraResource.Into
switch extraResource.Type {
case v1beta1.ResourceSourceTypeReference, "":
extraResources[extraResName] = &fnv1beta1.ResourceSelector{
ApiVersion: extraResource.APIVersion,
Kind: extraResource.Kind,
Match: &fnv1beta1.ResourceSelector_MatchName{
MatchName: extraResource.Ref.Name,
},
}
case v1beta1.ResourceSourceTypeSelector:
matchLabels := map[string]string{}
for _, selector := range extraResource.Selector.MatchLabels {
switch selector.GetType() {
case v1beta1.ResourceSourceSelectorLabelMatcherTypeValue:
// TODO validate value not to be nil
matchLabels[selector.Key] = *selector.Value
case v1beta1.ResourceSourceSelectorLabelMatcherTypeFromCompositeFieldPath:
value, err := fieldpath.Pave(xr.Resource.Object).GetString(*selector.ValueFromFieldPath)
if err != nil {
if !selector.FromFieldPathIsOptional() {
return nil, errors.Wrapf(err, "cannot get value from field path %q", *selector.ValueFromFieldPath)
}
continue
}
matchLabels[selector.Key] = value
}
}
if len(matchLabels) == 0 {
continue
}
extraResources[extraResName] = &fnv1beta1.ResourceSelector{
ApiVersion: extraResource.APIVersion,
Kind: extraResource.Kind,
Match: &fnv1beta1.ResourceSelector_MatchLabels{
MatchLabels: &fnv1beta1.MatchLabels{Labels: matchLabels},
},
}
}
}
return &fnv1beta1.Requirements{ExtraResources: extraResources}, nil
}
// Verify Min/Max and sort extra resources by field path within a single kind.
func verifyAndSortExtras(in *v1beta1.Input, extraResources map[string][]resource.Extra, //nolint:gocyclo // TODO(reedjosh): refactor
) (cleanedExtras map[string][]unstructured.Unstructured, err error) {
cleanedExtras = make(map[string][]unstructured.Unstructured)
for _, extraResource := range in.Spec.ExtraResources {
extraResName := extraResource.Into
resources, ok := extraResources[extraResName]
if !ok {
return nil, errors.Errorf("cannot find expected extra resource %q", extraResName)
}
switch extraResource.GetType() {
case v1beta1.ResourceSourceTypeReference:
if len(resources) == 0 {
if in.Spec.Policy.IsResolutionPolicyOptional() {
continue
}
return nil, errors.Errorf("Required extra resource %q not found", extraResName)
}
if len(resources) > 1 {
return nil, errors.Errorf("expected exactly one extra resource %q, got %d", extraResName, len(resources))
}
cleanedExtras[extraResName] = append(cleanedExtras[extraResName], *resources[0].Resource)
case v1beta1.ResourceSourceTypeSelector:
selector := extraResource.Selector
if selector.MinMatch != nil && len(resources) < int(*selector.MinMatch) {
return nil, errors.Errorf("expected at least %d extra resources %q, got %d", *selector.MinMatch, extraResName, len(resources))
}
if err := sortExtrasByFieldPath(resources, selector.GetSortByFieldPath()); err != nil {
return nil, err
}
if selector.MaxMatch != nil && len(resources) > int(*selector.MaxMatch) {
resources = resources[:*selector.MaxMatch]
}
for _, r := range resources {
cleanedExtras[extraResName] = append(cleanedExtras[extraResName], *r.Resource)
}
}
}
return cleanedExtras, nil
}
// Sort extra resources by field path within a single kind.
func sortExtrasByFieldPath(extras []resource.Extra, path string) error { //nolint:gocyclo // TODO(phisco): refactor
if path == "" {
return errors.New("cannot sort by empty field path")
}
p := make([]struct {
ec resource.Extra
val any
}, len(extras))
var t reflect.Type
for i := range extras {
p[i].ec = extras[i]
val, err := fieldpath.Pave(extras[i].Resource.Object).GetValue(path)
if err != nil && !fieldpath.IsNotFound(err) {
return err
}
p[i].val = val
if val == nil {
continue
}
vt := reflect.TypeOf(val)
switch {
case t == nil:
t = vt
case t != vt:
return errors.Errorf("cannot sort values of different types %q and %q", t, vt)
}
}
if t == nil {
// we either have no values or all values are nil, we can just return
return nil
}
var err error
sort.Slice(p, func(i, j int) bool {
vali, valj := p[i].val, p[j].val
if vali == nil {
vali = reflect.Zero(t).Interface()
}
if valj == nil {
valj = reflect.Zero(t).Interface()
}
switch t.Kind() { //nolint:exhaustive // we only support these types
case reflect.Float64:
return vali.(float64) < valj.(float64)
case reflect.Float32:
return vali.(float32) < valj.(float32)
case reflect.Int64:
return vali.(int64) < valj.(int64)
case reflect.Int32:
return vali.(int32) < valj.(int32)
case reflect.Int16:
return vali.(int16) < valj.(int16)
case reflect.Int8:
return vali.(int8) < valj.(int8)
case reflect.Int:
return vali.(int) < valj.(int)
case reflect.String:
return vali.(string) < valj.(string)
default:
// should never happen
err = errors.Errorf("unsupported type %q for sorting", t)
return false
}
})
if err != nil {
return err
}
for i := 0; i < len(extras); i++ {
extras[i] = p[i].ec
}
return nil
}