Kubernetes 为什么通过helm的set文件从yaml文件读取的数据包含额外字符?

Kubernetes 为什么通过helm的set文件从yaml文件读取的数据包含额外字符?,kubernetes,yaml,kubernetes-helm,Kubernetes,Yaml,Kubernetes Helm,我有一张舵图里面的配置图: --- apiVersion: v1 kind: ConfigMap metadata: name: card-template data: card.tmpl: |- {{- if .Values.customMessageCardTemplate }} {{ toYaml .Values.customMessageCardTemplate | indent 4 }} {{- else }} {{ .Files.Get "card.tmpl" | indent

我有一张舵图里面的配置图:

---
apiVersion: v1
kind: ConfigMap
metadata:
 name: card-template
data:
  card.tmpl: |-
{{- if .Values.customMessageCardTemplate }}
{{ toYaml .Values.customMessageCardTemplate | indent 4 }}
{{- else }}
{{ .Files.Get "card.tmpl" | indent 4 }}
{{- end }}
此配置映射从
.Values.customMessageCardTemplate
值读取数据

我有一个文件
custom card.tmpl
,在安装图表期间,其内容应设置为
customMessageCardTemplate
的值

自定义卡.tmpl
中的数据为:

{{ define "teams.card" }}
{
  "@type": "MessageCard",
  "@context": "http://schema.org/extensions",
  "themeColor": "{{- if eq .Status "resolved" -}}2DC72D
                 {{- else if eq .Status "firing" -}}
                    {{- if eq .CommonLabels.severity "critical" -}}8C1A1A
                    {{- else if eq .CommonLabels.severity "warning" -}}FFA500
                    {{- else -}}808080{{- end -}}
                 {{- else -}}808080{{- end -}}",
  "summary": "{{- if eq .CommonAnnotations.summary "" -}}
                  {{- if eq .CommonAnnotations.message "" -}}
                    {{- .CommonLabels.alertname -}}-hai
                  {{- else -}}
                    {{- .CommonAnnotations.message -}}
                  {{- end -}}
              {{- else -}}
                  {{- .CommonAnnotations.summary -}}
              {{- end -}}",
  "title": "Prometheus Alert ({{ .Status }})",
  "sections": [ {{$externalUrl := .ExternalURL}}
  {{- range $index, $alert := .Alerts }}{{- if $index }},{{- end }}
    {
      "activityTitle": "[{{ $alert.Annotations.description }}]({{ $externalUrl }})",
      "facts": [
        {{- range $key, $value := $alert.Annotations }}
        {
          "name": "{{ reReplaceAll "_" " " $key }}",
          "value": "{{ reReplaceAll "_" " " $value }}"
        },
        {{- end -}}
        {{$c := counter}}{{ range $key, $value := $alert.Labels }}{{if call $c}},{{ end }}
        {
          "name": "{{ reReplaceAll "_" " " $key }}",
          "value": "{{ reReplaceAll "_" " " $value }}"
        }
        {{- end }}
      ],
      "markdown": true
    }
    {{- end }}
  ]
}
{{ end }}
使用
set file
标志运行安装命令时:

helm install --name my-rel --dry-run --debug --set-file customMessageCardTemplate=custom-card.tmpl ./my-chart
helm在从文件读取的数据中插入一些额外字符:

# Source: my-chart/templates/configMapTemplate.yaml
apiVersion: v1
kind: ConfigMap
metadata:
 name: card-template
data:
  card.tmpl: |-
    "{{ define \"teams.card\" }}\r\n{\r\n  \"@type\": \"MessageCard\",\r\n  \"@context\":
      \"http://schema.org/extensions\",\r\n  \"themeColor\": \"{{- if eq .Status \"resolved\"
      -}}2DC72D\r\n                 {{- else if eq .Status \"firing\" -}}\r\n                    {{-
      if eq .CommonLabels.severity \"critical\" -}}8C1A1A\r\n                    {{- else
      if eq .CommonLabels.severity \"warning\" -}}FFA500\r\n                    {{- else
      -}}808080{{- end -}}\r\n                 {{- else -}}808080{{- end -}}\",\r\n  \"summary\":
      \"{{- if eq .CommonAnnotations.summary \"\" -}}\r\n                  {{- if eq .CommonAnnotations.message
      \"\" -}}\r\n                    {{- .CommonLabels.alertname -}}-hai\r\n                  {{-
      else -}}\r\n                    {{- .CommonAnnotations.message -}}\r\n                  {{-
      end -}}\r\n              {{- else -}}\r\n                  {{- .CommonAnnotations.summary
      -}}\r\n              {{- end -}}\",\r\n  \"title\": \"Prometheus Alert ({{ .Status
      }})\",\r\n  \"sections\": [ {{$externalUrl := .ExternalURL}}\r\n  {{- range $index,
      $alert := .Alerts }}{{- if $index }},{{- end }}\r\n    {\r\n      \"activityTitle\":
      \"[{{ $alert.Annotations.description }}]({{ $externalUrl }})\",\r\n      \"facts\":
      [\r\n        {{- range $key, $value := $alert.Annotations }}\r\n        {\r\n          \"name\":
      \"{{ reReplaceAll \"_\" \" \" $key }}\",\r\n          \"value\": \"{{ reReplaceAll
      \"_\" \" \" $value }}\"\r\n        },\r\n        {{- end -}}\r\n        {{$c :=
      counter}}{{ range $key, $value := $alert.Labels }}{{if call $c}},{{ end }}\r\n        {\r\n
      \         \"name\": \"{{ reReplaceAll \"_\" \" \" $key }}\",\r\n          \"value\":
      \"{{ reReplaceAll \"_\" \" \" $value }}\"\r\n        }\r\n        {{- end }}\r\n
      \     ],\r\n      \"markdown\": true\r\n    }\r\n    {{- end }}\r\n  ]\r\n}\r\n{{
      end }}\r\n"
为什么会发生这种情况?当我使用base-64对原始数据和读取数据进行编码时,两者似乎都不同

如何解决这个问题

注意:

我无法使用extraValues.yaml将数据设置为:

customMessageCardTemplate:
  {{ define "teams.card" }}
  {
    .
    .
    .
  }
  {{ end }}
它给出了一个错误:

Error: failed to parse extraValues.yaml: error converting YAML to JSON: yaml: line 2: did not find expected key
但如果值文件如下所示,则不会出现此错误:

customMessageCardTemplate:
  card.tmpl: |-
    {{ define "teams.card" }}
    {
      .
      .
    }
    {{ end }}

它只是按照你说的做
customMessageCardTemplate
包含一个字符串,因此
toYaml
将其编码为双引号YAML字符串。这样做时,它会用转义序列替换特殊字符,如行尾和双引号


由于要粘贴到块标量中,因此不需要转义。只要放下
toYaml
,你就应该没事了。

它只会按照你说的做
customMessageCardTemplate
包含一个字符串,因此
toYaml
将其编码为双引号YAML字符串。这样做时,它会用转义序列替换特殊字符,如行尾和双引号

由于要粘贴到块标量中,因此不需要转义。只要放下
toYaml
,你就会没事了