我在头盔图表中有一个configMap:
---
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 }}
此configMap从.Values.customMessageCardTemplate
值中读取数据。
我有一个文件custom-card.tmpl
,其内容应在图表安装期间设置为customMessageCardTemplate
的值。
custom-card.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 }}
答案 0 :(得分:2)
它完全按照您的指示进行。 customMessageCardTemplate
包含一个字符串,因此toYaml
将其编码为双引号的YAML字符串。这样做时,它会用转义序列替换特殊字符,例如行尾和双引号。
由于要粘贴到块标量中,因此不需要转义。只需放下toYaml
,就可以了。