commonLabels, namePrefix/nameSuffix, namespace, commonAnnotations—and the images transformer. The example repository layout has two component directories under a top-level k8s/ directory:
- k8s/
- api/
- api-depl.yaml
- api-service.yaml
- kustomization.yaml
- db/
- db-depl.yaml
- db-service.yaml
- db-config.yaml
- kustomization.yaml
- kustomization.yaml (root)
- api/
kustomization.yaml files and a step-by-step walkthrough showing how each transformer is applied and what resources it affects.
This guide focuses on transformer scope: where you place a transformer (root vs. subdirectory) determines which resources get modified. Use
kustomize build <dir> to inspect the composed manifests after each change.Subdirectory kustomization files
Each component folder lists its own resources in akustomization.yaml.
Example: api/kustomization.yaml
db/kustomization.yaml
kustomization.yaml composes the API and DB overlays:
1) Apply a global common label
To add the same label to every resource produced by the root composition, setcommonLabels in the root kustomization.yaml:
api/ and db/) include the department: engineering label.
2) Apply labels scoped to a subdirectory
If you setcommonLabels in a subdirectory kustomization.yaml, the label only applies to the resources listed by that kustomization.
Example: add a label in api/kustomization.yaml:
- Resources under
api/includefeature: api. - Resources under
db/do NOT includefeature: api(they still only have any root-level labels, e.g.department: engineeringif configured).
db/kustomization.yaml:
feature: db. This demonstrates that transformers declared in a kustomization only affect resources declared by that kustomization (and any children it references).
3) Set a namespace globally
To assign all generated resources to a specific namespace from the root, addnamespace:
kustomize build k8s, resources that support namespaces will include:
4) Name prefix and name suffix
UsenamePrefix at the root for a global prefix:
nameSuffix in subdirectory kustomizations to append a suffix only to that overlay’s resources.
Example: api/kustomization.yaml
db/kustomization.yaml
- API deployment original
api-deployment->KodeKloud-api-deployment-web - DB deployment original
db-deployment->KodeKloud-db-deployment-storage
5) Add common annotations
To add the same annotation to every generated resource, usecommonAnnotations at the root:
logging: verbose appear under metadata.annotations for each resource.
6) Image transformer (rewrite container images)
Theimages transformer rewrites image names and tags. Placement controls scope:
images:in the root kustomization affects any resource in the composition that references the image.images:in a subdirectory affects only resources listed by that subdirectory.
mongo to Postgres only in the DB overlay by adding to db/kustomization.yaml:
Always quote
newTag (e.g. "4.2") so Kustomize parses it as a string. Unquoted numeric tags can cause an error like: “cannot unmarshal number into Go struct field image of type string”.name: mongo remains unchanged; only the image field is rewritten. If you move the same images block to the root kustomization.yaml, every resource referencing mongo anywhere in the composed output would be rewritten to postgres:4.2.
Quick reference: transformer scope
Summary
- Place
commonLabels,commonAnnotations,namespace, andnamePrefixin the rootkustomization.yamlto affect the entire composition. - Place
commonLabels,nameSuffix,images, or other overlay-specific transformers in a subdirectorykustomization.yamlto limit their effect to that overlay’s resources. - The
imagestransformer matches existing image names and rewritesnewNameandnewTag(use quotes fornewTag). - Use
namePrefixat root for a consistent global prefix andnameSuffixin components to distinguish resources by component.