Sub topic 13
Copying values between fields with replacements
This chapter assumes you can write a kustomization.yaml, build a base and overlays, and read a strategic merge patch. It also assumes you know how generators and hash suffixes work from earlier chapters.
The problem replacements solve
Sometimes one value must appear in two places. A Service port should match the container port. A hostname in an Ingress should match a value in a ConfigMap. Kustomize has no variables or templates, so you cannot write ${PORT} in the base.
A replacement copies a value from a field in one resource (the source) into fields in other resources (the targets). It runs after the resources are loaded and after generators and patches have produced their output. The base stays plain, valid YAML.
Replacements supersede the older vars feature, which is deprecated. Use replacements in new work.
A worked example
The base has a Deployment named web that listens on port 8080, as in earlier chapters. Suppose a Service must send traffic to that same port.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 1The targetPort: 1 is a placeholder. The replacement below overwrites it with the Deployment's container port.
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
replacements:
- source:
kind: Deployment
name: web
fieldPath: spec.template.spec.containers.[name=web].ports.0.containerPort
targets:
- select:
kind: Service
name: web
fieldPaths:
- spec.ports.0.targetPortBuild it with kubectl kustomize .. The rendered Service now has targetPort: 8080. If someone changes the container port later, the Service follows on the next build.
Anatomy of a replacement
Each entry has two parts.
sourceselects one resource bykind,nameand optionallynamespace,groupandversion.fieldPathnames the field to read.targetsis a list. Each target has aselectblock, which picks the resources to change, andfieldPaths, which lists the fields to overwrite.
Field paths use dots. A list element is chosen by index (ports.0) or by a key match (containers.[name=web]). Key matching is safer than an index because it survives reordering.
Targeting several resources
A select block can match many resources, and a reject block can exclude some. You can also list several field paths in one target.
replacements:
- source:
kind: ConfigMap
name: web-config
fieldPath: data.LOG_LEVEL
targets:
- select:
kind: Deployment
reject:
- name: legacy-worker
fieldPaths:
- metadata.annotations.log-level
options:
create: trueBy default, a replacement fails if the target path does not exist. Setting create: true makes Kustomize add the missing field, here an annotation.
Replacing part of a string
Sometimes the source value belongs inside a longer string, such as a URL. Use delimiter and index in the target options to replace one segment.
replacements:
- source:
kind: ConfigMap
name: web-config
fieldPath: data.HOST
targets:
- select:
kind: Deployment
name: web
fieldPaths:
- spec.template.spec.containers.[name=web].args.0
options:
delimiter: "="
index: 1If args.0 is --host=placeholder, the text after the first = is replaced and the result is --host= followed by the source value.
Common errors
- Source not found. The
namemust match the resource as it is when replacements run. That is the name from the base, before anynamePrefixis applied. - Several sources match. The source must select exactly one resource.
- Missing target field. Add the field to the base or set
create: true. - Placeholder values left in the base. Keep them obviously fake, so a build that skips the replacement is easy to spot.
Exercise
A replacement reads data.HOST from a ConfigMap and targets spec.rules.0.host on an Ingress. The build fails with an error saying the target field does not exist. The Ingress in the base has no rules entry yet. What are two ways to fix it?
Show answer
Add a rules entry with a placeholder host to the Ingress in the base, so the path exists. Or add options: create: true to the target. The first keeps the base explicit. The second lets the replacement create the missing field. For a list index such as rules.0, adding the placeholder to the base is usually the safer choice.