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

# Imperative Commands

> This guide explains how to imperatively update `kustomization.yaml` using `kustomize edit` subcommands via the command line interface.

In this guide, you’ll learn how to **imperatively** update your `kustomization.yaml` using the `kustomize edit` subcommands. Anything you normally declare in YAML—adding or removing labels, prefixes, namespaces, modifying images or replica counts—can also be done directly via the CLI.

Before you start, check the help output for available subcommands:

```bash theme={null}
kustomize edit --help
```

You’ll see usage examples and commands such as:

```bash theme={null}
# Adds a ConfigMap to the kustomization file
kustomize edit add configmap NAME --from-literal=k=v

# Sets the nameprefix field
kustomize edit set nameprefix <prefix-value>

# Sets the namesuffix field
kustomize edit set namesuffix <suffix-value>
```

## Key Subcommands

| Subcommand    | Action                                |
| ------------- | ------------------------------------- |
| `edit set`    | Modify existing fields                |
| `edit add`    | Add generators, resources, or plugins |
| `edit remove` | Remove items from the kustomization   |

***

## edit set

Use `kustomize edit set` to change fields in your `kustomization.yaml` without editing it by hand.

### Change an image tag

```bash theme={null}
kustomize edit set image nginx=nginx:1.2.2
```

Resulting entry:

```yaml theme={null}
images:
- name: nginx
  newTag: 1.2.2
```

### Set or update a namespace

```bash theme={null}
kustomize edit set namespace staging
```

Produces:

```yaml theme={null}
namespace: staging
```

<Callout icon="lightbulb" color="#1CB2FE">
  This command only updates your `kustomization.yaml`. It does **not** apply changes to your cluster.
</Callout>

### Add common labels

```bash theme={null}
kustomize edit set label org=KodeKloud env=staging
```

Yields:

```yaml theme={null}
commonLabels:
  org: KodeKloud
  env: staging
```

### Set replica count for a Deployment

```bash theme={null}
kustomize edit set replicas nginx-deployment=5
```

Adds:

```yaml theme={null}
replicas:
- name: nginx-deployment
  count: 5
```

***

## edit add

The `add` subcommands let you generate ConfigMaps, Secrets, and include additional resources.

### Add a ConfigMap generator

```bash theme={null}
kustomize edit add configmap db-creds \
  --from-literal=password=password1 \
  --from-literal=username=root
```

Generates:

```yaml theme={null}
configMapGenerator:
- name: db-creds
  literals:
  - password=password1
  - username=root
```

### Add a Secret generator

```bash theme={null}
kustomize edit add secret my-secret \
  --from-literal=login=user1 \
  --from-literal=password=mypassword
```

Produces:

```yaml theme={null}
secretGenerator:
- name: my-secret
  type: Opaque
  literals:
  - login=user1
  - password=mypassword
```

### Add a resource

Include external YAML manifests in your kustomization:

```bash theme={null}
kustomize edit add resource db/db-depl.yaml
```

Results in:

```yaml theme={null}
resources:
- db/db-depl.yaml
```

***

## Which `kustomization.yaml` Gets Updated?

If your project uses multiple overlays or bases, the file modified depends on where you run the command:

```bash theme={null}
cd k8/base
kustomize edit set label org=KodeKloud
cd ../overlays/prod
kustomize edit set namespace prod
# Updates k8/overlays/prod/kustomization.yaml
```

If there’s no `kustomization.yaml` in your current directory, you’ll see an error.

***

## Links and References

* [Kustomize Documentation](https://kubectl.docs.kubernetes.io/references/kustomize/)
* [Kubernetes Configuration](https://kubernetes.io/docs/concepts/overview/config-management/)
* [GitHub: kustomize](https://github.com/kubernetes-sigs/kustomize)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/kustomize/module/060e95ac-e56c-42ed-be87-8701328432c3/lesson/e18c8a87-99db-439e-a8bc-bff7cc658853" />

  <Card title="Practice Lab" icon="installation" cta="Learn more" href="https://learn.kodekloud.com/user/courses/kustomize/module/060e95ac-e56c-42ed-be87-8701328432c3/lesson/f83fd542-9130-4e60-8e67-ed2d7548d895" />
</CardGroup>
