garden icon indicating copy to clipboard operation
garden copied to clipboard

Some template strings (e.g. $forEach) don't work in K8s manifests

Open eysi09 opened this issue 1 year ago • 3 comments

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.

eysi09 avatar Mar 07 '23 11:03 eysi09

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.

eysi09 avatar Apr 25 '23 11:04 eysi09

Thanks! Let's push it to 0.14.

vvagaytsev avatar Apr 25 '23 13:04 vvagaytsev

Fixed in #5270

stefreak avatar Oct 20 '23 09:10 stefreak