ingress.yaml template returns error in renderring --> nil pointer evaluating interface {}.service

五迷三道 提交于 2020-08-06 07:58:26

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/57475521/ingress-yaml-template-returns-error-in-renderring-nil-pointer-evaluating-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!