<p>I have a configMap inside a helm chart:</p>

<pre><code>---
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 }}
</code></pre>

<p>This configMap reads data from <code>.Values.customMessageCardTemplate</code> value.</p>

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

<p>The data inside <code>custom-card.tmpl</code> is :</p>

<pre><code>{{ 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 }}
</code></pre>

<p>When running the install command with <code>set-file</code> flag:</p>

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

<p>helm inserts some extra characters into the data it reads from the file:</p>

<pre><code># 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"
</code></pre>

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

<p>How to solve this issue?</p>

<p><strong>Note:</strong></p>

<p>I cannot set the data using an extraValues.yaml as:</p>

<pre><code>customMessageCardTemplate:
  {{ define "teams.card" }}
  {
    .
    .
    .
  }
  {{ end }}
</code></pre>

<p>It gives an error:</p>

<pre><code>Error: failed to parse extraValues.yaml: error converting YAML to JSON: yaml: line 2: did not find expected key
</code></pre>

<p>But this error doesn't appear if the values file is like:</p>

<pre><code>customMessageCardTemplate:
  card.tmpl: |-
    {{ define "teams.card" }}
    {
      .
      .
    }
    {{ end }}
</code></pre>

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

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