garden
garden copied to clipboard
Some template strings (e.g. $forEach) don't work in K8s manifests
Bug
Current Behavior
Garden supports Garden template strings and functions in K8s manifests.
However, due to the fact that we're treating the manifest as a string, as opposed to JSON, it incorrectly handles values that aren't primitives.
Furthermore, even config that has been commented out will cause Garden to fail.
For example, the following config
# In e.g. manifests/deployment.yaml
apiVersion: apps/v1
kind: Deployment
spec:
template:
containers:
#
env:
$forEach: ${var.env}
$return:
name: ${item.key}
value: ${item.value}
will fail with:
err: TemplateStringError: Could not find key item. Available keys: command, datetime, environment, git, inputs, local, modules, project, providers, runtime, secrets, this, var and variables.
Even if you comment out the $forEach
loop, you'll see the same error.
Workaround
On 0.12.X you can work around this by declaring the variables in the Garden config in the same format they should be in the K8s manifest and then JSON encoding them. The example above would look like this:
# In garden.yml
kind: Module
variables:
env:
- name: FOO
value: "foo"
- name: BAR
value: "bar"
# In e.g. manifests/deployment.yaml
apiVersion: apps/v1
kind: Deployment
spec:
template:
containers:
#
env: ${jsonEncode(var.env)} # <--- JSON encode the var.env variable.
On 0.13 this will be fixed.
Note that we will not be fixing this on 0.12 since it's technically a breaking change.
I'm wondering if we should push this to 0.14 to reduce the number of breaking changes (and our own check list).
If we do include this with 0.13 we should try and detect and warn the user, otherwise things can fail quite unexpectedly.
Thanks! Let's push it to 0.14.
Fixed in #5270