在 yaml 文件中包含 helm 模板,删除引号

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

这是我的头盔图表

helpers.tpl
的片段:

{{/*
Pod-specific labels - added to pod template only
Adding a revision label to the pod will cause it to restart every time the chart is deployed.
*/}}
{{- define "app.podLabels" -}}
helm-revision: {{ .Release.Revision | quote }}
{{- end }}

将其包含在 pod 标签中,如下所示:

  labels:
    {{- include "app.podLabels" . | nindent 8 }}

结果如下图所示。

1
周围的引号是必需的,因为 Kubernetes 仅接受字符串标签。

  labels:
    helm-revision: "1"

我需要对 init 容器使用相同的模板,将

:
替换为
=
,如下所示:

  args:
    - "pod"
    - "-l {{ include "app.podLabels" . | replace ": " "=" }}"

但是输出将是一个不正确的 yaml:

  args:
    - "pod"
    - "-l helm-revision="1""

有错误:

error converting YAML to JSON: yaml: line 34: did not find expected '-' indicator

我真正想要的是这样的东西,不包含围绕

1
的引号:

  args:
    - "pod"
    - "-l helm-revision=1"

我怎样才能实现这个目标?

kubernetes kubernetes-helm podspec
1个回答
0
投票

这可以通过将

"
替换为任何内容来实现(实际上,删除
"
)。需要使用
\
字符转义引号。

还添加了

replace
函数来删除换行符并添加
, 
作为标签之间的分隔符。如果存在多个 pod 特定标签,这将很有帮助。但请确保在渲染模板时这适用于您的情况。

      args:
        - "pod"
        - "-l {{ include "app.podLabels" . | replace ": " "=" | replace "\n" ", " | replace "\"" "" }}"
© www.soinside.com 2019 - 2024. All rights reserved.