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

> Learn to update, add, and remove keys in Kubernetes deployment configurations using JSON 6902 and strategic merge patches with step-by-step examples.

In this lesson, you'll learn how to update, add, and remove keys in a Kubernetes deployment configuration using both JSON 6902 patches and strategic merge patches. The step-by-step examples provided below will help you understand the process and decide which patching method best fits your configuration management needs.

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

## Updating a Key with a JSON 6902 Patch

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

Assume you have a deployment configuration with a label `component: api` that needs to be updated to `component: web`. First, consider the original deployment 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: nginx
          image: nginx
```

To update this label using a JSON 6902 patch, add the following patch to your `kustomization.yaml`:

```yaml theme={null}
patches:
  - target:
      kind: Deployment
      name: api-deployment
    patch: |-
      - op: replace
        path: /spec/template/metadata/labels/component
        value: web
```

<Callout icon="lightbulb" color="#1CB2FE">
  The patch uses the "replace" operation to update the existing key at the specified path (`/spec/template/metadata/labels/component`) with the new value `web`.
</Callout>

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

## Updating a Key with a Strategic Merge Patch

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

You can achieve the same update using a strategic merge patch. Instead of including the patch inline, store it in a separate file named `label-patch.yaml` and reference it in your `kustomization.yaml`.

In your `kustomization.yaml`, include:

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

Then, create `label-patch.yaml` with only the fields that need to be updated:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  template:
    metadata:
      labels:
        component: web
```

Kustomize will merge this patch with the original configuration, updating only the `component` label value from `api` to `web`.

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

## Adding a New Key Using a JSON 6902 Patch

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

Suppose you want to add a new label `org: KodeKloud` to the existing deployment configuration. Consider the following original configuration:

```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: nginx
          image: nginx
```

Now, add the following JSON 6902 patch in your `kustomization.yaml`:

```yaml theme={null}
# kustomization.yaml
patches:
  - target:
      kind: Deployment
      name: api-deployment
    patch: |-
      - op: add
        path: /spec/template/metadata/labels/org
        value: KodeKloud
```

In this patch:

• The **add** operation instructs Kubernetes to introduce a new key.\
• The **path** defines where the new label should be inserted (`/spec/template/metadata/labels/org`).\
• The **value** `KodeKloud` is the new label value.

After applying the patch, the final configuration will include both labels—`component: api` and `org: KodeKloud`:

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

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

## Adding a New Key with a Strategic Merge Patch

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

To add a new label using a strategic merge patch, reference an external patch file. In your `kustomization.yaml`, include:

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

Then, in the `label-patch.yaml` file, specify the new key:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  template:
    metadata:
      labels:
        org: kodekloud
```

Kustomize will merge this patch with the original configuration, resulting in the addition of the new `org` label.

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

## Removing a Key Using a JSON 6902 Patch

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

Consider a deployment that currently has two labels: `org: KodeKloud` and `component: api`. Here is the existing configuration:

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

To remove the `org` label, apply the following JSON 6902 patch:

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

This patch uses the "remove" operation to delete the `org` label. After merging, only the `component: api` label remains.

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

## Removing a Key with a Strategic Merge Patch

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

With a strategic merge patch, you can remove a key by setting its value to `null`. Starting with the same configuration as above, reference an external patch file in your `kustomization.yaml`:

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

Then, in `label-patch.yaml`, mark the `org` label for removal:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  template:
    metadata:
      labels:
        org: null
```

Kustomize merges this patch to remove the `org` label, leaving only the `component: api` label in the configuration.

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

## Conclusion

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

This lesson demonstrated how to update (replace), add, and remove keys in your Kubernetes deployment configurations using both JSON 6902 patches and strategic merge patches. Choose the method that aligns best with your workload and configuration complexity. For further information, explore the [Kubernetes Documentation](https://kubernetes.io/docs/).

Use these techniques to maintain clean, efficient deployment configurations in your Kubernetes environment.

<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/045b04b8-d4bc-42a1-8c50-dfb934e67cce" />
</CardGroup>
