# Add Pod and Container Security Contexts to Kustomize Manifests

Yes. An agent reads every Deployment your kustomization renders, adds non-root users, dropped capabilities, read-only roots and seccomp as patches, opens a PR.

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

## What the agent does

The agent starts by rendering what the cluster would get, because a kustomization can hide a lot in patches and bases. In a repository like argoproj/argocd-example-apps, `kustomize build sock-shop` renders fourteen Deployments listed one by one under `resources:` in `sock-shop/kustomization.yaml`, from `base/carts-dep.yaml` to `base/user-svc.yaml`, and `kustomize build kustomize-guestbook` renders one, `guestbook-ui`, with a `namePrefix: kustomize-`. It then reads each container spec for what is already there. Most of sock-shop is in good shape: `front-end`, `carts`, `orders`, `catalogue`, `payment`, `shipping`, `user` and the databases carry a `securityContext` with `runAsNonRoot: true`, `runAsUser: 10001`, `capabilities.drop: [all]` and `readOnlyRootFilesystem: true`, and `carts` adds back `NET_BIND_SERVICE` because it listens on port 80 and mounts an `emptyDir` on `/tmp` so the read-only root still has somewhere to write.

The gaps are specific. `base/queue-master-dep.yaml` and `base/catalogue-db-dep.yaml` have no `securityContext` at all, so they run as whatever their images default to, with a writable root filesystem and the full capability set. Not one of the fifteen Deployments sets `seccompProfile`, `allowPrivilegeEscalation` or a pod-level `securityContext`, and `kustomize-guestbook/guestbook-ui-deployment.yaml` runs `gcr.io/google-samples/gb-frontend:v5` on port 80 with nothing but a `containerPort`. The agent does not guess at what each image needs: it pulls `weaveworksdemos/queue-master:0.3.1`, `weaveworksdemos/catalogue-db:0.3.0` and the guestbook image in the sandbox, after installing `docker.io` and starting `dockerd`, and inspects their `USER`, exposed ports and the paths they write to, which is how it learns that a database needs a writable data volume and a Java service needs `/tmp`.

The change is written as patches, not edits to the base, so the overlay says what policy it enforces. In `sock-shop/kustomization.yaml` a `patches:` entry with a `target` of `kind: Deployment` applies a strategic merge that sets a pod-level `securityContext` with `seccompProfile.type: RuntimeDefault` and `runAsNonRoot: true`, and on every container `allowPrivilegeEscalation: false` and `capabilities.drop: [ALL]`; `queue-master` and `catalogue-db` then get their own named patches for `runAsUser`, `readOnlyRootFilesystem: true` and the `emptyDir` mounts the image inspection showed they need, in the shape `carts-dep.yaml` already uses. `kustomize-guestbook/kustomization.yaml` gets the same pod-level patch plus `NET_BIND_SERVICE` for the port-80 frontend. The agent keeps the `base/` files untouched, so the plain `guestbook/` and `helm-guestbook/` variants in the same repository are unaffected.

The proof is a render and a run. The agent runs `kustomize build` on both directories before and after and diffs the output so the pull request shows exactly which fields were added to which pod. It validates the rendered manifests with `kubeconform` against the Kubernetes schema, and checks them against the restricted Pod Security Standard with `kyverno apply` and the published `restricted` policy set, which reports each remaining violation by resource and field. Then it starts a `kind` cluster on the Docker it installed, applies the rendered sock-shop, and waits for every pod to reach `Running` with no `CrashLoopBackOff`, which is the only way to know that `readOnlyRootFilesystem` did not break the queue-master's Java runtime or the database's startup.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add pod and container security contexts to the Kustomize applications in this repository. Read every kustomization.yaml and every Deployment it references first, render each with kustomize build, and list which containers already set runAsNonRoot, runAsUser, capabilities, readOnlyRootFilesystem, allowPrivilegeEscalation and seccompProfile and which do not. Install docker.io, start dockerd, and pull and inspect the images of the containers that lack a security context to learn their user, ports and writable paths. Add patches in each kustomization.yaml rather than editing the base files: a pod-level securityContext with seccompProfile RuntimeDefault and runAsNonRoot on every Deployment, allowPrivilegeEscalation false and capabilities drop ALL on every container, and per-Deployment patches for runAsUser, readOnlyRootFilesystem and emptyDir mounts where the image needs them, adding back NET_BIND_SERVICE only for containers that bind port 80. Diff the rendered output before and after, validate it with kubeconform, check it against the restricted Pod Security Standard with kyverno apply, then start a kind cluster, apply the rendered manifests and wait for every pod to be Running. Open a pull request with the render diff, the policy report and the pod status.",
  "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 on its own branch that changes only `sock-shop/kustomization.yaml` and `kustomize-guestbook/kustomization.yaml`, adding a `patches:` section to each, plus the small patch files they reference if the agent kept them out of line for readability. No file under `sock-shop/base/` is touched, so the diff is the policy and nothing else.

