【问题标题】:helm template: how do i assign the result of a template to a variablehelm 模板:如何将模板的结果分配给变量
【发布时间】:2020-01-28 22:50:33
【问题描述】:

我正在尝试做类似的事情:

{{- $cassandrafullname := template "cassandra.fullname" . -}}

但我在试运行时遇到此错误:

Error: UPGRADE FAILED: parse error in "cassandra/templates/service.yaml": template: cassandra/templates/service.yaml:1: unexpected <template> in command

我遇到这个问题的原因是因为我无法在一个范围内使用template cassandra.fullname,所以我试图将值放入一个变量并在范围内使用它。因此,如果有解决方案,它也将被接受!

【问题讨论】:

    标签: kubernetes-helm go-templates


    【解决方案1】:

    Helm defines an include function 与标准 template 相同,只是它返回渲染的输出而不是输出它。你应该会写

    {{- $cassandrafullname := include "cassandra.fullname" . -}}
    

    【讨论】:

    • 非常感谢这个建议。
    【解决方案2】:

    不幸的是,这不适用于fromYaml,因此您不能像往常一样将 yaml 结构读入管道操作。一个比较大的缺点。很多时候我需要将一个列表过滤到另一个列表中,但这对于 helm 来说似乎是不可能的:

    {{- define "sometpl" -}}
    - bla: dibla
    - oki: doki
    {{- end -}}
    ---
    {{- $v := include "sometpl" . | fromYaml }}
    some: {{- $v | toYaml | nindent 2 }}
    

    会给

    some:
      Error: 'error unmarshaling JSON: while decoding JSON: json: cannot unmarshal array
        into Go value of type map[string]interface {}'
    

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2020-04-07
      • 2020-05-16
      • 2013-12-24
      • 2018-01-13
      相关资源
      最近更新 更多