<p>I want to be able to take multiple sub-properties from a <code>values.yaml</code> property and put them all under a property in a template. For example:</p>
<p><strong>values.yaml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>selectors:
  propA: valueA
  propB: valueB
  propC: valueC
</code></pre>
<p><strong>/templates/deployment.yaml</strong></p>
<pre class="lang-yaml prettyprint-override"><code># ...elided...
spec:
  selector:
    matchLabels:
      # This is pseudo code
      {{ includeAll .Values.selectors | nindent 6 }}
</code></pre>
<p>And then this would result in K8s getting a yaml of:</p>
<pre class="lang-yaml prettyprint-override"><code># ...elided...
spec:
  selector:
    matchLabels:
      propA: valueA
      propB: valueB
      propC: valueC
</code></pre>
<p>How can I do something like that?</p>

Don Rhummy
<p>It's turns out not to be super simple. It depends (from what I can tell) on which type of thing is underneath.</p>
<p><strong>Simple name:value pairs</strong></p>
<p>You can use the <code>range</code> function which acts a lot like a for-each loop. The</p>
<pre class="lang-golang prettyprint-override"><code>{{- range $name, $labelValue := .Values.labels }}
{{ $name }}: {{ $labelValue }}
{{- end }}
</code></pre>
<p><strong>Nested properties of many levels</strong></p>
<p>If you have a nested, multi-level property, then the only way I could find is to convert it to a yaml. And you need to be very careful about the indenting. It does a couple things:</p>
<ol>
<li>It starts it with a new line (a full empty line)</li>
<li>Whatever indent you add will be in front of every line</li>
<li>If you start it indented in your template, that will be included too</li>
</ol>
<p>Example from <code>values.yaml</code></p>
<pre class="lang-yaml prettyprint-override"><code>envVariables:
  - name: MYSQL_ROOT_PASSWORD
    valueFrom: 
      secretKeyRef:
        name: db-creds
        key: db-pass
</code></pre>
<p><strong>First valid way:</strong></p>
<pre class="lang-yaml prettyprint-override"><code>spec:
  containers:
    - image: &quot;{{ .Values.image.name }}:{{ .Values.image.tag }}&quot;
      imagePullPolicy: {{ .Values.image.pullPolicy }}
      env: {{ toYaml .Values.envVariables | nindent 12 }}
</code></pre>
<p>This will result in:</p>
<pre class="lang-yaml prettyprint-override"><code>spec:
  containers:
    - image: &quot;{{ .Values.image.name }}:{{ .Values.image.tag }}&quot;
      imagePullPolicy: {{ .Values.image.pullPolicy }}
      env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom: 
            secretKeyRef:
              name: db-creds
              key: db-pass
</code></pre>
<p><strong>Second valid way:</strong></p>
<pre class="lang-yaml prettyprint-override"><code>spec:
  containers:
    - image: &quot;{{ .Values.image.name }}:{{ .Values.image.tag }}&quot;
      imagePullPolicy: {{ .Values.image.pullPolicy }}
      env: 
{{ toYaml .Values.envVariables | nindent 12 }}
</code></pre>
<p>Notice it's left aligned. This will result in a blank line after <code>env:</code> but will have the right indentation.</p>
<p><strong>INVALID WAY:</strong></p>
<pre class="lang-yaml prettyprint-override"><code>spec:
  containers:
    - image: &quot;{{ .Values.image.name }}:{{ .Values.image.tag }}&quot;
      imagePullPolicy: {{ .Values.image.pullPolicy }}
      env: 
        {{ toYaml .Values.envVariables | nindent 12 }}
</code></pre>
<p>With the above, there is <em><strong>no way</strong></em> to get the indenting correct as the first line only will have additional indentation and the other lines will not.</p>
