37

I am installing a helm chart which has a ingress.yaml template.

I get this error:

Error: render error in "chartmuseum/templates/ingress.yaml": template: chartmuseum/templates/ingress.yaml:35:22: executing "chartmuseum/templates/ingress.yaml" at <.Values.service.servicename>: nil pointer evaluating interface {}.service

I am not able to find where the problem is. The same set of if else structure works abolutely fine in the service.yaml of the same helm chart.

- path: {{ default "/" .path | quote }}
        backend:
        {{- if .Values.service.servicename }}
          serviceName: {{ .Values.service.servicename }}
        {{- else }}
          serviceName: {{ include "chartmuseum.fullname" . }}
        {{- end }}

Getting error on this line --> serviceName: {{ .Values.service.servicename }}

The code that works in service.yaml fine is

metadata:
{{- if .Values.service.servicename }}
  name: {{ .Values.service.servicename }}
{{- else }}
  name: {{ include "chartmuseum.fullname" . }}
{{- end }}

Expected result: if there is a servcice.servicename in values in values.yaml file , the ingress should pick the value from there for the key serviceName. Else it should include "chartmuseum.fullname".

The same structure works fine for service.yaml.

Below is the url of the original helm chart that i am using.

https://github.com/helm/charts/tree/master/stable/chartmuseum

I just modified the ingress.yaml to add if else block around line 31. Ingress.yaml https://github.com/helm/charts/blob/master/stable/chartmuseum/templates/ingress.yaml

Values.yaml file is insignificant. I have the below values in it

service:
  servicename: helm-charts-test

but even without this value, the if else block is expected to work.

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
RAMNEEK GUPTA
  • 713
  • 1
  • 6
  • 13

2 Answers2

78

What you're seeing is a weird caveat in Go templating. Your conditional logic is being evaluated inside a range loop. This means . you're using to access Values is not the one you expect it to be, as it's overridden for each range iteration evaluation. You can use $, which references the global scope in order to access the Values as expected.

For your scenario, it would be something like:

- path: {{ default "/" .path | quote }}
        backend:
        {{- if $.Values.service.servicename }}
          serviceName: {{ $.Values.service.servicename }}
        {{- else }}
          serviceName: {{ include "chartmuseum.fullname" $ }}
        {{- end }}

See here for more details.

Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38
Yaniv Oliver
  • 3,372
  • 1
  • 19
  • 20
  • Thanks for the answer. It solves the error in concern. But now it gives an error on the else part. Error: render error in "chartmuseum/templates/ingress.yaml": template: chartmuseum/templates/ingress.yaml:38:26: executing "chartmuseum/templates/ingress.yaml" at : error calling include: template: chartmuseum/templates/_helpers.tpl:50:37: executing "chartmuseum.fullname" at <.Values.global>: nil pointer evaluating interface {}.global – RAMNEEK GUPTA Aug 13 '19 at 14:11
  • seems like cant use {{ include "chartmuseum.fullname" . }} inside the range. Any workaround ? – RAMNEEK GUPTA Aug 13 '19 at 14:18
  • how do i use $ with include {{ include "chartmuseum.fullname" . }} ? Although i have managed to run it fine by replacing the else part with {{ default $serviceName .service }} and defining the variable $servicename at the beginning {{- $serviceName := include "chartmuseum.fullname" . -}} – RAMNEEK GUPTA Aug 13 '19 at 21:15
  • What you did is definitely an option here. You could also save the global context out of the control structure in order to make your solution more generic. See [here](https://github.com/helm/helm/issues/1311#issuecomment-252536380). – Yaniv Oliver Aug 14 '19 at 07:23
  • I've also updated the answer to be complete the example. – Yaniv Oliver Aug 14 '19 at 10:31
  • @yanivoliver thank you so much!!!!!! you made my day!!!! תודה רבה!!!! – Michael A. Aug 21 '20 at 20:14
  • May the invisible space wizard bless your imaginary imortal being. – Cristiano Fontes Mar 08 '21 at 11:41
  • the "see here for more details" like is broken - this one should help. https://helm.sh/docs/chart_template_guide/variables/ – Andrew Lohr Mar 07 '22 at 20:17
11

I followed this answer by @Torrey and replaced

targetPort: {{ .Values.non_existing.port | default 1234 }}

with

targetPort: {{ (.Values.non_existing).port | default 1234 }}

and it worked

krzk
  • 363
  • 5
  • 14