如何在 helm 图表中从 value.yaml 获取值到 _helpers.tpl

m9m*_*m9m 7 yaml kubernetes-helm

这是values.yaml 文件。它包含以下内容,当我尝试将其放入 _helper.tpl 时,我得到了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

值.yaml

ingress:
    enabled: true
    tls: true
    certificate: ''
    issuer:
        name: letsencrypt-staging
    hosts:
        windows:
            - name: ''
            path: /
Run Code Online (Sandbox Code Playgroud)

_helpers.tpl

 {{/*
 Calculate certificate
 */}}
 {{- define "windows.certificate" }}
 {{- printf .Values.ingress.enabled }}  // error line is this. line no 38
 {{- end }}
Run Code Online (Sandbox Code Playgroud)

在 windows.yaml 中

    - secretName: {{ template "windows.certificate" . }} // calling the helper method.
Run Code Online (Sandbox Code Playgroud)

Áng*_*ela 5

当您调用帮助器时,上下文可能不是定义所期望的根。

举个例子,如果你在这样的模板中使用它:

{{- range .Values.deployments }}
  {{ $certificate := include "windows.certificate" . }}
{{- end }}
Run Code Online (Sandbox Code Playgroud)

调用帮助程序时的上下文是.Values.deployments。因此,.Values.ingress.certificate将指向.Values.deployments.Values.ingress.certificate,这当然不存在。

在helm 模板指南的变量部分的开头,您有一个示例,说明with块如何影响.含义。阅读它可能会帮助您了解如何了解传递给帮助程序模板的内容。


wol*_*lmi 2

问题是缩进试试这个

值.yaml

ingress:
  enabled: true
  tls: true
  certificate: ''
  issuer:
    name: letsencrypt-staging
  hosts:
    windows:
      - name: ''
        path: /
Run Code Online (Sandbox Code Playgroud)

还对助手进行了一些更改以控制定义块的输出

_helpers.tpl

 {{/*
 Calculate certificate
 */}}
 {{- define "windows.certificate" }}
 {{- if .Values.ingress.enabled }}
 {{- printf .Values.ingress.certificate }} 
 {{- end }}     
 {{- end }}
Run Code Online (Sandbox Code Playgroud)