> ## 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.

# Patches list

> Learn to modify Kubernetes Deployment containers by replacing, adding, and deleting items using JSON 6902 and strategic merge patches while maintaining YAML hierarchy.

In this article, you'll learn how to modify containers within a Kubernetes Deployment by performing operations on list items. We cover how to replace, add, and delete items from the container list using both JSON 6902 patches and strategic merge patches. Every example maintains the correct YAML hierarchy and list indexing to ensure a seamless configuration update.

Below is the base deployment configuration used in all examples:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: api
  template:
    metadata:
      labels:
        component: api
    spec:
      containers:
        - name: nginx
          image: nginx
```

──────────────────────────────

## 1. Replacing a Container Using a JSON 6902 Patch

In this first example, we update the container’s name and image from "nginx" to "haproxy". Since the container section is defined as a list, we specifically target the first container (index 0) in the patch path. Remember that list indexes start at zero.

The following JSON 6902 patch replaces the container at index 0:

```yaml theme={null}
patches:
  - target:
      kind: Deployment
      name: api-deployment
    patch: |-
      - op: replace
        path: /spec/template/spec/containers/0
        value:
          name: haproxy
          image: haproxy
```

Kustomize will apply this patch to update the first (and only) container of the Deployment.

<Callout icon="lightbulb" color="#1CB2FE">
  Ensure you use the correct index when working with lists in your YAML configurations.
</Callout>

──────────────────────────────

## 2. Replacing a Container Using a Strategic Merge Patch

An alternative approach is to update the container using a strategic merge patch. In this example, we update the image of an existing container. Verify that the Deployment configuration and the patch file refer to the same container name. Note that a minor typo ("ngin" instead of "nginx") has been corrected for clarity.

The base deployment remains as:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: api
  template:
    metadata:
      labels:
        component: api
    spec:
      containers:
        - name: nginx
          image: nginx
```

The strategic merge patch, defined in a file (e.g., `label-patch.yaml`), looks like this:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  template:
    spec:
      containers:
        - name: nginx
          image: haproxy
```

In your `kustomization.yaml`, reference the patch file:

```yaml theme={null}
patches:
  - label-patch.yaml
```

When merged, this patch updates the container named "nginx", changing its image to "haproxy".

──────────────────────────────

## 3. Adding an Item to a List Using a JSON 6902 Patch

To add another container to your Deployment, use the JSON patch with an "add" operation. The patch path ends with a dash (-) to signal that the new container should be appended to the end of the list. Although indexing (e.g., 0 for the beginning) is possible, appending is achieved easily using the dash notation.

The base deployment configuration is as follows:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: api
  template:
    metadata:
      labels:
        component: api
    spec:
      containers:
        - name: nginx
          image: nginx
```

The JSON 6902 patch to add a new container is:

```yaml theme={null}
patches:
  - target:
      kind: Deployment
      name: api-deployment
    patch: |-
      - op: add
        path: /spec/template/spec/containers/-
        value:
          name: haproxy
          image: haproxy
```

After applying this patch, the Deployment will include both the original "nginx" container and the newly added "haproxy" container.

──────────────────────────────

## 4. Adding an Item to a List Using a Strategic Merge Patch

You can also add a container by merging configuration files. In this approach, the original Deployment configuration is combined with a patch file. In this example, the initial Deployment includes a placeholder container named "web" with the image "nginx":

```yaml theme={null}
# api-depl.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: api
  template:
    metadata:
      labels:
        component: api
    spec:
      containers:
        - name: web
          image: nginx
```

The patch file (e.g., `label-patch.yaml`) adds an additional container:

```yaml theme={null}
# label-patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  template:
    spec:
      containers:
        - name: haproxy
          image: haproxy
```

Reference the patch in your `kustomization.yaml`:

```yaml theme={null}
# kustomization.yaml
patches:
  - label-patch.yaml
```

After applying the merge, the Deployment will feature two containers: one named "web" using the image "nginx" and another named "haproxy" using the image "haproxy".

<Callout icon="lightbulb" color="#1CB2FE">
  Using strategic merge patches lets you combine multiple configuration files effortlessly while keeping your Deployment definitions clean and modular.
</Callout>

──────────────────────────────

## 5. Deleting an Item from a List Using a JSON 6902 Patch

If you need to remove a container from your Deployment, such as one named "database", a JSON 6902 patch can be used to delete it by specifying its index in the list. In the example below, the "web" container is at index 0, and the "database" container is at index 1.

The original configuration:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: api
  template:
    metadata:
      labels:
        component: api
    spec:
      containers:
        - name: web
          image: nginx
        - name: database
          image: mongo
```

The JSON 6902 patch to remove the container at index 1 is:

```yaml theme={null}
patches:
  - target:
      kind: Deployment
      name: api-deployment
    patch: |-
      - op: remove
        path: /spec/template/spec/containers/1
```

After applying this patch, the "database" container will be removed, leaving only the "web" container in your Deployment.

──────────────────────────────

## 6. Deleting an Item from a List Using a Strategic Merge Patch

Removing a container using a strategic merge patch is achieved by adding a delete directive in your patch file. In this example, we remove the container with the name "database".

The starting configuration is:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: api
  template:
    metadata:
      labels:
        component: api
    spec:
      containers:
        - name: web
          image: nginx
        - name: database
          image: mongo
```

The strategic merge patch file (e.g., `label-patch.yaml`) includes the `$patch: delete` directive:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  template:
    spec:
      containers:
        - $patch: delete
          name: database
```

Add this patch to your `kustomization.yaml`:

```yaml theme={null}
patches:
  - label-patch.yaml
```

After merging, Kustomize removes the container named "database", leaving only the "web" container in the final configuration.

<Callout icon="triangle-alert" color="#FF6B6B">
  Double-check list indexes and container names when applying deletion patches to avoid removing the wrong configuration.
</Callout>

──────────────────────────────

## Summary

By using JSON 6902 patches and strategic merge patches, you can precisely modify list elements in your Kubernetes configurations. The methods discussed include:

| Operation | Patch Type          | Use Case                                    | Example File Reference |
| --------- | ------------------- | ------------------------------------------- | ---------------------- |
| Replace   | JSON 6902/Strategic | Update container name and image             | See Sections 1 & 2     |
| Add       | JSON 6902/Strategic | Append new container to the Deployment list | See Sections 3 & 4     |
| Delete    | JSON 6902/Strategic | Remove a container based on index or name   | See Sections 5 & 6     |

These techniques allow you to flexibly manage your container configurations. For further reading on Kubernetes patches, visit the [Kubernetes Documentation](https://kubernetes.io/docs/).

By understanding and applying these strategies, maintaining complex deployments becomes more efficient and error-free. Happy patching!

<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/5e018d72-ca12-47c0-9816-5bde98ec4ca5" />

  <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/919f7a3a-8ba1-4c84-a363-2108c51fff1d" />
</CardGroup>
