Why does data read from a yaml file via set-file of helm contains extra characters?

11/15/2019

I have a configMap inside a helm chart:

---
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 }}

This configMap reads data from .Values.customMessageCardTemplate value.

I have a file custom-card.tmpl whose content should be set as the value of customMessageCardTemplate during the installation of the chart.

The data inside custom-card.tmpl is :

{{ 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 }}

When running the install command with set-file flag:

helm install --name my-rel --dry-run --debug --set-file customMessageCardTemplate=custom-card.tmpl ./my-chart

helm inserts some extra characters into the data it reads from the file:

# 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"

Why does this happen? When I encode the original data and the read data using base-64, both seem different.

How to solve this issue?

Note:

I cannot set the data using an extraValues.yaml as:

customMessageCardTemplate:
  {{ define "teams.card" }}
  {
    .
    .
    .
  }
  {{ end }}

It gives an error:

Error: failed to parse extraValues.yaml: error converting YAML to JSON: yaml: line 2: did not find expected key

But this error doesn't appear if the values file is like:

customMessageCardTemplate:
  card.tmpl: |-
    {{ define "teams.card" }}
    {
      .
      .
    }
    {{ end }}
-- AnjanaDyna
kubernetes
kubernetes-helm
yaml

1 Answer

11/15/2019

It just does exactly what you tell it to. customMessageCardTemplate contains a string, so toYaml encodes it as double-quoted YAML string. While doing so, it replaces special characters such as line endings and double quotes with escape sequences.

Since you're pasting into a block scalar, you don't need the escaping. Just drop the toYaml and you should be fine.

-- flyx
Source: StackOverflow