ConfigMap & Secret Generators
Writing ConfigMaps by hand has the problem met in Files, ConfigMaps & Secrets: changing one doesn't restart the Pods that read it. Kustomize's generators build ConfigMaps and Secrets from literals, files or .env files, and append a hash of the content to the name. Change the content, the name changes; every reference is rewritten to the new name; the Pod template changes; Kubernetes rolls out new Pods. No annotations needed.
configMapGenerator
mkdir -p app && cd app
cat > deployment.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: linkly
spec:
selector: {matchLabels: {app: linkly}}
template:
metadata: {labels: {app: linkly}}
spec:
containers:
- name: linkly
image: ghcr.io/acme/linkly:1.4.0
envFrom: [{configMapRef: {name: linkly-config}}]
volumeMounts: [{name: blocklist, mountPath: /etc/linkly}]
volumes:
- name: blocklist
configMap: {name: linkly-blocklist}
EOF
printf 'phishing.example\nmalware.example\n' > blocklist.txt
printf 'REDIS_ADDR=linkly-redis:6379\nBASE_URL=https://lnk.example.com\n' > app.env
cat > kustomization.yaml <<'EOF'
resources: [deployment.yaml]
configMapGenerator:
- name: linkly-config
envs: [app.env]
literals: [LOG_LEVEL=info]
- name: linkly-blocklist
files: [blocklist.txt]
EOF
kustomize build . | grep -E '^ name:|name: linkly-'
echo "--- after changing LOG_LEVEL:"
sed -i 's/LOG_LEVEL=info/LOG_LEVEL=debug/' kustomization.yaml
kustomize build . | grep -E '^ name:|name: linkly-' name: linkly-blocklist-hk97mdm49d
name: linkly-config-2h5m8d6bc2
name: linkly
name: linkly-config-2h5m8d6bc2
name: linkly-blocklist-hk97mdm49d
--- after changing LOG_LEVEL:
name: linkly-blocklist-hk97mdm49d
name: linkly-config-cftmhcgc66
name: linkly
name: linkly-config-cftmhcgc66
name: linkly-blocklist-hk97mdm49d
Three sources, which can be combined in one generator:
| Source | Becomes |
|---|---|
literals: [KEY=value] |
one key per literal |
envs: [app.env] |
one key per KEY=value line of the file |
files: [blocklist.txt] or files: [key=path] |
one key per file, named after the file (or key), holding its content |
After the edit, only linkly-config got a new suffix, and the Deployment's configMapRef followed it, while linkly-blocklist kept its name. The old ConfigMap stays in the cluster until something prunes it (kubectl apply --prune, or a GitOps tool with pruning), which is a feature: Pods of the previous ReplicaSet still have their config during the rollout and after a rollback.
How the suffix is computed
The suffix is a hash of the object's kind and content, shortened to 10 characters with an alphabet that avoids vowel-like digits. This model reproduces Kustomize 5.8's suffix for the first ConfigMap above (before the edit), which shows that the name itself is not part of the hash:
import hashlib, json
def kustomize_suffix(kind, data, extra=None):
obj = {"kind": kind, "name": "", "data": data, **(extra or {})}
encoded = json.dumps(obj, sort_keys=True, separators=(",", ":")) # like Go's json.Marshal
digest = hashlib.sha256(encoded.encode()).hexdigest()[:10]
return digest.translate(str.maketrans("013ae", "ghkmt"))
config = {"BASE_URL": "https://lnk.example.com", "LOG_LEVEL": "info", "REDIS_ADDR": "linkly-redis:6379"}
print("linkly-config-" + kustomize_suffix("ConfigMap", config))
print("other-name-" + kustomize_suffix("ConfigMap", config))
print("after the edit: linkly-config-" + kustomize_suffix("ConfigMap", {**config, "LOG_LEVEL": "debug"}))linkly-config-2h5m8d6bc2
other-name-2h5m8d6bc2
after the edit: linkly-config-cftmhcgc66
The first and third lines match the names in the session above; the second shows that renaming the generator would not change the suffix. Since the hash depends only on content, two environments with identical config get identical names, which makes overlay diffs quiet when nothing changed.
Overlays: merge or replace
An overlay can change a generator from the base with behavior:
mkdir -p base staging
cat > base/kustomization.yaml <<'EOF'
configMapGenerator:
- name: linkly-config
literals: [LOG_LEVEL=info, REDIS_ADDR=linkly-redis:6379]
EOF
cat > staging/kustomization.yaml <<'EOF'
resources: [../base]
configMapGenerator:
- name: linkly-config
behavior: merge
literals: [LOG_LEVEL=debug, FEATURE_QR=true]
secretGenerator:
- name: linkly-db
literals: [url=postgres://linkly:staging-only@db.staging:5432/linkly]
generatorOptions:
labels: {app.kubernetes.io/part-of: linkly}
EOF
kustomize build stagingapiVersion: v1
data:
FEATURE_QR: "true"
LOG_LEVEL: debug
REDIS_ADDR: linkly-redis:6379
kind: ConfigMap
metadata:
labels:
app.kubernetes.io/part-of: linkly
name: linkly-config-7ftd87btc2
---
apiVersion: v1
data:
url: |
cG9zdGdyZXM6Ly9saW5rbHk6c3RhZ2luZy1vbmx5QGRiLnN0YWdpbmc6NTQzMi9saW5rbH
k=
kind: Secret
metadata:
labels:
app.kubernetes.io/part-of: linkly
name: linkly-db-b4d526ff47
type: Opaque
behavior: merge kept REDIS_ADDR from the base and overrode LOG_LEVEL; replace would discard the base's keys; create (the default) fails if the name already exists. The Secret's values are base64-encoded in data, and it got type: Opaque (set type: on the generator for TLS or docker-config Secrets).
Options and cautions
generatorOptions.disableNameSuffixHash: trueturns the hash off, for ConfigMaps read by something that needs a fixed name (another team's app, a Helm chart'sexistingConfigMap). You lose the automatic rollout.options: {immutable: true}on a generator marks the object immutable, which pairs well with hashed names: content never changes in place, it gets a new name.secretGeneratorwith literals or files puts the secret in Git. Fine for a local dev overlay, never for real credentials. In real environments, reference Secrets created by External Secrets Operator or Sealed Secrets, or encrypt the files with SOPS (the KSOPS plugin or Flux's SOPS support decrypts at build time).- Generated names are only rewritten in reference fields Kustomize knows. A ConfigMap name inside a command-line argument or a custom resource is not updated; use
replacements(next-but-one lesson) for those.
Test yourself
Why does changing a generated ConfigMap trigger a rollout?
- Kustomize restarts the Pods
- The content hash in the name changes, the Deployment's reference changes, so the Pod template changes
- Kubernetes watches ConfigMaps
- A checksum annotation is added
An overlay must change one key of the base's generated ConfigMap. Which behavior?
- create
- merge
- replace
- delete
Is secretGenerator with literals: a safe way to ship a production database password?
- Yes, Kustomize encrypts it
- Yes, base64 is enough
- No: the value sits in Git in clear text
- Only with disableNameSuffixHash