Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_var is not working in loops #972

Open
aljoshare opened this issue Feb 6, 2024 · 3 comments
Open

get_var is not working in loops #972

aljoshare opened this issue Feb 6, 2024 · 3 comments

Comments

@aljoshare
Copy link
Contributor

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

@fgaillot-qosenergy
Copy link

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.

@codablock
Copy link
Collaborator

codablock commented Feb 29, 2024

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
Copy link
Collaborator

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: #997

I'm not going to close this issue, at least until pallets/jinja#1478 is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants