forked from crossplane/function-template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfn.go
53 lines (42 loc) · 1.86 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
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"
"github.com/crossplane/function-template-go/input/v1beta1"
)
// 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())
// This creates a new response to the supplied request. Note that Functions
// are run in a pipeline! Other Functions may have run before this one. If
// they did, response.To will copy their desired state from req to rsp. Be
// sure to pass through any desired state your Function is not concerned
// with unmodified.
rsp := response.To(req, response.DefaultTTL)
// Input is supplied by the author of a Composition when they choose to run
// your Function. Input is arbitrary, except that it must be a KRM-like
// object. Supporting input is also optional - if you don't need to you can
// delete this, and delete the input directory.
in := &v1beta1.Input{}
if err := request.GetInput(req, in); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get Function input from %T", req))
return rsp, nil
}
// TODO: Add your Function logic here!
//
// Take a look at function-sdk-go for some utilities for working with req
// and rsp - https://pkg.go.dev/github.com/crossplane/function-sdk-go
//
// Also, be sure to look at the tips in README.md
response.Normalf(rsp, "I was run with input %q", in.Example)
return rsp, nil
}