Skip to main content
In this lesson we’ll learn how to use Kustomize to apply consistent modifications across multiple Kubernetes manifests using common transformers. Kustomize includes several built-in common transformers (and supports custom transformers), which let you centrally configure changes such as labels, annotations, namespaces, or name prefixes/suffixes for all resources referenced by a kustomization.yaml. Problem statement: imagine you have multiple resource files (for example, a Deployment and a Service) and you need to apply the same metadata or naming convention to all of them (for example, add an org: KodeKloud label to every resource, or append -dev to every resource name). Editing each manifest by hand is error-prone and does not scale. Example original manifests:
Manually editing each file to add labels, annotations, namespaces, or to modify names is tedious. Kustomize common transformers let you configure these consistently from a single kustomization.yaml so that every resource referenced by that kustomization receives the same transformations. Here is the desired output after applying common transformers (what Kustomize will emit):
This is exactly what common transformers are for — applying common labels, name prefixes/suffixes, namespaces, or annotations to all resources referenced by a kustomization. Below is a quick reference table of common transformers and their purpose.
A slide titled "Common Transformations" listing four Kubernetes resource transformations: commonLabel, namePrefix/Suffix, Namespace, and commonAnnotations. Each bullet briefly explains it adds a label, a name prefix/suffix, a common namespace, or an annotation to all resources.
When you run kustomize build (or kubectl apply -k) from a directory containing a kustomization.yaml, the common transformers in that file are applied to the set of resources referenced by that kustomization.
Below are concise examples showing how to configure each common transformer in a kustomization.yaml. Use these snippets directly in the directory that contains the referenced resource files (for example deployment.yaml, service.yaml).
  1. commonLabels
kustomization.yaml:
This will merge org: KodeKloud into each resource’s metadata.labels and into pod template labels where applicable (for example, Deployment.template.metadata.labels).
  1. namespace
If you want to ensure all resources are assigned to the same namespace, set the namespace field in the kustomization: kustomization.yaml:
After transformation, each resource will have metadata.namespace: lab. Note that Kustomize will not create a Namespace object for you unless you include a Namespace manifest explicitly.
  1. namePrefix and nameSuffix
To add a prefix and/or suffix to every resource name: kustomization.yaml:
Given a resource defined with metadata.name: api-service, the resulting name after kustomize will be KodeKloud-api-service-dev.
  1. commonAnnotations
To add the same annotation to every resource: kustomization.yaml:
After transformation, each resource will have metadata.annotations.branch: master. Putting them together You can include multiple common transformers in a single kustomization.yaml. For example: kustomization.yaml:
Running kustomize build (or kubectl apply -k .) in that directory will emit the resources with the namespace, prefixed/suffixed names, labels, and annotations applied consistently across all referenced resources.
Keep in mind: transformers defined in a kustomization.yaml only affect the resources referenced by that kustomization. Use overlays, separate kustomizations, or different kustomization directories if you need environment-specific transformations.
Further reading and references

Watch Video