【问题标题】:Helm: It is possible to use nindent function with template function?Helm:可以将 nindent 函数与模板函数一起使用吗?
【发布时间】:2023-02-04 11:08:44
【问题描述】:

我对 helm 语法有点陌生,我正在尝试在我的部署文件中包含一个模板。在我的一些部署中,缩进必须不同。有没有办法包含我的模板并使用缩进功能?

{{ template "my-chart.nodeaffinity" include "my-chart.fullname" . | toYaml | nindent 8 }}

可以使用模板到 Yaml在同一语句中运行?

谢谢!

我尝试添加以下代码,但无法正常工作,当我使用 helm 模板时,nindent 函数无法正常工作。

| toYaml | nindent 8 

【问题讨论】:

    标签: yaml kubernetes-helm indentation


    【解决方案1】:

    您不能像您描述的那样真正一起使用 templatetoYaml 。您可以做的是使用类似但特定于 Helm 的 include 函数代替 template

    {{ include "my-chart.nodeaffinity" (include "my-chart.fullname" .) | toYaml | nindent 8 }}
    {{/*^^^^^^ not template            ^---    need parens here   ---^                    */}}
    

    template 是内置的 Go text/template 动作。它在其块的其余部分使用管道,并且始终将其输出发送到呈现的模板输出。所以{{ template "name" "arg" | toYaml }}读作"arg" | toYaml范围到模板,并且没有办法处理它的输出。

    include 是模板语法中的扩展函数。它以与template 相同的方式呈现模板,但它返回呈现的字符串,然后可以参与管道。所以 {{ include "name" "arg" | toYaml }} 使用指定的参数调用模板,然后处理返回值。

    一个推论是include总是返回一个字符串.即使看起来帮助程序模板正在生成 YAML 对象,它实际上也是一个字符串。这可能意味着您不希望 toYaml 在这里,因为它可能会导致您不需要的额外转义。

    您可以在 default chart scaffolding 中看到更具体的示例。一组标准标签被定义为模板

    {{- define "<CHARTNAME>.labels" -}}
    helm.sh/chart: {{ include "<CHARTNAME>.chart" . }}
    ...
    {{- end }}
    

    使用它时,它是通过include(而不是template)使用的,它的输出是(nindented

    metadata:
      labels:
        {{- include "<CHARTNAME>.labels" . | nindent 4 }}
    

    【讨论】:

      猜你喜欢
      • 2012-05-24
      • 2020-08-09
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      相关资源
      最近更新 更多