How to get values from values.yaml to _helpers.tpl in helm charts

跟風遠走 提交于 2019-12-11 09:48:13

问题


This is values.yaml file. It contains the following and when I am trying to get it into _helper.tpl im getting Helm template failed. Error: render error in "windows/templates/ingresses/windows.yaml": template: windows/templates/_helpers.tpl:38:18: executing "windows.certificate" at <.Values.ingress.enab...>: can't evaluate field ingress in type interface {} : exit status 1

values.yaml

ingress:
    enabled: true
    tls: true
    certificate: ''
    issuer:
        name: letsencrypt-staging
    hosts:
        windows:
            - name: ''
            path: /

_helpers.tpl

 {{/*
 Calculate certificate
 */}}
 {{- define "windows.certificate" }}
 {{- printf .Values.ingress.enabled }}  // error line is this. line no 38
 {{- end }}

in windows.yaml

    - secretName: {{ template "windows.certificate" . }} // calling the helper method.

回答1:


The problem is the indentation try this

values.yaml

ingress:
  enabled: true
  tls: true
  certificate: ''
  issuer:
    name: letsencrypt-staging
  hosts:
    windows:
      - name: ''
        path: /

Also some changes on the helpers to control the output of the define block

_helpers.tpl

 {{/*
 Calculate certificate
 */}}
 {{- define "windows.certificate" }}
 {{- if .Values.ingress.enabled }}
 {{- printf .Values.ingress.certificate }} 
 {{- end }}     
 {{- end }}



回答2:


It is possible that when you call the helper, the context is not the root as the definition expects.

Take for example, if you use it in a template like this:

{{- range .Values.deployments }}
  {{ $certificate := include "windows.certificate" . }}
{{- end }}

The context when calling the helper would be .Values.deployments. So, .Values.ingress.certificate would point to .Values.deployments.Values.ingress.certificate, which of course, does not exist.

At the start of the variables section of the helm templating guide, you have an example of how with blocks affect what . means. Reading it might help you understand how to be aware of what you pass to your helper template.




回答3:


for those having the same problem.
In my case, I had to rename my file from Values.yaml to values.yaml (mind the lowercase filename).



来源:https://stackoverflow.com/questions/56679428/how-to-get-values-from-values-yaml-to-helpers-tpl-in-helm-charts

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