|
| 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. |
0 commit comments