For the most part, when mapping a file into a container using Helm, the standard approach is to use a ConfigMap, Volume, and VolumeMount. For a single file, this approach works perfectly fine, but what if the requirement is to map a directory of files into a container? What if those filenames do not conform to the YAML key naming standards?

Probably the main reason to use Helm is to dynamically build Kubernetes objects based on an ever-changing set of variables. The ability to dynamically generate a ConfigMap every time you run a Helm upgrade is powerful, but when combined with volume mounts for file mapping, specifically where there are many files that are changing with each deployment or filename that are not supported by YAML, this poses some cumbersome challenges. This post will try and address some of these issues and poses an elegant solution for mapping in a directory of files dynamically.

The simple example — one file

Suppose we wish to map a file into a container at a specific path. The simple way to go about this is as follows:

Step 1:

Create a ConfigMap to map a file called_ config.yaml l_ike so:

apiVersion: v1
kind: ConfigMap
  name: cm-single-file
{{ range $path, $bytes := .Files.Glob (printf "/path/to/configfile/config.yaml")}}
{{ $name := base $path }}
{{- printf "%s" $name}}{{ print ": "}}{{ $.Files.Get $path | b64enc }}
{{ end }}


Create a VolumeMount and Volume section in one of a Job, StatefulSet, DaemonSet, Deployment, etc. Map the Volume to the ConfigMap and the VolumeMounts to the Volume like so:

  - name: cm-single-file-volume
      name: cm-single-file
  - name: cm-single-file-volume
    mountPath: /location/to/mount/config.yaml
    subPath: config.yaml

The example above takes a file called _config.yaml, _and maps it not a container via a ConfigMap. Things are not always this simple, however.

#kubernetes #helm #helm-3

Helm 3 — Mapping a directory of files into a container
41.50 GEEK