【问题标题】:How to get values from values.yaml to _helpers.tpl in helm charts如何在掌舵图中从 values.yaml 到 _helpers.tpl 获取值
【发布时间】:2019-06-20 05:22:03
【问题描述】:

这是 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

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 }}

在 windows.yaml 中

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

【问题讨论】:

    标签: yaml kubernetes-helm


    【解决方案1】:

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

    例如,如果您在这样的模板中使用它:

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

    调用助手时的上下文是.Values.deployments。因此,.Values.ingress.certificate 将指向 .Values.deployments.Values.ingress.certificate,当然,它并不存在。

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

    【讨论】:

    • 我应该在哪里添加这个代码?在 _helpers.tpl 或我调用这个辅助方法的地方。我修改了我的问题并添加了我如何调用辅助方法。 {{ 模板“windows.certificate”。 }}
    • 我们需要查看调用辅助方法之前的代码。您需要检查是否有任何 {{range... }}{{with...}} 可能改变了上下文
    【解决方案2】:

    对于那些有同样问题的人。
    就我而言,我必须将文件从 Values.yaml 重命名为 values.yaml(注意小写文件名)。

    【讨论】:

      【解决方案3】:

      问题是缩进试试这个

      values.yaml

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

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

      _helpers.tpl

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

      【讨论】:

      • 是的.. !!我检查了它,但错误仍然相同。我也编辑了我的代码
      • 现在检查,我已经编辑了答案以在帮助程序上添加一些更改
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 2018-08-14
      • 1970-01-01
      • 2022-10-14
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多