Replacements
Outcome: a value written once — a ConfigMap entry — copied by the build into a container's environment variable, with the base build's placeholder and the production build's real sentence both on the page as the proof.
The gap replacements fill
Chapter 4's Deployment carries a small embarrassment:
env:
- name: GREETING
value: PLACEHOLDER
The greeting the application serves is
configuration, and configuration already
has a home: the generated ConfigMap holds
greeting as a literal. Writing the same
sentence in the Deployment as well would
put one fact in two places, free to drift
apart at the first careless edit.
Templates solve this with a variable.
Kustomize solves it with a
replacement: an instruction to copy
a field from one built resource into a
field of another, at build time.
The replacement
Production's kustomization declares it (transcript t04):
replacements:
- source:
kind: ConfigMap
name: webhello-config
fieldPath: data.greeting
targets:
- select:
kind: Deployment
name: webhello
fieldPaths:
- spec.template.spec.containers.0.env.[name=GREETING].value
Read it as two addresses and an arrow:
- Source — in the resource of kind
ConfigMapnamedwebhello-config, the field atdata.greeting. The name given is the name as written in the kustomization, before prefixes and hash suffixes; kustomize resolves it to the generated, suffixed resource. - Target — in the Deployment named
webhello, the field at the given path. The path walks the resource: the pod template's first container'senvlist, the entry whosenameisGREETING(the[name=GREETING]segment selects a list entry by a field rather than by position), and that entry'svalue. - The arrow copies source to target, overwriting what the target held.
The proof, in three builds
- Base (t02): no replacement is
declared, so
GREETINGkeeps its placeholder:value: PLACEHOLDER. Honest scaffolding — visible, and obviously not a real greeting. - Staging (t03): staging declares no
replacement either, so it too prints
PLACEHOLDER. A layer only gets what it asks for. - Production (t04): the environment
prints
value: Hello from production.
The last line is the whole chapter in one
field. The sentence came from the
ConfigMap — where production's merge
had already replaced the base's greeting
with its own (chapter 6) — and the
replacement carried the final value
into the Deployment. Neither file
contains the other's copy; the build
joined them. Change the literal in
production's generator, rebuild, and the
ConfigMap and the environment variable
move together, hash and all.
When to use them
Reach for a replacement when a value has
one rightful owner and several consumers:
an image name that must also appear in a
job's argument, a service's name that a
client needs as an address, an identifier
minted in one resource and referenced in
another. The source can be any field of
any built resource — metadata.name
included, which is the tidy way to hand a
generated, suffixed name to something the
reference-rewriter does not know about.
And a restraint to keep: replacements are plumbing, not a programming language. If a kustomization accumulates dozens of them, weaving values through every resource, the base has stopped being readable on its own — the very thing chapter 1 promised to protect. A few copies, each with an owner you can point at, is the healthy shape.