Skip to content

Commit

Permalink
Add examples for toYaml & fromYaml
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-di committed Nov 13, 2023
1 parent 1043560 commit 1e6331b
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@

# Go workspace file
go.work

# Binaries
function-go-templating
61 changes: 31 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# function-go-templating

[![CI](https://github.com/crossplane-contrib/function-go-templating/actions/workflows/ci.yml/badge.svg)](https://github.com/crossplane-contrib/function-go-templating/actions/workflows/ci.yml) ![GitHub release (latest SemVer)](https://img.shields.io/github/release/crossplane-contrib/function-go-templating)

This [composition function][docs-functions] allows you to compose Crossplane
resources using [Go templates][go-templates]. If you've written a [Helm
chart][helm-chart] before, using this function will be a familiar experience.
chart][helm-chart] before, using this function will be a familiar experience.

Here's an example:

Expand All @@ -18,26 +19,26 @@ spec:
kind: XR
mode: Pipeline
pipeline:
- step: create-a-bucket
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: Bucket
metadata:
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: bucket
spec:
forProvider:
region: {{ .observed.composite.resource.spec.region }}
- step: automatically-detect-ready-composed-resources
functionRef:
name: function-auto-ready
- step: create-a-bucket
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: Bucket
metadata:
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: bucket
spec:
forProvider:
region: {{ .observed.composite.resource.spec.region }}
- step: automatically-detect-ready-composed-resources
functionRef:
name: function-auto-ready
```
## Using this function
Expand All @@ -54,10 +55,10 @@ The templates are passed a [`RunFunctionRequest`][bsr] as data. This means that
you can access the composite resource, any composed resources, and the function
pipeline context using notation like:

* `{{ .observed.composite.resource.metadata.name }}`
* `{{ .desired.composite.resource.status.widgets }}`
* `{{ (index .desired.composed.resource "resource-name").spec.widgets }}`
* `{{ index .context "apiextensions.crossplane.io/environment" }}`
- `{{ .observed.composite.resource.metadata.name }}`
- `{{ .desired.composite.resource.status.widgets }}`
- `{{ (index .desired.composed.resource "resource-name").spec.widgets }}`
- `{{ index .context "apiextensions.crossplane.io/environment" }}`

This function supports all of Go's [built-in template functions][builtin]. The
above examples use the `index` function to access keys like `resource-name` that
Expand Down Expand Up @@ -99,11 +100,11 @@ about `crossplane beta render`.

## Additional functions

| Name | Description |
| -------------- | --------------------------------------- |
| `randomChoice` | Randomly selects one of a given strings |
| `toYaml` | Marshals any object into a YAML string |
| `fromYaml`` | Unmarshals a YAML string into an object |
| Name | Description |
| ------------------------------------------------- | --------------------------------------- |
| [`randomChoice``](example/functions/randomChoice) | Randomly selects one of a given strings |
| [`toYaml`](example/functions/toYaml) | Marshals any object into a YAML string |
| [`fromYaml`](example/functions/fromYaml) | Unmarshals a YAML string into an object |

## Developing this function

Expand Down
13 changes: 13 additions & 0 deletions example/.dev/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
# Use this function yaml when developing and executing the function directly instead of in a container
# E.g. `go run . --insecure --debug --address=":9443"`

apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-go-templating
annotations:
render.crossplane.io/runtime: Development
render.crossplane.io/runtime-development-target: localhost:9443
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:latest # ignored
25 changes: 25 additions & 0 deletions example/functions/fromYaml/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: example-function-from-yaml
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1beta1
kind: XR
mode: Pipeline
pipeline:
- step: render-templates
functionRef:
name: function-go-templating
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
source: Inline
inline:
template: |
---
apiVersion: {{ .observed.composite.resource.apiVersion }}
kind: {{ .observed.composite.resource.kind }}
status:
# Extract single value from encoded yaml string
dummy: {{ (.observed.composite.resource.spec.yamlBlob | fromYaml).key2 }}
6 changes: 6 additions & 0 deletions example/functions/fromYaml/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-go-templating
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:latest
9 changes: 9 additions & 0 deletions example/functions/fromYaml/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: example.crossplane.io/v1beta1
kind: XR
metadata:
name: example
spec:
yamlBlob: |
key1: value1
key2: value2
key3: value3
25 changes: 25 additions & 0 deletions example/functions/toYaml/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: example-function-to-yaml
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1beta1
kind: XR
mode: Pipeline
pipeline:
- step: render-templates
functionRef:
name: function-go-templating
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
source: Inline
inline:
template: |
---
apiVersion: {{ .observed.composite.resource.apiVersion }}
kind: {{ .observed.composite.resource.kind }}
status:
# Copy the whole 'complexDictionary' as is by fomatting it as yaml
dummy: {{ .observed.composite.resource.spec.complexDictionary | toYaml | nindent 7 }}
6 changes: 6 additions & 0 deletions example/functions/toYaml/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-go-templating
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:latest
12 changes: 12 additions & 0 deletions example/functions/toYaml/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: example.crossplane.io/v1beta1
kind: XR
metadata:
name: example
spec:
complexDictionary:
scalar1: true
scalar2: text
scalar3: 123
list:
- abc
- def

0 comments on commit 1e6331b

Please sign in to comment.