ConfigMap Recursive directory issues. https://stackoverflow.com/questions/48937323/automatic-subdirectories-in-kubernetes-configmaps
Using a Helm template
tree -L 2 helm_templates/your-template
helm_templates/your-template
├── Chart.yaml
├── templates
│ ├── NOTES.txt
│ ├── _helpers.tpl
│ ├── configmap.yaml <--- ConfigMap
│ ├── deployment.yaml <--- Deployment
└── values.yaml
Load recursive directories from config_dir
def _get_config_maps(self) -> dict:
config_maps = {}
filenames = [y for x in os.walk(self.config_dir) for y in glob.glob(os.path.join(x[0], '*')) if os.path.isfile(y)]
for filename in filenames:
# remove parent dir path
relative_path = filename.replace(self.config_dir + os.path.sep, "")
# replace path separator by "__" to prevent Kubernetes syntax error
# currently file path doesn't validate '[-._a-zA-Z0-9]+'
key = relative_path.replace(os.path.sep, "__")
with open(filename, 'r') as file:
config_maps[key] = {
"path": relative_path,
"content": file.read()
}
return config_maps
Save to values.yaml
# load value template
with open('helm_templates/your-template/values.yaml', 'r') as file:
deploy_values = yaml.safe_load(file)
deploy_values['configMaps'] = self._get_config_maps()
ConfigMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "your-template.fullname" . }}-config
data:
{{- range $key, $value := .Values.configMaps }}
{{ $key }}: |-
{{ $value.content | indent 4 }}
{{- end }}
Deployment.yaml Mount all configMap data to volume using subPath
spec:
volumes:
- name: {{ template "your-template.fullname" . }}-config
configMap: { name: {{ template "your-template.fullname" . }}-config }
containers:
- volumeMounts:
{{- range $key, $value := .Values.configMaps }}
- name: {{ template "your-template.fullname" $ }}-config
mountPath: /config/{{ $value.path }}
subPath: {{ $key }}
{{- end }}