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

# Kustomize Controller

> The Kustomize Controller ensures cluster state matches desired Kubernetes manifests by validating, applying, monitoring, and optionally pruning resources.

The **Kustomize Controller** is the Flux component that ensures your cluster’s actual state matches the desired state described by Kubernetes manifests. It retrieves artifacts from the Source Controller and then:

| Feature                      | Description                                                                 |
| ---------------------------- | --------------------------------------------------------------------------- |
| Validation                   | Checks your manifests against the Kubernetes API before applying changes.   |
| Apply / Update               | Creates or updates resources based on the desired state.                    |
| Health Assessment            | Monitors workloads to ensure they remain healthy after deployment.          |
| Pruning (Garbage Collection) | Optionally removes resources that no longer exist in your source manifests. |

<Callout icon="lightbulb" color="#1CB2FE">
  Make sure you have installed [Flux](https://fluxcd.io/docs/installation/) and configured a `GitRepository` or other supported source.
</Callout>

## 1. Creating a Kustomization

Use the `flux create kustomization` command to link your source to a path containing Kustomize overlays. You can also enable garbage collection:

```bash theme={null}
flux create kustomization kustomization-app1 \
  --source=GitRepository/source-app1 \
  --path=./solar-system \
  --prune=true \
  --export > kustomization.yaml
```

The `--export` flag prints the `Kustomization` resource manifest:

```yaml theme={null}
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
  name: kustomization-app1
  namespace: flux-system
spec:
  interval: 1m0s
  path: ./solar-system
  prune: true
  sourceRef:
    kind: GitRepository
    name: source-app1
```

Apply it with:

```bash theme={null}
kubectl apply -f kustomization.yaml
```

## 2. Automatic Build Behavior

If there’s no `kustomization.yaml` file under `./solar-system`, Flux will auto-generate one for you:

```bash theme={null}
kustomize create --autodetect --recursive --output kustomization.yaml
```

This populates `kustomization.yaml` with all discovered resources:

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

<Callout icon="lightbulb" color="#1CB2FE">
  You don’t need to run the above command yourself—Flux performs it automatically when needed.
</Callout>

## 3. Providing Your Own Overlays

If you include a custom `kustomization.yaml` (or Kustomization overlay) in the specified path, the controller will skip auto-generation and apply your overlays directly. This allows you to:

* Add labels or annotations
* Patch existing resources
* Customize namespace or image tags

## 4. Checking Status

To inspect all active `Kustomization` resources and their reconciliation state:

```bash theme={null}
flux get kustomizations
```

Example output:

```plaintext theme={null}
NAME                   REVISION          SUSPENDED   READY   MESSAGE
flux-system            main/7e35674...   False       True    Applied revision: 'main/7e35674...'
kustomization-app1     main/1b31558...   False       True    Applied revision: 'main/1b31558...'
```

## Links and References

* [Flux Documentation](https://fluxcd.io/docs/)
* [Kustomize Official Site](https://kubectl.docs.kubernetes.io/references/kustomize/)
* [GitRepository Source for Flux](https://fluxcd.io/docs/components/source/git/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/gitops-with-fluxcd/module/857e34cf-a086-433b-bf3b-88a5a5096a6f/lesson/ffda282a-fad8-47c2-912e-38b034592ed9" />
</CardGroup>
