# Add Kyverno Policies to a Kubernetes Manifests Repository

Yes. An agent writes Kyverno policies in Audit mode for what your manifests already do, tests them with the Kyverno CLI against every manifest, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-kyverno-policies/kubernetes/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the repository into its own cloud sandbox and reads what the manifests already promise before writing a rule. In a multi-service app like GoogleCloudPlatform/microservices-demo, `kubernetes-manifests/` holds one file per service plus `kustomization.yaml`, and every Deployment there sets `runAsNonRoot: true`, `readOnlyRootFilesystem: true`, `allowPrivilegeEscalation: false`, drops `ALL` capabilities and declares CPU and memory requests and limits. The same services appear under `kustomize/base/` with optional components such as `network-policies` and `non-public-frontend`, and `release/kubernetes-manifests.yaml` is the rendered release where images are pinned to a version tag and `busybox` to a digest. One exception stands out: `loadgenerator.yaml` has no readiness or liveness probe, while every other service has both. The images in `kubernetes-manifests/` are bare names like `frontend` that skaffold rewrites, which is why the existing `kubevious-manifests-ci.yaml` workflow skips its latest-image rule there.

It turns those observations into a `policies/` directory of `ClusterPolicy` files, one concern each, with `failureAction: Audit` on every rule and `background: true`: require `runAsNonRoot`, require a read-only root filesystem, disallow privilege escalation, require all capabilities dropped, require requests and limits on every container, and require readiness and liveness probes. A seventh policy, disallow a missing or `latest` image tag, lives in its own `policies/release/` directory and is applied only to the rendered release file, because the development manifests are rewritten at build time. For the loadgenerator the agent writes a `PolicyException` resource that exempts that one Deployment from the probe rule, with the reason in its message, so the gap is recorded rather than silently waved through. Each policy gets a `kyverno-test.yaml` with a passing and a failing resource, the second a copy of a real manifest with the field removed.

Then it wires the check into CI the way the repository already runs its other manifest checks. A new `.github/workflows/kyverno-policies-ci.yaml` triggers on paths under `kubernetes-manifests/`, `kustomize/`, `release/` and `policies/`, installs the Kyverno CLI, runs `kyverno test policies/`, then `kyverno apply policies/ --resource kubernetes-manifests/ --exception policies/exceptions/loadgenerator-probes.yaml --table`, the same over a file rendered with `kubectl kustomize kustomize/` so the components are covered, and finally `kyverno apply policies/ policies/release/ --resource release/kubernetes-manifests.yaml --table` so the tag rule runs where images are pinned. The job gets `permissions: contents: read` and a `timeout-minutes` like the neighbouring `kubevious-manifests-ci.yaml`.

Before opening anything it runs the same commands in its sandbox: the CLI from the Kyverno GitHub release, `kyverno test` over every policy directory, and `kyverno apply` over the three resource sets. The expected result is a table where every rule passes except the probe rule on loadgenerator, which the exception then clears, and the agent pastes that table into the pull request so the reviewer sees the policies agree with the manifests before any cluster enforces them.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add Kyverno policies to this Kubernetes repository. Read every file under kubernetes-manifests/, kustomize/base/, kustomize/components/, release/ and .github/workflows/ first, and list which pod security settings, resource settings and probes every Deployment already has and which ones are missing. Write one ClusterPolicy per concern under policies/, each rule with failureAction Audit and background true, for runAsNonRoot, read-only root filesystem, no privilege escalation, dropped capabilities, requests and limits, and readiness and liveness probes, plus an image tag policy scoped so it never runs on manifests whose images are rewritten at build time. Where a workload legitimately misses a rule, write a PolicyException with the reason instead of weakening the rule. Give each policy a kyverno-test.yaml with a passing and a failing resource. Add a GitHub Actions workflow that installs the Kyverno CLI, runs kyverno test, and runs kyverno apply against the manifests directory, the rendered release file and the output of kubectl kustomize. In your sandbox install the CLI, run both commands, and open a pull request that lists the files added, the results table, and every exception with its reason.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request adding `policies/` with seven `ClusterPolicy` files, a `policies/exceptions/` directory with one `PolicyException`, a `kyverno-test.yaml` beside each policy, and one workflow file. The description lists each policy and what it checks, the one exception and why it exists, and the `kyverno apply` table from the sandbox with a pass count per rule.

