Skip to content

Commit

Permalink
Add dollar variable as global context
Browse files Browse the repository at this point in the history
Signed-off-by: twobiers <[email protected]>
  • Loading branch information
twobiers committed Nov 20, 2024
1 parent 448e4ab commit 8e3f97d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ For more information, see the example in [recursive](example/recursive).
| [`setResourceNameAnnotation`](example/inline) | Returns the special resource-name annotation with given name |
| [`include`](example/functions/include) | Outputs template as a string |

## Additional variables

| Name | Description |
| ---- | ----------------------------------------------------------------------------------------------- |
| `$` | As for Helm, the `$` variable will always point to the root context, thus the function request. |

## Developing this function

This function uses [Go][go], [Docker][docker], and the [Crossplane CLI][cli] to
Expand Down
3 changes: 3 additions & 0 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const (
annotationKeyReady = "gotemplating.fn.crossplane.io/ready"

metaApiVersion = "meta.gotemplating.fn.crossplane.io/v1alpha1"

itemDollar = "$"
)

// RunFunction runs the Function.
Expand Down Expand Up @@ -82,6 +84,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
response.Fatal(rsp, errors.Wrap(err, "cannot convert request to map"))
return rsp, nil
}
reqMap[itemDollar] = &reqMap

f.log.Debug("constructed request map", "request", reqMap)

Expand Down
39 changes: 39 additions & 0 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
var (
cd = `{"apiVersion":"example.org/v1","kind":"CD","metadata":{"annotations":{"gotemplating.fn.crossplane.io/composition-resource-name":"cool-cd"},"name":"cool-cd"}}`
cdTmpl = `{"apiVersion":"example.org/v1","kind":"CD","metadata":{"annotations":{"gotemplating.fn.crossplane.io/composition-resource-name":"cool-cd"},"name":"cool-cd","labels":{"belongsTo":{{.observed.composite.resource.metadata.name|quote}}}}}`
cdTmplWithDollar = `{"apiVersion":"example.org/v1","kind":"CD","metadata":{"annotations":{"gotemplating.fn.crossplane.io/composition-resource-name":"cool-cd"},"name":"cool-cd","labels":{"belongsTo":{{ $.observed.composite.resource.metadata.name|quote}}}}}`
cdWrongTmpl = `{"apiVersion":"example.org/v1","kind":"CD","metadata":{"name":"cool-cd","labels":{"belongsTo":{{.invalid-key}}}}}`
cdMissingKind = `{"apiVersion":"example.org/v1"}`
cdMissingResourceName = `{"apiVersion":"example.org/v1","kind":"CD","metadata":{"name":"cool-cd"}}`
Expand Down Expand Up @@ -316,6 +317,44 @@ func TestRunFunction(t *testing.T) {
},
},
},
"ResponseIsReturnedWithTemplatingFromGlobalContext": {
reason: "The Function should be able to template by accessing the global context from the dollar variable.",
args: args{
req: &fnv1beta1.RunFunctionRequest{
Meta: &fnv1beta1.RequestMeta{Tag: "templates"},
Input: resource.MustStructObject(
&v1beta1.GoTemplate{
Source: v1beta1.InlineSource,
Inline: &v1beta1.TemplateSourceInline{Template: cdTmplWithDollar},
}),
Observed: &fnv1beta1.State{
Composite: &fnv1beta1.Resource{
Resource: resource.MustStructJSON(xr),
},
},
Desired: &fnv1beta1.State{
Composite: &fnv1beta1.Resource{
Resource: resource.MustStructJSON(xr),
},
},
},
},
want: want{
rsp: &fnv1beta1.RunFunctionResponse{
Meta: &fnv1beta1.ResponseMeta{Tag: "templates", Ttl: durationpb.New(response.DefaultTTL)},
Desired: &fnv1beta1.State{
Composite: &fnv1beta1.Resource{
Resource: resource.MustStructJSON(xr),
},
Resources: map[string]*fnv1beta1.Resource{
"cool-cd": {
Resource: resource.MustStructJSON(`{"apiVersion": "example.org/v1","kind":"CD","metadata":{"annotations":{},"name":"cool-cd","labels":{"belongsTo":"cool-xr"}}}`),
},
},
},
},
},
},
"UpdateDesiredCompositeStatus": {
reason: "The Function should update the desired composite resource status.",
args: args{
Expand Down

0 comments on commit 8e3f97d

Please sign in to comment.