Sub topic 10
How hash suffixes and name references stay consistent
This chapter assumes you can read a kustomization.yaml, know what a ConfigMap and a Deployment are, and have used kubectl kustomize to render output. It explains why generated ConfigMap names change, and how Kustomize keeps every reference to them correct.
The problem a hash suffix solves
Kubernetes does not restart Pods when a ConfigMap changes. If you edit a ConfigMap in place, running Pods keep using the old values until something else restarts them.
Kustomize avoids this with a content hash. A configMapGenerator appends a short hash of the ConfigMap's contents to its name. Change the data and the name changes. A new name means the Deployment's Pod template changes, so Kubernetes starts a rolling update.
Here is the generator from the base:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
configMapGenerator:
- name: web-config
literals:
- LOG_LEVEL=debugThe rendered ConfigMap has a name such as web-config-<hash>. The hash is a fixed-length string derived from the data. The exact value depends on the content, so do not copy it by hand.
Name references are rewritten for you
Your Deployment can refer to the ConfigMap by its plain name:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: registry.example.com/web:1.0.0
envFrom:
- configMapRef:
name: web-configDuring the build, Kustomize finds fields that are known to hold object names, such as configMapRef.name, configMapKeyRef.name, and volumes[].configMap.name. It rewrites each one to match the generated name, including the hash and any prefix. You never write the hash in your own files.
The same mechanism handles namePrefix and nameSuffix. With namePrefix: prod-, the Deployment becomes prod-web, and the ConfigMap reference becomes prod-web-config-<hash>.
Merging in an overlay keeps the chain intact
An overlay can change generated data with behavior: merge:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: prod-
configMapGenerator:
- name: web-config
behavior: merge
literals:
- LOG_LEVEL=warnThe hash is computed from the final merged content, so it is warn-based, not debug-based. References are rewritten to that final name. Use behavior: replace to discard the base data entirely, or behavior: create (the default) for a new ConfigMap.
Common errors
- A reference that is not rewritten. Kustomize only updates fields it knows hold names. A custom resource that stores a ConfigMap name in an unusual field keeps the plain name, and the Pod fails to find it. Register the field with a
configurationsfile. - Hand-writing the hash. If you paste
web-config-abc123into a manifest, the reference breaks at the next data change. - Applying a ConfigMap on its own. Generated names exist only in the build output. Apply through
kubectl apply -k, not individual files. - Expecting no suffix. If you need a stable name, set
generatorOptionswithdisableNameSuffixHash: true. You then lose the automatic rollout on change.
generatorOptions:
disableNameSuffixHash: trueCheck the result
Render the overlay and search for the ConfigMap name:
kubectl kustomize overlays/production | grep web-configYou should see the same prod-web-config-<hash> value in the ConfigMap's metadata.name and in the Deployment's configMapRef.
Exercise
You change LOG_LEVEL in a configMapGenerator and run kubectl apply -k. What happens to the running Pods, and why?
Show answer
The hash in the ConfigMap name changes because the data changed. Kustomize rewrites the Deployment's reference to the new name, so the Pod template changes and Kubernetes performs a rolling update. With disableNameSuffixHash: true, the name would stay the same and Pods would not restart.
Exercise
An overlay sets namePrefix: prod- and merges a value into web-config. What name does the Deployment's configMapRef have in the rendered output?
Show answer
prod-web-config-<hash>, where the hash comes from the merged data.