掌舵模板:如何将模板结果分配给变量

时间:2018-09-20 03:06:40

标签: kubernetes-helm go-templates

我正在尝试执行以下操作:

{{- $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,所以我试图将值放入变量中并在范围内使用它。因此,如果有解决方案,那么它也将被接受!

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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

{{- 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 {}'
相关问题