Skip to main content
In this article, we explore how to efficiently modify your Kubernetes configurations using Kustomize’s built-in transformers. Kustomize allows you to apply common configuration changes across multiple YAML files—whether by adding labels, modifying resource names, setting namespaces, or applying annotations—without editing each file manually. Below, we outline the challenges these transformers solve and provide detailed examples of several common transformations.

The Problem

Imagine working with multiple Kubernetes resource files, such as a Deployment and a Service. Consider the following initial configuration files:
Suppose you want to apply a common configuration change—like adding a label (for example, org: KodeKloud) or appending a suffix (such as -dev) to resource names—across all these files. Manually updating each file in a production environment with numerous YAML files can be time-consuming and error-prone. This is where Kustomize’s transformers become extremely useful.

Transformations with Kustomize

1. Common Label Transformation

Instead of manually updating each resource, you can add a common label to all Kubernetes resources via your kustomization file. For example, if you want to add the label org: KodeKloud, you could modify your YAML files as follows:
Rather than updating every file individually, add the following to your kustomization file:
This change automatically appends the label to all imported resources.

2. Name Prefix/Suffix Transformation

If you need to append a suffix (like -dev) to the names of all your resources, you can adjust your resource file names manually or delegate the change to Kustomize. For instance:
Instead of manually updating each resource name, you can let Kustomize handle it with the following configuration:
Kustomize will automatically modify the names of all imported resources to include the specified prefix and suffix.

3. Namespace Transformation

The namespace transformer enables you to group all your Kubernetes resources under a designated namespace. Instead of individually editing each file, simply modify the metadata within your YAML. For example:
This method ensures that the Service is deployed in the specified namespace (lab in this case).

4. Common Annotations Transformation

When you need to apply common annotations across all your Kubernetes resources, Kustomize makes it easy. For example, to add an annotation like branch: master, update your resource file:
Then declare it in your kustomization file:
With this setup, every resource managed by this kustomization automatically receives the annotation.

Conclusion

Kustomize’s common transformers significantly streamline the process of updating and maintaining your Kubernetes configurations. By centrally managing transformations—such as adding labels, modifying resource names with prefixes or suffixes, setting namespaces, or applying annotations—you not only reduce manual effort but also minimize the possibility of errors in your production deployments.
Leveraging Kustomize transformers ensures consistent and scalable configuration management, which is essential in dynamic environments with multiple resources.
For more detailed information on Kubernetes and configuration management tools, make sure to explore the following resources:

Watch Video