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

tpl trick https://helm.sh/docs/howto/charts_tips_and_tricks/#using-the-tpl-function throws an error when used inside the range loop #12941

Closed
MilanMasek opened this issue Apr 11, 2024 · 2 comments

Comments

@MilanMasek
Copy link

MilanMasek commented Apr 11, 2024

Output of helm version:
version.BuildInfo{Version:"v3.14.3", GitCommit:"f03cc04caaa8f6d7c3e67cf918929150cf6f3f12", GitTreeState:"clean", GoVersion:"go1.21.7"}

Following example works. When the first line of template/service.yaml is moved inside the range loop, it throws an error.


$ cat values.yaml
mA: "my-awesome-app"
myApp:
appName: "{{ $.Values.mA }}-is-here"
replicas: 3
ports:
- 80
- 443
services:
- name: "web"
port: 80
- name: "api"
port: 8080

$ cat templates/service.yaml
app: {{ tpl $.Values.myApp.appName . }}
{{- range .Values.myApp.services }}
apiVersion: v1
kind: Service
metadata:
name: {{ .name }}-svc
spec:
ports:
- port: {{ .port }}
name: http
selector:
{{- end }}

`

When tpl is used like shown above anywhere inside the range loop, it throws an error:
 $ helm template . --values values.yaml Error: template: tt/templates/service.yaml:4:8: executing "tt/templates/service.yaml" at <tpl $.Values.myApp.appName .>: error calling tpl: error during tpl function execution for "{{ $.Values.mA }}-is-here": template: gotpl:1:4: executing "gotpl" at <$.Values.mA>: nil pointer evaluating interface {}.mA

@MilanMasek MilanMasek changed the title tpl trick https://helm.sh/docs/howto/charts_tips_and_tricks/#using-the-tpl-function throws an error when used inside the range function tpl trick https://helm.sh/docs/howto/charts_tips_and_tricks/#using-the-tpl-function throws an error when used inside the range loop Apr 12, 2024
@sabre1041
Copy link
Contributor

@MilanMasek When you move it inside the range, the scope changes. Since you want to stay at the top, or "root" scope, replace the . with a $ as shown below:

app: {{ tpl $.Values.myApp.appName $ }}

@MilanMasek
Copy link
Author

Understand - it works flawlessly! Thank you for educating me, @sabre1041!

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

2 participants