Skip to content

Commit 7a60054

Browse files
committed
Provider multistep pipeline example mixing P&T and Go Templating
* Provide example for https://github.com/crossplane-contrib/function-patch-and-transform/blob/main/README.md#mix-and-match-pt-with-other-functions Signed-off-by: Yury Tsarev <yury@upbound.io>
1 parent c4f2319 commit 7a60054

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ It's not just patches either. You can use P&T to derive composite resource
6666
connection details from a resource produced by another function, or use it to
6767
determine whether a resource produced by another function is ready.
6868

69+
A straightforward example for multistep mix and match pipeline with
70+
function-patch-and-transform and function-go-templating can be found
71+
[here](./example/multistep)
72+
6973
### Decouple P&T development from Crossplane core
7074

7175
When P&T development happens in a function, it's not coupled to the Crossplane

example/multistep/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
render:
2+
crossplane beta render xr.yaml composition.yaml functions.yaml -r

example/multistep/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Example multistep pipeline
2+
3+
Example Functions Pipeline with the 3 steps:
4+
5+
6+
1. function-patch-and-transform
7+
2. function-go-templating
8+
3. function-patch-and-transform again
9+
10+
The more details mechanics of each step described below
11+
12+
1. function-patch-and-transform creates `bucket`
13+
2. function-go-templating creates `bucketACL` and uses the data from the
14+
previous pipelines step to compose the resource spec
15+
```
16+
region: {{ .desired.resources.bucket.resource.spec.forProvider.region }}
17+
```
18+
3. function-patch-and-transform is used again to patch the `bucketACL` with the
19+
data from XR spec. Notice that `base` is omitted and resource `name` is
20+
matching the one that is set by the function-go-templating with `{{ setResourceNameAnnotation "bucketACL" }}`
21+
22+
To render `make render` target is available:
23+
24+
```
25+
crossplane beta render xr.yaml composition.yaml functions.yaml -r
26+
---
27+
apiVersion: example.crossplane.io/v1
28+
kind: XR
29+
metadata:
30+
name: example-xr
31+
status:
32+
conditions:
33+
- lastTransitionTime: "2024-01-01T00:00:00Z"
34+
message: 'Unready resources: bucket, bucketACL'
35+
reason: Creating
36+
status: "False"
37+
type: Ready
38+
---
39+
apiVersion: s3.aws.upbound.io/v1beta1
40+
kind: Bucket
41+
metadata:
42+
annotations:
43+
crossplane.io/composition-resource-name: bucket
44+
generateName: example-xr-
45+
labels:
46+
crossplane.io/composite: example-xr
47+
ownerReferences:
48+
- apiVersion: example.crossplane.io/v1
49+
blockOwnerDeletion: true
50+
controller: true
51+
kind: XR
52+
name: example-xr
53+
uid: ""
54+
spec:
55+
forProvider:
56+
region: eu-north-1
57+
---
58+
apiVersion: s3.aws.upbound.io/v1beta1
59+
kind: BucketACL
60+
metadata:
61+
annotations:
62+
crossplane.io/composition-resource-name: bucketACL
63+
generateName: example-xr-
64+
labels:
65+
crossplane.io/composite: example-xr
66+
ownerReferences:
67+
- apiVersion: example.crossplane.io/v1
68+
blockOwnerDeletion: true
69+
controller: true
70+
kind: XR
71+
name: example-xr
72+
uid: ""
73+
spec:
74+
forProvider:
75+
acl: private
76+
bucketSelector:
77+
matchControllerRef: true
78+
region: eu-north-1
79+
```
80+
81+
Notice that `BucketACL` is patched as expected
82+
83+
```
84+
spec:
85+
forProvider:
86+
acl: private # acl value that is set on 3rd pipeline step is in place
87+
bucketSelector:
88+
matchControllerRef: true
89+
region: eu-north-1 # region value that is set on 2nd pipeline step is in place
90+
```

example/multistep/composition.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
apiVersion: apiextensions.crossplane.io/v1
2+
kind: Composition
3+
metadata:
4+
name: function-patch-and-transform
5+
spec:
6+
compositeTypeRef:
7+
apiVersion: example.crossplane.io/v1
8+
kind: XR
9+
mode: Pipeline
10+
pipeline:
11+
- step: patch-and-transform
12+
functionRef:
13+
name: function-patch-and-transform
14+
input:
15+
apiVersion: pt.fn.crossplane.io/v1beta1
16+
kind: Resources
17+
resources:
18+
- name: bucket
19+
base:
20+
apiVersion: s3.aws.upbound.io/v1beta1
21+
kind: Bucket
22+
patches:
23+
- type: FromCompositeFieldPath
24+
fromFieldPath: "spec.location"
25+
toFieldPath: "spec.forProvider.region"
26+
transforms:
27+
- type: map
28+
map:
29+
EU: "eu-north-1"
30+
US: "us-east-2"
31+
32+
- step: render-templates
33+
functionRef:
34+
name: function-go-templating
35+
input:
36+
apiVersion: gotemplating.fn.crossplane.io/v1beta1
37+
kind: GoTemplate
38+
source: Inline
39+
inline:
40+
template: |
41+
---
42+
apiVersion: s3.aws.upbound.io/v1beta1
43+
kind: BucketACL
44+
metadata:
45+
annotations:
46+
{{ setResourceNameAnnotation "bucketACL" }}
47+
spec:
48+
forProvider:
49+
bucketSelector:
50+
matchControllerRef: true
51+
region: {{ .desired.resources.bucket.resource.spec.forProvider.region }}
52+
53+
- step: patch-and-transform-again
54+
functionRef:
55+
name: function-patch-and-transform
56+
input:
57+
apiVersion: pt.fn.crossplane.io/v1beta1
58+
kind: Resources
59+
resources:
60+
- name: bucketACL # resource name matches the one from function-go-templating setResourceNameAnnotation above, no `base` specified
61+
patches:
62+
- type: FromCompositeFieldPath
63+
fromFieldPath: "spec.acl"
64+
toFieldPath: "spec.forProvider.acl"

example/multistep/functions.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
apiVersion: pkg.crossplane.io/v1beta1
3+
kind: Function
4+
metadata:
5+
name: function-patch-and-transform
6+
spec:
7+
package: xpkg.upbound.io/crossplane-contrib/function-patch-and-transform:v0.5.0
8+
---
9+
apiVersion: pkg.crossplane.io/v1beta1
10+
kind: Function
11+
metadata:
12+
name: function-go-templating
13+
spec:
14+
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.4.1

example/multistep/xr.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Replace this with your XR!
2+
apiVersion: example.crossplane.io/v1
3+
kind: XR
4+
metadata:
5+
name: example-xr
6+
spec:
7+
location: EU
8+
acl: private

0 commit comments

Comments
 (0)