Skip to content

Commit a636459

Browse files
authored
Merge pull request #438 from bobh66/inputcontext
Add support for reading templates from an environment key
2 parents 7e5ee60 + 0d845eb commit a636459

File tree

13 files changed

+449
-11
lines changed

13 files changed

+449
-11
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ spec:
4343
4444
## Using this function
4545
46-
This function can load templates from two sources: `Inline` and `FileSystem`.
46+
This function can load templates from three sources: `Inline`, `FileSystem` and `Environment`.
4747

4848
Use the `Inline` source to specify a simple template inline in your Composition.
4949
Multiple YAML manifests can be specified using the `---` document separator.
5050

5151
Use the `FileSystem` source to specify a directory of templates. The
5252
`FileSystem` source treats all files under the specified directory as templates.
5353

54+
Use the `Environment` source to specify a key in the context environment that contains the templates.
55+
This allows templates to be dynamically loaded from sources such as `EnvironmentConfigs`.
56+
5457
The templates are passed a [`RunFunctionRequest`][bsr] as data. This means that
5558
you can access the composite resource, any composed resources, and the function
5659
pipeline context using notation like:

example/environment/README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# The `Environment` source
2+
3+
function-go-templating can read a template from an `Environment` key.
4+
5+
## Testing This Function Locally
6+
7+
You can run your function locally and test it using [`crossplane render`](https://docs.crossplane.io/latest/cli/command-reference/#render)
8+
with these example manifests.
9+
10+
```shell
11+
crossplane render \
12+
--extra-resources environmentConfigs.yaml \
13+
--include-context \
14+
xr.yaml composition.yaml functions.yaml
15+
```
16+
17+
Will produce an output like:
18+
19+
```shell
20+
---
21+
apiVersion: example.crossplane.io/v1
22+
kind: XR
23+
metadata:
24+
name: example-xr
25+
status:
26+
conditions:
27+
- lastTransitionTime: "2024-01-01T00:00:00Z"
28+
reason: Available
29+
status: "True"
30+
type: Ready
31+
fromEnv: e
32+
---
33+
apiVersion: render.crossplane.io/v1beta1
34+
fields:
35+
apiextensions.crossplane.io/environment:
36+
apiVersion: internal.crossplane.io/v1alpha1
37+
array:
38+
- "1"
39+
- "2"
40+
complex:
41+
a: b
42+
c:
43+
d: e
44+
f: "1"
45+
kind: Environment
46+
nestedEnvUpdate:
47+
hello: world
48+
template: |
49+
---
50+
apiVersion: meta.gotemplating.fn.crossplane.io/v1alpha1
51+
kind: Context
52+
data:
53+
# update existing EnvironmentConfig by using the "apiextensions.crossplane.io/environment" key
54+
"apiextensions.crossplane.io/environment":
55+
kind: Environment
56+
apiVersion: internal.crossplane.io/v1alpha1
57+
update: environment
58+
nestedEnvUpdate:
59+
hello: world
60+
array:
61+
- "1"
62+
- "2"
63+
# read existing context and move it to another key
64+
"other-context-key":
65+
complex: {{ index .context "apiextensions.crossplane.io/environment" "complex" | toYaml | nindent 6 }}
66+
# Create a new Context key and populate it with data
67+
newkey:
68+
hello: world
69+
---
70+
apiVersion: example.crossplane.io/v1
71+
kind: XR
72+
status:
73+
fromEnv: {{ index .context "apiextensions.crossplane.io/environment" "complex" "c" "d" }}
74+
update: environment
75+
newkey:
76+
hello: world
77+
other-context-key:
78+
complex:
79+
a: b
80+
c:
81+
d: e
82+
f: "1"```
83+
84+
## Debugging This Function
85+
86+
First we need to run the command in debug mode. In a terminal Window Run:
87+
88+
```shell
89+
# Run the function locally
90+
$ go run . --insecure --debug
91+
```
92+
93+
Next, set the go-templating function `render.crossplane.io/runtime: Development` annotation so that
94+
`crossplane render` communicates with the local process instead of downloading an image:
95+
96+
```yaml
97+
apiVersion: pkg.crossplane.io/v1beta1
98+
kind: Function
99+
metadata:
100+
name: crossplane-contrib-function-go-templating
101+
annotations:
102+
render.crossplane.io/runtime: Development
103+
spec:
104+
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.6.0
105+
```
106+
107+
While the function is running in one terminal, open another terminal window and run `crossplane render`.
108+
The function should output debug-level logs in the terminal.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apiVersion: apiextensions.crossplane.io/v1
2+
kind: Composition
3+
metadata:
4+
name: go-template-context.example.crossplane.io
5+
spec:
6+
compositeTypeRef:
7+
apiVersion: example.crossplane.io/v1
8+
kind: XR
9+
mode: Pipeline
10+
pipeline:
11+
- step: environmentConfigs
12+
functionRef:
13+
name: crossplane-contrib-function-environment-configs
14+
input:
15+
apiVersion: environmentconfigs.fn.crossplane.io/v1beta1
16+
kind: Input
17+
spec:
18+
environmentConfigs:
19+
- type: Reference
20+
ref:
21+
name: sampletemplate
22+
- type: Reference
23+
ref:
24+
name: example-config
25+
- step: go-templating-update-context
26+
functionRef:
27+
name: crossplane-contrib-function-go-templating
28+
input:
29+
apiVersion: gotemplating.fn.crossplane.io/v1beta1
30+
kind: GoTemplate
31+
source: Environment
32+
environment:
33+
key: template
34+
- step: automatically-detect-ready-composed-resources
35+
functionRef:
36+
name: crossplane-contrib-function-auto-ready
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apiVersion: apiextensions.crossplane.io/v1alpha1
2+
kind: EnvironmentConfig
3+
metadata:
4+
name: sampletemplate
5+
data:
6+
template: |
7+
---
8+
apiVersion: meta.gotemplating.fn.crossplane.io/v1alpha1
9+
kind: Context
10+
data:
11+
# update existing EnvironmentConfig by using the "apiextensions.crossplane.io/environment" key
12+
"apiextensions.crossplane.io/environment":
13+
kind: Environment
14+
apiVersion: internal.crossplane.io/v1alpha1
15+
update: environment
16+
nestedEnvUpdate:
17+
hello: world
18+
array:
19+
- "1"
20+
- "2"
21+
# read existing context and move it to another key
22+
"other-context-key":
23+
complex: {{ index .context "apiextensions.crossplane.io/environment" "complex" | toYaml | nindent 6 }}
24+
# Create a new Context key and populate it with data
25+
newkey:
26+
hello: world
27+
---
28+
apiVersion: example.crossplane.io/v1
29+
kind: XR
30+
status:
31+
fromEnv: {{ index .context "apiextensions.crossplane.io/environment" "complex" "c" "d" }}
32+
---
33+
apiVersion: apiextensions.crossplane.io/v1alpha1
34+
kind: EnvironmentConfig
35+
metadata:
36+
name: example-config
37+
data:
38+
complex:
39+
a: b
40+
c:
41+
d: e
42+
f: "1"

example/environment/functions.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
apiVersion: pkg.crossplane.io/v1beta1
3+
kind: Function
4+
metadata:
5+
name: crossplane-contrib-function-environment-configs
6+
spec:
7+
# This is ignored when using the Development runtime.
8+
package: xpkg.upbound.io/crossplane-contrib/function-environment-configs:v0.2.0
9+
---
10+
apiVersion: pkg.crossplane.io/v1beta1
11+
kind: Function
12+
metadata:
13+
name: crossplane-contrib-function-go-templating
14+
annotations:
15+
# This tells crossplane beta render to connect to the function locally.
16+
render.crossplane.io/runtime: Development
17+
spec:
18+
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.9.0
19+
---
20+
apiVersion: pkg.crossplane.io/v1beta1
21+
kind: Function
22+
metadata:
23+
name: crossplane-contrib-function-auto-ready
24+
spec:
25+
package: xpkg.upbound.io/crossplane-contrib/function-auto-ready:v0.4.0

example/environment/xr.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: example.crossplane.io/v1
2+
kind: XR
3+
metadata:
4+
name: example-xr
5+
spec: {}

example/environment/xrd.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
apiVersion: apiextensions.crossplane.io/v1
3+
kind: CompositeResourceDefinition
4+
metadata:
5+
name: xrs.example.crossplane.io
6+
spec:
7+
group: example.crossplane.io
8+
names:
9+
kind: XR
10+
plural: xrs
11+
connectionSecretKeys:
12+
- test
13+
versions:
14+
- name: v1
15+
served: true
16+
referenceable: true
17+
schema:
18+
openAPIV3Schema:
19+
type: object
20+
properties:
21+
status:
22+
type: object
23+
properties:
24+
fromEnv:
25+
type: string

fn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
6363
return rsp, nil
6464
}
6565

66-
tg, err := NewTemplateSourceGetter(f.fsys, in)
66+
tg, err := NewTemplateSourceGetter(f.fsys, req.GetContext(), in)
6767
if err != nil {
6868
response.Fatal(rsp, errors.Wrap(err, "invalid function input"))
6969
return rsp, nil

0 commit comments

Comments
 (0)