<p>Is there a way to flatten a dictionary with helm? I want to provide environment variables to the application from the chart by flattening a YAML config located in values.yaml. The config can look like. (Not Actual)</p>

<pre class="lang-yaml prettyprint-override"><code>config:
 server:
  port: 3333
 other:
  setting:
    name: test
</code></pre>

<p>And would like to provide environment variables as</p>

<pre class="lang-yaml prettyprint-override"><code>- name: CONFIG_SERVER_PORT
  value: 3333
- name: CONFIG_OTHER_SETTING_NAME
  value: test
</code></pre>

<p>I have considered using Kubernetes config maps but this would mean deploying slightly different instances of the app with random release names so that the config is not overwritten. 
This library <a href="https://github.com/jeremywohl/flatten" rel="nofollow noreferrer">https://github.com/jeremywohl/flatten</a> provides a way to flatten a <code>map[string]interface{}</code> with delimeters. Is there a way to provide a custom pipe for helm that uses the library or another way to flatten the config?</p>

user3533087
<p>I'm not aware of anything like that built in. <a href="https://masterminds.github.io/sprig/" rel="nofollow noreferrer">Sprig</a> provides most of the useful functions for helm templates but the <a href="https://masterminds.github.io/sprig/dicts.html" rel="nofollow noreferrer">dict functions</a> just cover the primitives.</p>
<p>You could <code>define</code> a <a href="https://helm.sh/docs/chart_template_guide/named_templates/" rel="nofollow noreferrer">named template</a> that does the business and recurses down the config dict/map. Then <code>include</code> the template where needed:</p>
<pre><code>{{- define &quot;recurseFlattenMap&quot; -}}
{{- $map := first . -}}
{{- $label := last . -}}
{{- range $key, $val := $map -}}
  {{- $sublabel := list $label $key | join &quot;_&quot; | upper -}}
  {{- if kindOf $val | eq &quot;map&quot; -}}
    {{- list $val $sublabel | include &quot;recurseFlattenMap&quot; -}}
  {{- else -}}
- name: {{ $sublabel | quote }}
  value: {{ $val | quote }}
{{ end -}}
{{- end -}}
{{- end -}}
</code></pre>
<p>Passing the <code>config</code> data in is a little complex here, via a <code>list</code> that is then separated back out into <code>$map</code> and <code>$label</code>. This is due to templates only accepting a single variable <a href="https://helm.sh/docs/chart_template_guide/named_templates/#setting-the-scope-of-a-template" rel="nofollow noreferrer">scope</a>.</p>
<pre><code>env: {{ list .Values.config &quot;CONFIG&quot; | include &quot;recurseFlattenMap&quot; | nindent 2 }}   
</code></pre>
<p>With the example values:</p>
<pre><code>config:
  server:
    port: 3333
  first: astr
  other:
    setting:
      name: test
</code></pre>
<p>Results in</p>
<pre><code>$ helm template . 
---
# Source: so61280873/templates/config.yaml
env: 
  - name: &quot;CONFIG_FIRST&quot; 
    value: &quot;astr&quot;
  - name: &quot;CONFIG_OTHER_SETTING_NAME&quot; 
    value: &quot;test&quot;
  - name: &quot;CONFIG_SERVER_PORT&quot; 
    value: &quot;3333&quot;
</code></pre>
