-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfn.go
More file actions
56 lines (49 loc) · 1.33 KB
/
Copy pathfn.go
File metadata and controls
56 lines (49 loc) · 1.33 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
package main
import (
"context"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"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/response"
"reflect"
)
const (
FunctionContextKeyEnvironment = "apiextensions.crossplane.io/environment"
)
// 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) {
rsp := response.To(req, response.DefaultTTL)
resources, err := request.GetDesiredComposedResources(req)
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, err.Error()))
}
for _, v := range resources {
spec := v.Resource.Unstructured.Object["spec"]
if spec != nil {
cleanSpec(spec.(map[string]interface{}))
}
}
return rsp, nil
}
func cleanSpec(m map[string]interface{}) {
if len(m) != 0 {
for k, v := range m {
if v == nil {
delete(m, k)
}
if reflect.TypeOf(v).Kind() == reflect.Map {
mm := v.(map[string]interface{})
cleanSpec(mm)
if len(mm) == 0 {
delete(m, k)
}
}
}
}
}