> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Transformers Demo

> This article explores using Kustomize transformations for managing Kubernetes configurations, including adding labels, namespaces, and modifying container images.

In this lesson, we explore how to use several common transformations in Kustomize, including the image transformer. The demo utilizes a structured Kubernetes (K8s) directory setup containing two folders—one for API components and one for database components.

At the API level, you will find:

* An API deployment YAML file
* An API service YAML file
* A kustomization.yaml file that imports these resources

Similarly, the database folder contains:

* A Database deployment YAML file
* A Database service YAML file
* A configuration file (`config.yaml`)
* A dedicated kustomization.yaml file that imports the database-related Kubernetes configurations

At the root level, the main kustomization.yaml brings together both the API and database directories:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api/
  - db/
```

## Adding a Common Label

To streamline management, you can add a common label to every resource in your environment. By updating the root kustomization.yaml file, you can apply a global label. For example, add the label `department: engineering` as shown below:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api/
  - db/
commonLabels:
  department: engineering
```

After saving the file, execute the following command in your terminal to build the configurations:

```bash theme={null}
kustomize build k8s
```

The output confirms that every resource (such as ConfigMap, Service, and Deployment) now has the new label applied. Here is a snippet of the generated output:

```yaml theme={null}
apiVersion: v1
data:
  password: example
  username: root
kind: ConfigMap
metadata:
  labels:
    department: engineering
  name: db-credentials
---
apiVersion: v1
kind: Service
metadata:
  labels:
    department: engineering
  name: api-service
spec:
  ports:
    - port: 80
      protocol: TCP
      targetPort: 3000
```

Every resource within both the API and database folders now includes the `department: engineering` label.

## Applying Labels in Subdirectory kustomization.yaml Files

In some scenarios, you might prefer to apply a label only to resources defined within a specific folder. For example, to add a label `feature: api` specifically for the API components, update the API folder’s kustomization.yaml as follows:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api-depl.yaml
  - api-service.yaml
commonLabels:
  feature: api
```

When you run the build command, resources in the API directory will display both `department: engineering` (from the root configuration) and `feature: api` (from the subdirectory). In contrast, resources in the database folder will not show the `feature: api` label.

Similarly, to add a label for the database folder, include the following in its kustomization.yaml:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - db-config.yaml
  - db-depl.yaml
  - db-service.yaml
commonLabels:
  feature: db
```

After building, the database deployment, service, and config map will reflect the `feature: db` label.

## Adding a Namespace

Applying a namespace to all resources is as simple as adding the `namespace` field to the root kustomization.yaml file. For example, to assign the namespace `debugging`, update the file accordingly:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api/
  - db/
commonLabels:
  department: engineering
namespace: debugging
```

After running `kustomize build`, every resource—including those from the API and database folders—will have the namespace `debugging`.

## Setting Name Prefixes and Suffixes

You may also want to add a name prefix and folder-specific suffix to your resources. Suppose you want to prefix every resource’s name with `KodeKloud-` and apply a suffix of `-web` to API objects and `-storage` to database objects.

To add a global name prefix, include it in the root kustomization.yaml:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api/
  - db/
commonLabels:
  department: engineering
namespace: debugging
namePrefix: KodeKloud-
```

Then, within each subdirectory’s kustomization.yaml, add a name suffix. For the API folder, update the file as follows:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api-depl.yaml
  - api-service.yaml
commonLabels:
  feature: api
nameSuffix: -web
```

For the database folder:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - db-config.yaml
  - db-depl.yaml
  - db-service.yaml
commonLabels:
  feature: db
nameSuffix: -storage
```

After building the configurations, the API deployment might be named `KodeKloud-api-deployment-web` and the database deployment `KodeKloud-db-deployment-storage`.

## Adding a Global Annotation

To attach an annotation to every resource, include the `commonAnnotations` field in the root kustomization.yaml file. For instance, to add an annotation `logging: verbose`, update the file as follows:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api/
  - db/
commonLabels:
  department: engineering
commonAnnotations:
  logging: verbose
namespace: debugging
namePrefix: KodeKloud-
```

Every resource will now feature the annotation `logging: verbose` in its metadata.

## Using an Image Transformer

The image transformer is a powerful feature that enables you to modify container image references directly in your configuration files. Suppose the current database deployment uses the Mongo image and you want to replace it with Postgres. Open the kustomization.yaml file in the database folder and add the following image transformer section:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - db-config.yaml
  - db-depl.yaml
  - db-service.yaml
commonLabels:
  feature: db
nameSuffix: -storage
images:
  - name: mongo
    newName: postgres
    newTag: "4.2"
```

This configuration replaces any reference to the Mongo image with the Postgres image tagged as "4.2". Note that only the image reference is modified—the container name remains unchanged.

<Callout icon="triangle-alert" color="#FF6B6B">
  If you encounter an error like:

  ```bash theme={null}
  Error: accumulating resources: accumulation err='accumulating resources from 'db/': 
  couldn't make target for path '...': json: cannot unmarshal number into Go struct field Image.images.newTag of type string
  ```

  ensure that the tag value is quoted so that it is processed as a string.
</Callout>

After applying the image transformation, the final rendered output for the database deployment will display the updated container image:

```yaml theme={null}
...
containers:
  - name: mongo
    image: postgres:4.2
...
```

Other image references remain unchanged.

## Conclusion

In this lesson, we demonstrated various techniques to modify Kubernetes configurations using Kustomize transformations. The key topics covered include:

* Importing resources using kustomization.yaml files
* Applying common labels and annotations globally and at the folder level
* Setting namespaces for resource segmentation
* Modifying resource names with prefixes and folder-specific suffixes
* Transforming container images using the image transformer

In the next section, you will have the opportunity to participate in a lab exercise to gain hands-on experience with these Kustomize transformations and further refine your Kubernetes configurations.

For more detailed information on Kubernetes configuration management, visit the [Kubernetes Documentation](https://kubernetes.io/docs/).

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/031e84b8-bcbc-4f39-94d6-66d93b05bddc/lesson/263d84a1-1c9d-40e7-afa3-9a7457c499cd" />

  <Card title="Practice Lab" icon="installation" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/031e84b8-bcbc-4f39-94d6-66d93b05bddc/lesson/2b58089b-bdb2-49bb-ac98-e1a8799c5526" />
</CardGroup>
