Patches and generators
Change only the field you need
Patches express a focused change to selected resources. A strategic merge patch uses Kubernetes field merge semantics where supported; for a Deployment container list, the container name is the merge key. It is not a generic deep merge for all lists or custom resources. Use the modern patches field; older patchesStrategicMerge and patchesJson6902 are legacy fields to check when migrating. Targets may specify group, version, kind, name, namespace and supported selector fields. Keep patches narrow and inspect all affected resources.
apiVersion: apps/v1kind: Deploymentmetadata: name: catalogspec: replicas: 1JSON patch and image changes
The staging overlay selects the base Deployment by API group, version, kind and original name. Its JSON patch replaces the existing replicas field with 3. JSON patch operations address exact paths; replacing a missing path fails, and list indexes can become fragile when items are reordered. The images field independently updates matching image names to a new tag. In a production pipeline use reviewed immutable image digests when appropriate; the fictional image here is for inspection only.
apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../basenamespace: staging-studynamePrefix: stg-patches:- path: replicas.json target: group: apps version: v1 kind: Deployment name: catalogimages:- name: registry.example.invalid/catalog newTag: v2configMapGenerator:- name: catalog-settings behavior: merge literals: - MODE=staging - PAGE_SIZE=20Understand generator output
The base generator declares MODE and PAGE_SIZE as strings. Each overlay merges selected keys into the generator with the same logical name. By default Kustomize adds a content-related suffix to generated ConfigMap and Secret names. It rewrites recognized references such as the container configMapRef to the final name. In staging the expected values are MODE=staging and PAGE_SIZE=20; do not memorize a suffix or hardcode it in a Pod. When content changes and a Deployment Pod template references the new name, that template change can trigger a rollout after apply. Existing running processes are not automatically reconfigured by building YAML.
configMapGenerator:- name: catalog-settings behavior: merge literals: - MODE=staging - PAGE_SIZE=20Control references and sensitive values
A replacement copies a field from a selected source resource to selected target fields, such as a Service name into a container argument that needs it. Use replacements with exact field paths and inspect their output; arbitrary strings are not necessarily rewritten by built-in name transformations. generatorOptions can disable generated name suffixes, but then a data-only change may leave the Pod template unchanged; choose an explicit reload or rollout strategy. secretGenerator can create Secret YAML from inputs, but rendering may expose encoded sensitive content. Never commit real secrets or upload rendered secrets to logs. Old generated resources may remain after apply; review ownership and references before any scoped cleanup.
replacements:- source: kind: Service name: catalog fieldPath: metadata.name targets: - select: kind: Deployment name: catalog fieldPaths: - spec.template.spec.containers.0.args.0# Illustrative only: target args must exist or be explicitly created.Inspect the relationships, not formatting
A useful test checks that the rendered Deployment and Service share selectors, the container points to the generated ConfigMap, and the expected namespace, replica count, image and data agree. YAML order and formatting are not the application contract. Save the two local builds to files if a renderer is available and use the optional rendered-output checks. Even successful rendering does not verify admission, registry access or application health. No live Kubernetes API is contacted by the included checker.
kustomize build overlays/dev > dev.yamlkustomize build overlays/staging > staging.yamlpython validate_offline.py dev.yaml staging.yamlPractice and recap
Predict the effect of replacing PAGE_SIZE only in development. Explain why the staging replica patch is independent of the ConfigMap generator. Spot two mistakes: using the wrong resource name as a patch target and hardcoding a generated suffix. Use the expected resource relationships rather than an invented exact hash. A reviewer should be able to trace every rendered field to a base, a patch or a transformation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Patches and generators — knowledge check
Reason about local composition and resource relationships.
The JSON patch replaces /spec/replicas with 3. What must be true?
The staging generator merges PAGE_SIZE=20 with the base. What is the expected value?
Why avoid hardcoding the generated ConfigMap suffix?
If name suffix generation is disabled and only ConfigMap data changes, what follows?
What is the safest treatment of generated Secret YAML?