多行字符串到掌舵模板中的变量?

multiline string to a variable in a helm template?

是否可以将多行字符串分配给 helm 模板中的变量?

我有一个变量如下:

{{- $fullDescription := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -}}

但我更愿意将其保留在我的代码库中,如

{{- $fullDescription :|- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                          xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                          xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -}}

.. 但这不是有效的 yaml。

这可以做到吗?

我认为它应该是这样工作的:

$fullDescription: | +
   "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

我通过从一个单独的文件中包含我需要的内容解决了这个问题。

例如。

  fullDescription: |-
{{ .Files.Get files/description.html | indent 4 }}

您可以使用 values.yaml,这样您就无需添加额外的文件。

fullDescription: |+
  line1
  line2

然后在模板中

fullDescription: |
{{ $fullDescription | indent 2}}

可能不是你想要的,但你可以使用换行符

{{- $fullDescription := "a\nb\nc\n" -}}

输入

values.yaml

myFile: |
  This is a multiline
  value, aka heredoc.

myArray:
  - key1=value1
  - key2=value2

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: myTemplate
data:
  myFile: {{- .Values.myFile | toYaml | indent 1 }}
  myArray: |
  {{- range $k, $v := .Values.myArray }}
    - {{ . | toYaml | indent 4 | trim }}
  {{- end }}

输出

apiVersion: v1
kind: ConfigMap
metadata:
  name: myTemplate
data:
  myFile: |
   This is a multiline
   value, aka heredoc.
  myArray: |
    - key1=value1
    - key2=value2