-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
170 lines (147 loc) · 4.63 KB
/
server.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
package server
import (
"context"
"encoding/json"
fnapi "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/resource"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/structpb"
"k8s.io/apimachinery/pkg/runtime"
"github.com/mistermx/crossplane-function-server/apis/v1alpha1"
)
// Server is a special Crossplane function which acts as a router that
// distributes request among several subroutines (aka server functions).
//
// A server receives a dedicated input payload by the composition that tells
// the server which subroutine to call.
type Server struct {
fnapi.UnimplementedFunctionRunnerServiceServer
functions map[string]ServerFunction
}
func (s *Server) RunFunction(ctx context.Context, req *fnapi.RunFunctionRequest) (*fnapi.RunFunctionResponse, error) {
serverInput := &v1alpha1.ServerInput{}
if err := resource.AsObject(req.Input, serverInput); err != nil {
return nil, errors.Wrap(err, "cannot parse input")
}
fn, exists := s.functions[serverInput.Spec.FunctionName]
if !exists {
return nil, errors.Errorf("no function with name %q", serverInput.Spec.FunctionName)
}
fnReq := RunServerFunctionRequest{
Req: req,
ServerInput: serverInput,
}
fnRes := RunServerFunctionResponse{}
if err := fn.Run(ctx, &fnReq, &fnRes); err != nil {
return nil, errors.Wrapf(err, "error while running subroutine function %q", serverInput.Spec.FunctionName)
}
res := &fnapi.RunFunctionResponse{
Desired: req.GetDesired(),
Context: req.GetContext(),
}
if fnRes.DesiredComposite != nil {
res.Desired.Composite = fnRes.DesiredComposite
}
if fnRes.DesiredComposed != nil {
res.Desired.Resources = fnRes.DesiredComposed
}
if fnRes.DesiredContext != nil {
res.Context = fnRes.DesiredContext
}
if fnRes.Results != nil {
res.Results = fnRes.Results
}
return res, nil
}
type RunServerFunctionRequest struct {
Req *fnapi.RunFunctionRequest
ServerInput *v1alpha1.ServerInput
}
func (r *RunServerFunctionRequest) GetNativeRequest() *fnapi.RunFunctionRequest {
return r.Req
}
func (r *RunServerFunctionRequest) GetComposite(target runtime.Object) error {
return resource.AsObject(r.Req.GetObserved().GetComposite().GetResource(), target)
}
func (r *RunServerFunctionRequest) GetComposed(name string, target runtime.Object) error {
resources := r.Req.GetObserved().GetResources()
res, exists := resources[name]
if !exists {
return NewErrorNotFound(name)
}
return resource.AsObject(res.GetResource(), target)
}
func (r *RunServerFunctionRequest) GetInput(target any) error {
return json.Unmarshal(r.ServerInput.Spec.Input.Raw, target)
}
type RunServerFunctionResponse struct {
DesiredComposite *fnapi.Resource
DesiredComposed map[string]*fnapi.Resource
DesiredContext *structpb.Struct
Results []*fnapi.Result
}
func (r *RunServerFunctionResponse) SetCompositeRaw(res *fnapi.Resource) {
r.DesiredComposite = res
}
func (r *RunServerFunctionResponse) SetComposite(o runtime.Object, mods ...ResourceModifier) error {
raw, err := resource.AsStruct(o)
if err != nil {
return err
}
r.DesiredComposite = &fnapi.Resource{
Resource: raw,
}
for _, m := range mods {
m(r.DesiredComposite)
}
return nil
}
func (r *RunServerFunctionResponse) GetComposite(target runtime.Object) error {
if r.DesiredComposite == nil || r.DesiredComposite.Resource == nil {
return nil // Return an error here?
}
return resource.AsObject(r.DesiredComposite.Resource, target)
}
func (r *RunServerFunctionResponse) SetComposedRaw(name string, res *fnapi.Resource) {
if r.DesiredComposed == nil {
r.DesiredComposed = map[string]*fnapi.Resource{}
}
r.DesiredComposed[name] = res
}
func (r *RunServerFunctionResponse) SetComposed(name string, o runtime.Object, mods ...ResourceModifier) error {
raw, err := resource.AsStruct(o)
if err != nil {
return err
}
res := &fnapi.Resource{
Resource: raw,
}
for _, m := range mods {
m(res)
}
r.SetComposedRaw(name, res)
return nil
}
func (r *RunServerFunctionResponse) GetComposed(name string, target runtime.Object) error {
state, exists := r.DesiredComposed[name]
if !exists {
return NewErrorNotFound(name)
}
return resource.AsObject(state.Resource, target)
}
func (r *RunServerFunctionResponse) SetContextField(key string, value any) error {
if r.DesiredContext == nil {
r.DesiredContext = &structpb.Struct{
Fields: map[string]*structpb.Value{},
}
}
raw, err := structpb.NewValue(value)
if err != nil {
return errors.Wrap(err, "cannot convert context value to protobuf")
}
r.DesiredContext.Fields[key] = raw
return nil
}
func (r *RunServerFunctionResponse) SetNativeResults(results []*fnapi.Result) {
r.Results = results
}