The run's record holds the CLI install, the `kyverno test` output and the three `kyverno apply` runs, so a reviewer can see which real manifest each failing test case was copied from and that the development manifests were never checked for image tags.

Before merging, a reviewer decides two things the pull request leaves open: whether the loadgenerator should get probes instead of an exception, and which policies move from `Audit` to `Enforce` once a cluster with Kyverno installed has shown clean reports for a while.

## Run it on every new Kubernetes repository

A platform team that wants the same baseline on every repository starts one run per repository. A handler on the team's own server receives the organisation's repository webhook from GitHub, checks the repository contains a manifests directory, and sends this request with that repository's URL in `resources`. Each run reads the manifests it is given, so a repository that already drops capabilities everywhere gets a passing table and a repository that does not gets failing cases that name the file.

A run started this way lands in its own sandbox and opens its own pull request. The key that starts it reports each finished run to its `webhook_url`, so the handler can post the results table where the team reads it.

## Why run it on Rush?

Policy as code is only trustworthy if the policies are tested against the manifests they will govern, and that means a CLI, a rendered kustomize output and a results table, not a YAML file written from memory. The agent does that work in a sandbox with the real manifests in front of it, so the pull request arrives with every rule already shown passing or failing against each service, and the one gap recorded as an exception with a reason.

The same request runs against every manifests repository an organisation has, each in its own sandbox, each producing policies that match what that repository actually does. The full command record stays with the run, so a reviewer reads the table and the diff together and spends their time on the two decisions only they can make.

## Frequently asked questions

### Why Audit rather than Enforce?

In `Audit` a violation is written to a policy report and the resource is still admitted, so a policy that turns out to be wrong cannot block a deploy. The pull request starts there and names the policies the team can switch to `Enforce` once reports have been clean.

### Does this need Kyverno installed in a cluster?

Not to review or merge it. The Kyverno CLI applies policies to files, which is what the workflow and the sandbox run do. Installing Kyverno in a cluster is a separate step that makes the same policies run at admission time.

### Why is the image tag policy not applied to the development manifests?

Those manifests use bare image names that the build tool replaces with a tagged image at deploy time, so a tag rule would fail on every file for no reason. The policy sits in its own directory and the workflow applies it only to the rendered release, where every image carries a version tag or a digest.

### What is the exception for?

One workload, the load generator, has no probes and arguably does not need them. A `PolicyException` records that decision with its reason next to the policies, so the rule stays strict for every other Deployment and the gap is visible in the repository rather than hidden in a weaker rule.

### Will it slow down pull requests?

The workflow only runs when manifests, kustomize files, the release file or the policies change, and the CLI checks a few dozen resources in seconds. It runs beside the existing kustomize build and manifest validation jobs rather than replacing them.

## More Kubernetes jobs

- [Add Liveness and Readiness Probes to Kubernetes Deployments](https://getrush.ai/run/add-liveness-readiness-probes/kubernetes/)
- [Add Kubernetes NetworkPolicies to a Multi-Service App](https://getrush.ai/run/add-network-policies/kubernetes/)
- [Add Pod Disruption Budgets to Kubernetes Manifests from a Linear Issue](https://getrush.ai/run/add-pod-disruption-budgets/kubernetes/)

## The same job on other stacks

- [Add Kyverno Policies to a Flux Repository](https://getrush.ai/run/add-kyverno-policies/flux/)
- [Add Kyverno Policies to a GKE Deployment](https://getrush.ai/run/add-kyverno-policies/gke/)

## More jobs like this: Hand off a long task

- [Add Lambda Powertools to a Serverless Framework Node.js API](https://getrush.ai/run/add-lambda-powertools/serverless/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