The description shows the `kustomize build` diff per Deployment, the `kubeconform` result, the `kyverno apply` report before and after with each restricted-standard violation named and then cleared, and the `kubectl get pods` output from the `kind` cluster with every pod `Running`. The transcript at `GET /o/{org}/sessions/{id}/messages` keeps the image inspection that justified each `emptyDir` and each `runAsUser`.

Before merging, a reviewer reads the two per-Deployment patches, since those encode what the images need to run read-only, and decides whether the `runAsUser` the agent chose for `catalogue-db` matches how that database image is run elsewhere. The pod status from `kind` is the evidence that the change starts; a production rollout still goes through Argo CD as usual.

## Run it on every terminal hand-off

A platform engineer who has been asked to meet the restricted Pod Security Standard by a date starts this from the terminal with this body and the manifests repository in `resources`, then goes back to the cluster upgrade. The run renders the real overlays and inspects the real images, so the patches match what each workload can tolerate rather than a blanket template.

Send a `request_id`, a UUID, with the body so a retried start returns the run it already began. When the turn ends, `GET /o/{org}/sessions/{id}` names the pull request, and a follow-up to the same session can extend the same patches to the Helm chart variants in the repository.

## Why run it on Rush?

Security contexts are easy to write and hard to get right blind: a read-only root filesystem breaks a Java service that writes to `/tmp`, a non-root user breaks a database image that expects to own its data directory, and dropping `ALL` capabilities breaks a frontend that binds port 80. Finding that out on a cluster, one rollout at a time, is the slow way. The agent finds it out in a sandbox by inspecting the images and starting the rendered manifests on `kind`, and keeps the evidence with the pull request.

The result is a policy expressed as Kustomize patches, checked against the restricted standard by `kyverno`, validated by `kubeconform` and shown running, so the reviewer reads a diff and a pod list rather than repeating the experiment. Several manifest repositories can be done at once, each in its own sandbox with its own pull request.

## Frequently asked questions

### Why patches instead of editing the base Deployments?

Because the base files in this repository are shared by more than one overlay and the policy belongs to the overlay. A `patches:` entry targeting `kind: Deployment` states the rule once, applies it to every Deployment the kustomization renders, including ones added later, and leaves `sock-shop/base/` readable as the plain application. The per-Deployment patches for `queue-master` and `catalogue-db` sit beside it with a comment on why each exists.

### What if a container cannot run read-only or as non-root?

The image inspection and the `kind` run show that before the pull request is opened. For a container that writes to a path, the agent adds an `emptyDir` mount there, as `carts` already does for `/tmp`. For an image that must run as root, it keeps `readOnlyRootFilesystem` and the dropped capabilities, leaves `runAsNonRoot` off that one Deployment, and says so in the description with the reason, so the exception is visible rather than silent.

### Does this make the namespace pass the restricted Pod Security Standard?

The `kyverno apply` report in the description says exactly which checks pass after the change. Seccomp, non-root, no privilege escalation and dropped capabilities are the fields the restricted profile requires, and the patches set all of them; `NET_BIND_SERVICE` is the one capability the restricted profile allows adding back, which is why the port-80 containers get it and nothing else.

### Why does the agent need Docker in the sandbox?

To inspect the images and to run `kind`. The sandbox does not ship Docker, so the agent installs `docker.io` with apt, starts `dockerd`, pulls the images whose containers lacked a security context, and creates a `kind` cluster to apply the rendered manifests. Both steps are in the transcript.

## More Kustomize jobs

- [Add Prometheus ServiceMonitors to a Kustomize App](https://getrush.ai/run/add-service-monitors/kustomize/)
- [Add a Staging Overlay with Kustomize from a Linear Issue](https://getrush.ai/run/add-staging-overlay/kustomize/)
- [Fix "# Warning: 'bases' is deprecated. Please use 'resources' instead" Across a Kustomize Repository](https://getrush.ai/run/fix-kustomize-bases-is-deprecated/kustomize/)

## The same job on other stacks

- [Add Pod and Container Security Contexts to a Helm Chart](https://getrush.ai/run/add-security-context/helm/)

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

- [Add Security Headers to an Actix Web App](https://getrush.ai/run/add-security-headers/actix/)
- [Add Security Headers to an aiohttp App](https://getrush.ai/run/add-security-headers/aiohttp/)

## 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/)
