Sub topic 5
Setting namespace, name prefix and labels
This chapter assumes you can read a kustomization.yaml, you know how a base and an overlay relate, and you can run kubectl kustomize. Here you will use the built-in transformers that change every resource in a build at once.
What a transformer does
A transformer is a step that Kustomize runs on the loaded resources after it reads them and before it prints them. The base files stay unchanged. The transformer edits the in-memory copy, so the output differs per overlay while the base stays plain, valid YAML.
Three fields cover most environment differences:
namespacesetsmetadata.namespaceon every namespaced resource.namePrefixandnameSuffixadd text to every resource name.labelsandcommonAnnotationsadd metadata to every resource.
Namespace
Add namespace to the overlay:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: productionKustomize sets the namespace on each namespaced resource, even if the base file already names a different one. It leaves cluster-scoped kinds such as ClusterRole alone. It does not create the namespace. If production does not exist, kubectl apply -k fails, so add a Namespace manifest to your resources.
Name prefix and suffix
namePrefix: prod-The Deployment web becomes prod-web. Kustomize also updates references to renamed objects. A Service selector is not a name reference and is unaffected, but fields such as configMapKeyRef.name and a volumes entry that points to a ConfigMap are rewritten.
Prefixes let several environments share one namespace. They also make the environment obvious in kubectl get output.
Labels
Use the labels field:
labels:
- pairs:
env: production
team: storefront
includeSelectors: falseincludeSelectors is the setting that matters most. When it is true, Kustomize also adds the labels to spec.selector.matchLabels and to the pod template labels. A Deployment's selector cannot be changed after creation. If you add a label with includeSelectors: true to a Deployment that already exists, the apply fails with an immutable field error. Use false (the default) for labels that describe the environment, and reserve true for labels you set from the first deploy.
The older commonLabels field always changes selectors. Prefer labels in new projects.
Annotations
commonAnnotations works like labels but never touches selectors, so it is safe to add later:
commonAnnotations:
owner: [email protected]Seeing the result
Render the overlay and check the output:
kubectl kustomize overlays/productionThe Deployment now has name: prod-web, namespace: production, and the added labels and annotations.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `namespaces "production" not found` | Kustomize does not create namespaces. | Add a `Namespace` resource. |
| `field is immutable` on a Deployment | Labels with selectors were added after the first apply. | Set `includeSelectors: false`, or recreate the Deployment. |
| Service stops reaching pods | A selector label was changed on one side only. | Keep selector labels out of environment transformers. |
Exercise
An overlay uses labels with includeSelectors: true to add env: staging to a Deployment that has been running for a month. What happens on the next kubectl apply -k, and how do you avoid it?
Show answer
The apply fails because the change modifies spec.selector.matchLabels, which is immutable. Set includeSelectors: false so the label is added only to metadata, or delete and recreate the Deployment if you really need the label in the selector.