# Add Kubernetes NetworkPolicies to a Multi-Service App

Yes. An agent reads which services talk to which in your Kubernetes manifests and code, writes default-deny NetworkPolicies with the exact allows, and opens a PR.

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

## What the agent does

The agent maps the traffic before it writes a policy. In a repository like dockersamples/example-voting-app, `k8s-specifications/` holds nine files: Deployments and Services for `db` (`postgres:15-alpine` on 5432), `redis` (`redis:alpine` on 6379), `vote` and `result` (each a container on port 80 behind a `NodePort` Service on 31000 and 31001), and a `worker` Deployment with no Service and no ports. Every pod is labelled only `app: <name>`, and no namespace is set, so everything lands in whatever namespace `kubectl apply` targets. There is no `NetworkPolicy` in the directory, so every pod can reach every other.

Who calls whom is in the code, not the manifests. `vote/app.py` opens `Redis(host="redis")`, so `vote` needs egress to `redis` on 6379 and nothing else. `worker/Program.cs` connects to `Server=db` and resolves `redis` with `Dns.GetHostEntryAsync`, so `worker` needs egress to both and to the cluster DNS. `result/server.js` connects with `postgres://postgres:postgres@db/postgres`, so `result` needs `db` on 5432. `docker-compose.yml` already encodes the same split with its `front-tier` and `back-tier` networks, which the agent uses as a cross-check: `vote` and `result` sit on both tiers, `worker`, `redis` and `db` on the back tier only.

From that it writes `k8s-specifications/network-policies.yaml` in the same label vocabulary. A `default-deny` policy selects every pod and sets both `Ingress` and `Egress` policy types. An `allow-dns` policy lets every pod reach `kube-dns` in `kube-system` on UDP and TCP 53, because the first thing `worker` does is a DNS lookup. `redis` admits ingress on 6379 from pods labelled `app: vote` or `app: worker`; `db` admits 5432 from `app: worker` and `app: result`. `vote` and `result` admit ingress on port 80 with no `from` restriction, because the `NodePort` Services bring traffic from outside the cluster whose source the policy cannot name. Egress rules mirror the ingress ones, so a compromised `vote` pod cannot reach `db` at all. Every policy is commented with the file and line that justifies it.

Checking happens in two layers. The agent validates every manifest against the Kubernetes schema with `kubeconform -strict`, including the existing nine files, and renders a table of allowed flows from the policies to compare with the map it drew from the code. The sandbox has no Docker, so an enforced end-to-end test is opt-in: on request it installs `docker.io`, starts `dockerd`, creates a `kind` cluster with the default CNI disabled and Calico installed, because kind's own CNI does not enforce NetworkPolicy, applies the directory and casts a vote through the `vote` Service to show it is counted by `worker` and shown by `result`. The default run stops at validation and says so.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add Kubernetes NetworkPolicies to this application. Read every file under k8s-specifications/, docker-compose.yml, and the code that opens connections in vote/app.py, worker/Program.cs and result/server.js, and write down which pod talks to which on which port. Create k8s-specifications/network-policies.yaml with a default-deny policy for ingress and egress on every pod, an allow-dns policy to kube-dns in kube-system on UDP and TCP 53, and one policy per service that admits exactly the callers and ports the code uses, with ingress on port 80 for the two NodePort services left open to any source. Use the existing app labels, comment each rule with the file that justifies it, validate all manifests with kubeconform in strict mode, and open a pull request that includes the flow table. Do not start a cluster unless asked.",
  "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 adding `k8s-specifications/network-policies.yaml` and a short section in the README on applying it and on the DNS rule. The nine existing manifests are unchanged, so the Deployments and Services roll out exactly as before and only the policies are new.

The description carries the flow table, one row per source, destination and port, each with the source file that proves it, and the `kubeconform` output for the whole directory. The commands and their output are in the transcript at `GET /o/{org}/sessions/{id}/messages`.

Before merging, a reviewer confirms their cluster's CNI enforces NetworkPolicy, since on one that does not the file is accepted and ignored, checks the `kube-dns` label matches their DNS deployment, and applies the file to a staging namespace first. A follow-up message to the same session can run the kind-with-Calico test or add a policy for a monitoring scraper the team runs.

## Run it on every Linear issue labelled security

A team that tracks hardening work in Linear starts one run per issue from an automation with a `linear` trigger on the `security` label. The automation adds the issue after this prompt, so an issue that names a namespace, an ingress controller or a metrics scraper that must keep access is what the agent reads, and the run starts in the repository the automation names.

Each run opens its own pull request from its own sandbox, and an automation run has its own `max_runs_per_hour` cap. The pull request links the issue and carries the flow table, so the review is a comparison of that table with what the team knows the app does.

## Why run it on Rush?

A NetworkPolicy that is one port short takes a service down quietly, and the information needed to get it right is scattered across manifests, Compose files and three languages of application code. The agent reads all of it in one sandbox, writes the policies from that evidence, and validates every manifest against the schema before anything is applied.

The pull request carries the flow table with its sources and the validation output, so a reviewer checks the reasoning rather than re-deriving it. Several applications can be hardened at once, each in its own sandbox, with one pull request each.

## Frequently asked questions

### Why is there a DNS policy?

With a default-deny egress rule, pods can no longer resolve Service names, and `worker/Program.cs` resolves `redis` by name before it connects. The `allow-dns` policy admits UDP and TCP 53 to pods labelled `k8s-app: kube-dns` in `kube-system`, which is the label the standard CoreDNS deployment carries; the pull request says to adjust it if a cluster labels DNS differently.

### Why are vote and result open to any source on port 80?

Both are exposed through `NodePort` Services, and traffic arriving that way has a source the policy cannot usefully name. The policy admits port 80 and nothing else, which still blocks anything reaching those pods on another port. A cluster with an ingress controller can narrow the `from` clause to that controller's namespace in a follow-up.

### Does this change the existing manifests?

No. The policies select pods by the `app` labels the Deployments already have, so the nine existing files are untouched and the new file can be applied or deleted independently.

### Will the policies actually be enforced?

Only on a cluster whose network plugin implements NetworkPolicy. Calico, Cilium and most managed clusters' default plugins do; kind's default CNI does not, which is why the optional end-to-end check installs Calico. The pull request states this so nobody assumes protection on a cluster that silently ignores the objects.

## More Kubernetes jobs

- [Add Pod Disruption Budgets to Kubernetes Manifests from a Linear Issue](https://getrush.ai/run/add-pod-disruption-budgets/kubernetes/)
- [Add Pre-commit Hooks to a Kubernetes Manifest Repository](https://getrush.ai/run/add-pre-commit-hooks/kubernetes/)
- [Add Priority Classes to Kubernetes Manifests](https://getrush.ai/run/add-priority-classes/kubernetes/)

## The same job on other stacks

- [Add NetworkPolicies to a Kustomize App](https://getrush.ai/run/add-network-policies/kustomize/)
- [Add Network Policies to a GKE Guestbook App](https://getrush.ai/run/add-network-policies/gke/)

## More jobs like this: Turn a Linear label into a PR

- [Add a NetworkPolicy Option to a Helm Chart](https://getrush.ai/run/add-network-policy/helm/)

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