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

6/20/2019

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.
-- m9m9m
kubernetes-helm
yaml

3 Answers

11/22/2019

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).

-- Bob Claerhout
Source: StackOverflow

6/20/2019

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.

-- Ángela
Source: StackOverflow

6/20/2019

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 }}
-- wolmi
Source: StackOverflow