kluctl icon indicating copy to clipboard operation
kluctl copied to clipboard

get_var is not working in loops

Open aljoshare opened this issue 1 year ago • 3 comments
trafficstars

Operating system

macOS

Kluctl Version

2.23.3

Kubernetes Version

v1.26.12

kubectl Version

v1.29.1

Bug description

The function get_var is not working in a local context (e.g. for loops). For example, the following statement would always return false because get_var can't access the local context which contains the entry object:

{% for entry in list %}
 {{ get_var('entry.enabled', 'false') }}
{% endfor %}

I would expect that if entry.enabled is set to true, get_var also returns true instead of the default value false.

Steps to reproduce

No response

Relevant log output

No response

aljoshare avatar Feb 06 '24 15:02 aljoshare

I guess I run into the same issue with the following (simplified) piece of code:

{% for server in list %}
  {{ "{{ server.name }}"|render }}
{% endfor %}

Leads to this error:

UndefinedError: 'server' is undefined

According to the render doc:

Renders the input string with the current Jinja2 context.

I might be missing something about the current jinja2 context, but I was expected my server variable to be available in it.

My kluctl version is 2.23.4.

fgaillot-qosenergy avatar Feb 29 '24 08:02 fgaillot-qosenergy

I can confirm that this is a bug and supposed to work, so the docs are correct, but the implementation is wrong. The get_var and render function is currently only considering the globals from the current Jinja2 environment, while it should also include local variables.

For now, at least in regard to get_var, you can workaround this by iterating through indexes and using get_var('list[%d].enabled' % i).

Regarding render, this will be harder atm. I assume involving some global variables will be required.

codablock avatar Feb 29 '24 08:02 codablock

This issue seems to be more complicated then I thought. It's a combination of missing functionality in Jinja2 and bugs in go-jinja2. See this PR, which tries to fix it partially: https://github.com/kluctl/kluctl/pull/997

I'm not going to close this issue, at least until https://github.com/pallets/jinja/issues/1478 is fixed.

codablock avatar Mar 04 '24 10:03 codablock