What configmap
is
A ConfigMap is an API object used to store non-confidential data in
key-value pairs. Pods can consume ConfigMaps as environment variables,
command-line arguments, or as configuration files in a volume.
A ConfigMap allows you to decouple environment-specific configuration
from your container images, so that your applications are easily
portable.
In other words when configmap
is created from external file (like your case) or from other values, API server will validate if everything is correct and then will save it to etcd
. This also explains reasons why when you edit
your configmap
you see its entire context. This happens because configmap
is read from etcd
, not any external sources.
This is also one of the reasons why it's not recommended to store save big files as configmaps or secrets - it will affect kubernetes cluster performance as all cluster's objects are stored within etcd
.
Kustomize
This is one of the way to achieve your requirement to run one command on creating/configuring configmap
based on the file in the same directory.
It is a standalone tool to customize Kubernetes objects through a kustomization file.
I created a short and simple example to show the idea:
$ tree
.
├── application.properties
└── kustomization.yaml
0 directories, 2 files
$ cat kustomization.yaml
generatorOptions:
disableNameSuffixHash: true # this flag is used to avoid creation of new configmap, instead it will be modified when file context is changed
configMapGenerator:
- name: application-config
files:
- application.properties
$ cat application.properties
listen.url=localhost:9010
client.url=some_url:3000
Test this, this command will only render configmap, it won't create it yet:
$ kubectl kustomize application.settings/
apiVersion: v1
data:
application.properties: |
listen.url=localhost:9010
client.url=some_url:3000
kind: ConfigMap
metadata:
name: application-config
Final step is to apply it:
$ kubectl apply -k application.settings/
configmap/application-config created
$ kubectl get cm
NAME DATA AGE
application-config 1 23s
Short explanation to command above:
To apply those Resources, run kubectl apply with --kustomize or -k
flag.
Useful links: