Sub topic 9
Generating ConfigMaps and Secrets
This chapter assumes you can read Kubernetes YAML, know what a ConfigMap and a Secret are, and have a base and an overlay set up as in the earlier chapters.
Why generate instead of write
You can keep a ConfigMap as a plain YAML file and list it under resources. Kustomize also offers generators. A generator builds the ConfigMap or Secret from literal values, files, or env files. It also appends a hash of the content to the object's name. When the content changes, the name changes, and the Deployment that references it is updated and rolls out new Pods.
Generating a ConfigMap in the base
Add a configMapGenerator to base/kustomization.yaml:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
configMapGenerator:
- name: web-config
literals:
- LOG_LEVEL=debugRender it with kubectl kustomize base. The output contains a ConfigMap named like web-config-7h5k2m9d4t. The suffix is derived from the data, so the same data always produces the same name.
Sources for generated data
A generator accepts three kinds of input. You can combine them in one entry.
| Field | Source | Result |
|---|---|---|
| `literals` | `KEY=value` lines in the kustomization | One key per literal |
| `files` | A file path, or `key=path` | The file's contents under one key |
| `envs` | A `.env` style file | One key per line in the file |
Secrets work the same way with secretGenerator. Kustomize base64-encodes the values for you.
secretGenerator:
- name: web-credentials
envs:
- credentials.env
type: OpaqueMerging or replacing per overlay
An overlay can change a generated ConfigMap by declaring a generator with the same name and a behavior:
createis the default and makes a new object.mergekeeps the base keys and adds or overrides the keys you list.replacediscards the base data and uses only what you list.
configMapGenerator:
- name: web-config
behavior: merge
literals:
- LOG_LEVEL=warnWith merge, production keeps every key from the base and overrides LOG_LEVEL to warn. The overlay's generator name must match the base name, written without the hash suffix.
How references stay consistent
The Deployment refers to the ConfigMap by its plain name, web-config. When Kustomize renders, it rewrites that reference to the hashed name, including after a namePrefix is applied. You do not edit the Deployment by hand. A reference counts only where Kustomize knows to look, such as envFrom, configMapKeyRef, and volumes.
envFrom:
- configMapRef:
name: web-configTwo cautions
If a ConfigMap is used only by something Kustomize cannot see, such as a custom resource, the name is not rewritten. Set disableNameSuffixHash: true under generatorOptions to turn the suffix off, but then Pods will not restart automatically when the data changes.
Old hashed ConfigMaps stay in the cluster after an update unless you prune them. With kubectl apply -k, you can add --prune with a label selector to remove them.
Exercise
The production overlay declares configMapGenerator with name: web-config and behavior: merge, and the base has LOG_LEVEL=debug and TIMEOUT=30. The overlay lists only LOG_LEVEL=warn. Which keys does the production ConfigMap contain, and what changes if you use behavior: replace?
Show answer
With merge, it contains LOG_LEVEL=warn and TIMEOUT=30. With replace, it contains only LOG_LEVEL=warn, because the base data is discarded.