RunFunction runs the Function.
(ctx context.Context, req *fnv1.RunFunctionRequest)
| 31 | |
| 32 | // RunFunction runs the Function. |
| 33 | func (f *Function) RunFunction(ctx context.Context, req *fnv1.RunFunctionRequest) (*fnv1.RunFunctionResponse, error) { //nolint:gocognit // See below. |
| 34 | // This loop is fairly complex, but more readable with less abstraction. |
| 35 | |
| 36 | log := f.log.WithValues("tag", req.GetMeta().GetTag()) |
| 37 | log.Info("Running Function") |
| 38 | |
| 39 | // TODO(negz): We can probably use a longer TTL if all resources are ready. |
| 40 | rsp := response.To(req, response.DefaultTTL) |
| 41 | |
| 42 | input := &v1beta1.Resources{} |
| 43 | if err := request.GetInput(req, input); err != nil { |
| 44 | response.Fatal(rsp, errors.Wrap(err, "cannot get Function input")) |
| 45 | return rsp, nil |
| 46 | } |
| 47 | |
| 48 | // Our input is an opaque object nested in a Composition, so unfortunately |
| 49 | // it won't handle validation for us. |
| 50 | if err := ValidateResources(input); err != nil { |
| 51 | response.Fatal(rsp, errors.Wrap(err, "invalid Function input")) |
| 52 | return rsp, nil |
| 53 | } |
| 54 | |
| 55 | // The composite resource that actually exists. |
| 56 | oxr, err := request.GetObservedCompositeResource(req) |
| 57 | if err != nil { |
| 58 | response.Fatal(rsp, errors.Wrap(err, "cannot get observed composite resource")) |
| 59 | return rsp, nil |
| 60 | } |
| 61 | |
| 62 | log = log.WithValues( |
| 63 | "xr-version", oxr.Resource.GetAPIVersion(), |
| 64 | "xr-kind", oxr.Resource.GetKind(), |
| 65 | "xr-name", oxr.Resource.GetName(), |
| 66 | ) |
| 67 | |
| 68 | // The composite resource desired by previous functions in the pipeline. |
| 69 | dxr, err := request.GetDesiredCompositeResource(req) |
| 70 | if err != nil { |
| 71 | response.Fatal(rsp, errors.Wrap(err, "cannot get desired composite resource")) |
| 72 | return rsp, nil |
| 73 | } |
| 74 | |
| 75 | // This is a bit of a hack. The Functions spec tells us we should only |
| 76 | // return the desired status of the XR. Crossplane doesn't need anything |
| 77 | // else. It already knows the XR's GVK and name, and thus "re-injects" them |
| 78 | // into the desired state before applying it. However we need a GVK to be |
| 79 | // able to use runtime.DefaultUnstructuredConverter internally, which fails |
| 80 | // if you ask it to unmarshal JSON/YAML without a kind. Technically the |
| 81 | // Function spec doesn't say anything about APIVersion and Kind, so we can |
| 82 | // return these without being in violation. ;) |
| 83 | // https://github.com/crossplane/crossplane/blob/53f71/contributing/specifications/functions.md |
| 84 | dxr.Resource.SetAPIVersion(oxr.Resource.GetAPIVersion()) |
| 85 | dxr.Resource.SetKind(oxr.Resource.GetKind()) |
| 86 | |
| 87 | // The composed resources that actually exist. |
| 88 | observed, err := request.GetObservedComposedResources(req) |
| 89 | if err != nil { |
| 90 | response.Fatal(rsp, errors.Wrapf(err, "cannot get observed composed resources from %T", req)) |