Skip to content

Commit

Permalink
Merge pull request #115 from ytsarev/multistep
Browse files Browse the repository at this point in the history
Provider multistep pipeline example mixing P&T and Go Templating
  • Loading branch information
negz authored May 4, 2024
2 parents c4f2319 + 7a60054 commit 00c3238
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ It's not just patches either. You can use P&T to derive composite resource
connection details from a resource produced by another function, or use it to
determine whether a resource produced by another function is ready.

A straightforward example for multistep mix and match pipeline with
function-patch-and-transform and function-go-templating can be found
[here](./example/multistep)

### Decouple P&T development from Crossplane core

When P&T development happens in a function, it's not coupled to the Crossplane
Expand Down
2 changes: 2 additions & 0 deletions example/multistep/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
render:
crossplane beta render xr.yaml composition.yaml functions.yaml -r
90 changes: 90 additions & 0 deletions example/multistep/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Example multistep pipeline

Example Functions Pipeline with the 3 steps:


1. function-patch-and-transform
2. function-go-templating
3. function-patch-and-transform again

The more details mechanics of each step described below

1. function-patch-and-transform creates `bucket`
2. function-go-templating creates `bucketACL` and uses the data from the
previous pipelines step to compose the resource spec
```
region: {{ .desired.resources.bucket.resource.spec.forProvider.region }}
```
3. function-patch-and-transform is used again to patch the `bucketACL` with the
data from XR spec. Notice that `base` is omitted and resource `name` is
matching the one that is set by the function-go-templating with `{{ setResourceNameAnnotation "bucketACL" }}`

To render `make render` target is available:

```
crossplane beta render xr.yaml composition.yaml functions.yaml -r
---
apiVersion: example.crossplane.io/v1
kind: XR
metadata:
name: example-xr
status:
conditions:
- lastTransitionTime: "2024-01-01T00:00:00Z"
message: 'Unready resources: bucket, bucketACL'
reason: Creating
status: "False"
type: Ready
---
apiVersion: s3.aws.upbound.io/v1beta1
kind: Bucket
metadata:
annotations:
crossplane.io/composition-resource-name: bucket
generateName: example-xr-
labels:
crossplane.io/composite: example-xr
ownerReferences:
- apiVersion: example.crossplane.io/v1
blockOwnerDeletion: true
controller: true
kind: XR
name: example-xr
uid: ""
spec:
forProvider:
region: eu-north-1
---
apiVersion: s3.aws.upbound.io/v1beta1
kind: BucketACL
metadata:
annotations:
crossplane.io/composition-resource-name: bucketACL
generateName: example-xr-
labels:
crossplane.io/composite: example-xr
ownerReferences:
- apiVersion: example.crossplane.io/v1
blockOwnerDeletion: true
controller: true
kind: XR
name: example-xr
uid: ""
spec:
forProvider:
acl: private
bucketSelector:
matchControllerRef: true
region: eu-north-1
```

Notice that `BucketACL` is patched as expected

```
spec:
forProvider:
acl: private # acl value that is set on 3rd pipeline step is in place
bucketSelector:
matchControllerRef: true
region: eu-north-1 # region value that is set on 2nd pipeline step is in place
```
64 changes: 64 additions & 0 deletions example/multistep/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-patch-and-transform
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
mode: Pipeline
pipeline:
- step: patch-and-transform
functionRef:
name: function-patch-and-transform
input:
apiVersion: pt.fn.crossplane.io/v1beta1
kind: Resources
resources:
- name: bucket
base:
apiVersion: s3.aws.upbound.io/v1beta1
kind: Bucket
patches:
- type: FromCompositeFieldPath
fromFieldPath: "spec.location"
toFieldPath: "spec.forProvider.region"
transforms:
- type: map
map:
EU: "eu-north-1"
US: "us-east-2"

- step: render-templates
functionRef:
name: function-go-templating
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
source: Inline
inline:
template: |
---
apiVersion: s3.aws.upbound.io/v1beta1
kind: BucketACL
metadata:
annotations:
{{ setResourceNameAnnotation "bucketACL" }}
spec:
forProvider:
bucketSelector:
matchControllerRef: true
region: {{ .desired.resources.bucket.resource.spec.forProvider.region }}
- step: patch-and-transform-again
functionRef:
name: function-patch-and-transform
input:
apiVersion: pt.fn.crossplane.io/v1beta1
kind: Resources
resources:
- name: bucketACL # resource name matches the one from function-go-templating setResourceNameAnnotation above, no `base` specified
patches:
- type: FromCompositeFieldPath
fromFieldPath: "spec.acl"
toFieldPath: "spec.forProvider.acl"
14 changes: 14 additions & 0 deletions example/multistep/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-patch-and-transform
spec:
package: xpkg.upbound.io/crossplane-contrib/function-patch-and-transform:v0.5.0
---
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-go-templating
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.4.1
8 changes: 8 additions & 0 deletions example/multistep/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Replace this with your XR!
apiVersion: example.crossplane.io/v1
kind: XR
metadata:
name: example-xr
spec:
location: EU
acl: private

0 comments on commit 00c3238

Please sign in to comment.