helm 范围内的多行文件

问题描述 投票:0回答:2

我正在尝试创建一个包含多个文件的秘密。

我的value.yaml(多行的格式不是yaml或json)

secretFiles:
  - name: helm-template-file
    subPath: ".file1"
    mode: "0644"
    value: |
      This is a multiline
      value, aka heredoc.

那么我的秘密文件模板是secret.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: {{ include "helm-template.fullname" . }}-file
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ include "helm-template.name" . }}
    chart: {{ include "helm-template.chart" . }}
type: Opaque
stringData: 
  {{- range .Values.secretFiles }}

  {{ .subPath}}: |
    {{  .value  | indent 4}}
  {{- end }}

helm 安装出现错误“将 YAML 转换为 JSON 时出错:yaml:第 12 行:未找到预期的注释或换行符”。我该如何修复它?谢谢你。

kubernetes-helm
2个回答
1
投票

values.yaml

secretFiles:
  - name: helm-template-file
    subPath: ".file1"
    mode: "0644"
    value: |
      This is a multiline
      value, aka heredoc.
  - name: helm-template-file2
    subPath: ".file2"
    mode: "0644"
    value: |
      This is a multiline222
      value, aka heredoc.

xxx_tpl.yaml

...
stringData: 
  {{- range .Values.secretFiles }}
  {{ .subPath}}: |
    {{-  .value  | nindent 4}}
  {{- end }}

输出

...
stringData:
  .file1: |
    This is a multiline
    value, aka heredoc.
    
  .file2: |
    This is a multiline222
    value, aka heredoc.

参考:

宁缺
nindent 函数与 indent 函数相同,但是 在字符串的开头添加一个新行。

缩进
indent 函数将给定字符串中的每一行缩进到指定的缩进宽度


0
投票

当您

indent
一行时,您需要确保该行中的标记从第一列开始。

stringData: 
  {{- range .Values.secretFiles }}
  {{ .subPath}}: |
{{  .value  | indent 4}}{{/* no space at start of this line */}}
  {{- end }}

在原始问题的形式中,行开头的四个空格被复制到输出中,然后

indent
表达式在每行的开头添加四个空格。这会导致第一行缩进 8 个空格,第二行缩进 4 个空格,并且这种不一致会导致您看到的 YAML 解析错误。

如果您在图表上运行

helm template --debug
,您也可以直观地看到这一点;您会看到相同的错误,而且还会看到无法解析的模板输出。

(@z.x 的回答以不同的方式解决了同样的问题:将

-
放在大括号内会删除前导空格和前面的换行符,然后将
indent
更改为
nindent
放置换行符回来。)

© www.soinside.com 2019 - 2024. All rights reserved.