ConfigMaps and Secrets
Outcome: a ConfigMap and a Secret generated from files and literals, the hash suffix on their names understood — including the production build where the hash changed — and the base64 fact about Secrets proven by decoding one, with the working rule that follows from it.
Generation, not transcription
Chapter 4's base wrote no ConfigMap YAML at all. It pointed a configMapGenerator at two sources:
configMapGenerator:
- name: webhello-config
files:
- settings.conf
literals:
- greeting=Hello from the base
files:— each file becomes an entry, keyed by its file name, holding its contents exactly. Editsettings.confand the next build carries the edit; nobody re-indents a pasted copy.literals:— eachkey=valuebecomes an entry directly. For one-line values that live naturally beside the kustomization, this is the tidy source.
A secretGenerator has the same shape and sources, and produces a Secret:
secretGenerator:
- name: webhello-secrets
literals:
- greeting-token=base-token-1234
Generators can also read values from an
environment-style file (envs:) — the
documentation's third source, not needed
by the lab.
The hash suffix is the mechanism
Generated names arrive suffixed:
webhello-config-t9599k79c5. The suffix
is a hash — a short fingerprint computed
from the resource's contents. Change the
contents and the fingerprint changes;
keep them and it holds still. The lab's
three builds demonstrate all of it
(transcript t06):
| build | ConfigMap | Secret |
|---|---|---|
| base | webhello-config-t9599k79c5 |
webhello-secrets-b955cdg665 |
| staging | staging-webhello-config-t9599k79c5 |
staging-webhello-secrets-b955cdg665 |
| production | prod-webhello-config-gdfm5tkmd4 |
prod-webhello-secrets-b955cdg665 |
The Secret's hash never moves: no overlay touches its contents, only its name's prefix. The ConfigMap's hash holds in staging (prefix only) and changes in production, because production changes a literal. Why want this? Because Kubernetes rolls out a Deployment when its pod template changes — and a reference to a new ConfigMap name is a template change. Edit the settings, and the chain fires by itself: new contents, new hash, new name, updated references, rolling update. Without the suffix, the name would stand still while its contents changed, and running pods would keep the old file with no rollout to save them.
The behaviour can be switched off
(generatorOptions: disableNameSuffixHash:
true), which some teams do so other
tooling can predict names. Know what you
are trading away before you reach for it:
the rollout guarantee above.
Changing a generated value per overlay
Production's kustomization reopens the generator (transcript t04):
configMapGenerator:
- name: webhello-config
behavior: merge
literals:
- greeting=Hello from production
behavior: is how an overlay's generator
entry meets the base's entry of the same
name:
merge— combine: sources add, and a literal repeated here replaces the base's value for that key. Production's ConfigMap still carriessettings.conffrom the base'sfiles:(check the build, t04) with only the greeting overridden — one literal of difference, and the new hash to show for it.replace— discard the base's entry entirely and use this one.create— the default when the base has no entry of this name: a new generated resource, this overlay only.
Secrets: encoding is not encryption
Now the fact this whole guide is careful
about. The base build prints the token as
YmFzZS10b2tlbi0xMjM0. That is
base64: a way of writing arbitrary
bytes as safe text, the format Kubernetes
Secrets use on the wire and on disk.
Base64 has no key and no secret of its
own; anyone can decode it, and the lab
does (t06):
$ echo YmFzZS10b2tlbi0xMjM0 | base64 -d
base-token-1234
So a literal typed into a kustomization file, or a file fed to a secretGenerator, is stored in the open — in your repository, in every build output, in this guide's transcripts. Nothing about the word "Secret" in the output changes that; the secrecy of a Kubernetes Secret is a property of who can read it inside the cluster, not of the YAML that describes it.
The working rules, which the lab obeyed with its invented token:
- Real values do not go in files you commit. Not as literals, not as files, not "temporarily". Generate placeholders in the repository, as the lab does, and let the real value arrive by a channel built for secrets.
- The usual channels: a dedicated secret manager, your platform's secret store, or an overlay that lives outside version control and is applied only where it is needed — an overlay whose generator reads a local file that itself never gets committed. The shape of the kustomization is the same; only the source of the value changes.
- Treat build output as sensitive as the
values in it.
kustomize buildprints Secrets in full; a build pasted into a ticket or a chat is a disclosure.
None of this makes generators useless — it makes them precise. They are excellent for configuration, which is public by nature, and for the shape of a Secret. The value is the part you source with care.