> For the complete documentation index, see [llms.txt](https://dinush-chathurya.gitbook.io/kustomize-e-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dinush-chathurya.gitbook.io/kustomize-e-book/additional-topics/17-cli-tools-for-kustomize.md).

# CLI Tools for Kustomize

The Kustomize CLI and its ecosystem of plugins and helper tools are essential for building, validating, and automating your Kubernetes manifests. In this chapter, we’ll explore the most important commands, plugins, and tips to streamline your Kustomize workflows.

***

## 1. `kubectl kustomize` vs `kustomize build`

| Command                   | Description                                                                   |
| ------------------------- | ----------------------------------------------------------------------------- |
| `kubectl kustomize <dir>` | Built-in since Kubernetes v1.14; renders manifests without a separate binary. |
| `kustomize build <dir>`   | The standalone Kustomize binary; supports the latest features and plugins.    |

**Tip:** Use `kubectl kustomize` for quick local tests; use `kustomize build` in CI to ensure feature parity and plugin support.

***

## 2. Core CLI Commands

* **`kustomize build [dir]`**\
  Render all resources in the directory.
* **`kustomize edit set image OLD=NEW`**\
  Update image references in your `kustomization.yaml`.
* **`kustomize cfg`** subcommands
  * `kustomize cfg check` — validate your resource configurations.
  * `kustomize cfg fmt` — format your Kustomize files.
  * `kustomize cfg test` — run YAML-based tests.
* **`kustomize version`**\
  Shows the client version; important for reproducibility.

***

## 3. Plugin Ecosystem

Kustomize supports three plugin types—**Generators**, **Transformers**, and **Executables**—to extend functionality:

```plaintext
~/.config/kustomize/plugin/
├── yourdomain.com/
│   ├── generators/
│   │   └── mygen/
│   │       └── mygen.go
│   └── transformers/
│       └── mytrans/
│           └── mytrans.go
```

* **Generators** produce resources (e.g., CRDs).
* **Transformers** modify existing resources (e.g., add labels).
* **Exec Plugins** invoke arbitrary executables to generate or transform.

Tip: Vendor plugins alongside your repo and pin Kustomize versions to avoid incompatibility.

## 4. Linting & Validation

### kustomize doctor \[dir]

Diagnose issues in your Kustomize configuration (e.g., missing resources).

### kubeval / kube-linter

Validate rendered YAML against Kubernetes schemas and best practices:

```bash
kustomize build overlays/prod | kubeval --strict
kustomize build overlays/prod | kube-linter lint -
```

### Open Policy Agent (OPA) + Conftest

Write Rego policies to enforce security or compliance rules on generated manifests:

```bash
kustomize build overlays/prod | conftest test -
```

***

## 5. Scripting & Automation Tips

### Makefiles

```makefile
build-dev:
    kustomize build overlays/dev > deploy/dev.yaml
deploy-dev:
    kubectl apply -f deploy/dev.yaml
```

### Shell Scripts

Wrap multi-step commands:

```bash
#!/usr/bin/env bash
set -e
kustomize build overlays/$1 | kubectl apply -f -
```

### CI Caching

Cache the `$HOME/.config/kustomize/plugin` directory and Docker images generated by plugins.

***

## 6. Version Management

### Homebrew / apt / yum packages for the standalone binary.

### GitHub Releases: Pin to a specific release tag:

```bash
curl -s "https://api.github.com/repos/kubernetes-sigs/kustomize/releases/latest"   | jq -r '.assets[] | select(.name|test("linux_amd64")) | .browser_download_url'   | xargs curl -L -o kustomize
chmod +x kustomize && mv kustomize /usr/local/bin/
```

### Docker Image:

```bash
docker run --rm sigs.k8s.io/kustomize/kustomize:v4.5.7 build overlays/dev
```

***

## 7. Performance & Scaling

* `--load-restrictor=none` when loading remote bases (use with caution).
* Limit resources in large repos by splitting into “apps of apps.”
* Parallelize builds with GNU parallel or CI matrix jobs.

***

## 8. Best Practices

* Pin CLI versions in CI to avoid surprises.
* Vendor plugins and test them in isolated environments.
* Integrate linters and policy checks early in your pipeline.
* Document your Makefile or scripts in the repo’s `README.md`.
* Use `kubectl diff` (`kubectl apply --server-dry-run`) to preview changes before applying.

***

## Summary

Mastering the Kustomize CLI and its ecosystem of plugins and tools enables you to build robust, automated pipelines that handle complex configurations at scale. By leveraging validation, scripting, and best practices, you’ll ensure reliable, reproducible, and secure Kubernetes deployments.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://dinush-chathurya.gitbook.io/kustomize-e-book/additional-topics/17-cli-tools-for-kustomize.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
