<p>As I know the way to create a configMap in Kubernetes from a file is to use:<br />
<code>--from-file</code> option for <code>kubectl</code></p>
<p>What I am looking for is a way to only load part of the yaml file into the configMap.<br />
Example:
Let's say I have this yml file:</p>
<pre><code>family:
  Boys:
    - name: Joe
    - name: Bob
    - name: dan
  Girls:
    - name: Alice
    - name: Jane  
</code></pre>
<p>Now I want to create a configMap called 'boys' which will include only the 'Boys' section.<br />
Possible?</p>
<p>Another thing that could help if the above is not possible is when I am exporting the configMap as environment variables to a pod (using <code>envFrom</code>) to be able to only export part of the configMap.<br />
Both options will work for me.</p>
<p>Any idea?</p>

<p>Configmaps cannot contain rich yaml data. Only key value pairs. So if you want to have a list of things, you need to express this as a multiline string.</p>
<p>With that in mind you could use certain tools, such a yq to query your input file and select the part you want.</p>
<p>For example:</p>
<pre class="lang-sh prettyprint-override"><code>podman run -rm --interactive bluebrown/tpl '{{ .family.Boys | toYaml }}' &lt; fam.yaml \
  | kubectl create configmap boys --from-file=Boys=/dev/stdin
</code></pre>
<p>The result looks like this</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: boys
  namespace: sandbox
data:
  Boys: |+
    - name: Joe
    - name: Bob
    - name: dan
</code></pre>
<p>You could also encode the file or part of the file with base64 and use that as an environment variable, since you get a single string, which is easily processable, out of it. For example:</p>
<pre class="lang-sh prettyprint-override"><code>$ podman run --rm --interactive bluebrown/tpl \
  '{{ .family.Boys | toYaml | b64enc }}' &lt; fam.yaml

# use this string as env variable and decode it in your app
LSBuYW1lOiBKb2UKLSBuYW1lOiBCb2IKLSBuYW1lOiBkYW4K
</code></pre>
<p>Or with set env which you could further combine with dry run if required.</p>
<pre class="lang-sh prettyprint-override"><code>podman run --rm --interactive bluebrown/tpl \
 'YAML_BOYS={{ .family.Boys | toYaml | b64enc }}' &lt; fam.yaml \
  | kubectl set env -e - deploy/myapp
</code></pre>
<p>Another thing is, that YAML is a superset of JSON, in many cases you are able to convert YAML to JSON or at least use JSON like syntax.<br />
This can be useful in such a scenario in order to express this as a single line string rather than having to use multiline syntax. It's less fragile.<br />
Every YAML parser will be able to parse JSON just fine. So if you are parsing the string in your app, you won't have problems.</p>
<pre class="lang-sh prettyprint-override"><code>$ podman run --rm --interactive bluebrown/tpl '{{ .family.Boys | toJson }}' &lt; fam.yaml
[{&quot;name&quot;:&quot;Joe&quot;},{&quot;name&quot;:&quot;Bob&quot;},{&quot;name&quot;:&quot;dan&quot;}]
</code></pre>
<p>Disclaimer, I created the above used tool <a href="https://github.com/bluebrown/tpl" rel="nofollow noreferrer">tpl</a>. As mentioned, you might as well use alternative tools such as <a href="http://mikefarah.github.io/yq/" rel="nofollow noreferrer">yq</a>.</p>
