Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate Kubernetes ConfigMap data fields into individual files

I'm playing around with kubernetes ConfigMaps. In the official documentation, I see "file-like keys" in the data field:

apiVersion: v1
kind: ConfigMap
metadata:
  name: game-demo
data:
  # file-like keys
  game.properties: |
    enemy.types=aliens,monsters
    player.maximum-lives=5    
  user-interface.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true    

Is it possible to break these "file-like keys" into different files and reference them in this ConfigMap resource?

I see several benefits of this approach:

  • Slimmed down ConfigMap
  • Proper syntax highlighting for the "file-like" configurations
  • Can run auto formatters against the "file-like" configurations
like image 680
Johnny Metz Avatar asked Oct 18 '25 12:10

Johnny Metz


1 Answers

  • Proper syntax highlighting for the "file-like" configurations
  • Can run auto formatters against the "file-like" configurations

Yes, it is easier to save the files as proper files on your machine and in Git.

I propose that you use the kustomize feature of kubectl and use configMapGenerator to generate the ConfigMap instead.

Example kustomization.yaml (saved in the same directory as your files, e.g. in config/)

configMapGenerator:
- name: game-demo
  files:
  - game.properties
  - user-interface.properties

Then you can apply (and generate the configMap) with (if your config is in config/):

kubectl -k config/

Or you can preview the "generated" configMap with:

kubectl kustomize config/
like image 74
Jonas Avatar answered Oct 21 '25 02:10

Jonas