# Add NetworkPolicies to a Kustomize App

Yes. An agent reads your Kustomize base, maps which Deployment talks to which Service and port, writes default-deny NetworkPolicies with exact allows, in a PR.

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

## What the agent does

The agent reads the kustomization and every resource it lists. In an app like the `sock-shop/` directory of argoproj/argocd-example-apps, `kustomization.yaml` lists 29 files under `base/`: fourteen Deployments, fourteen Services and one Ingress, with no namespace, no `commonLabels` and no NetworkPolicy anywhere in the repository. Every pod is labelled only `name: <service>`, and the Services select on that label. The ports come straight from the manifests: `front-end` listens on 8079 behind a Service on 80; `carts`, `catalogue`, `orders`, `payment`, `queue-master`, `shipping` and `user` listen on 80; `carts-db` and `orders-db` run the `mongo` image and `user-db` a `weaveworksdemos/user-db` image, all on 27017; `catalogue-db` is MySQL on 3306; `session-db` is `redis:alpine` on 6379; and `rabbitmq` exposes 5672, a management port 15672 that no Service publishes, and a `rabbitmq-exporter` sidecar on 9090 that the Service does publish. `front-end-ingress.yaml` uses `ingressClassName: nginx` and routes `sockshop.apps.argoproj.io` to the `front-end` Service.

Some edges are written down and some are not. `user-dep.yaml` sets `MONGO_HOST` to `user-db:27017`; `front-end-dep.yaml` sets `SESSION_REDIS` to `true`, so it needs `session-db`; `carts`, `orders`, `shipping` and `queue-master` carry a `ZIPKIN` variable pointing at `zipkin.jaeger.svc.cluster.local`, an egress to another namespace. The other calls, front-end to the four APIs, orders to payment, shipping, user and carts, carts to carts-db, catalogue to catalogue-db and the two queue users to rabbitmq, are not in any environment variable: they are how the Sock Shop images are built. The agent takes those from the application's architecture, marks each one as inferred rather than read, and puts the whole table, source, destination, port and where the evidence came from, in the pull request so a reviewer can strike a row.

It then writes `base/network-policies.yaml` and adds it to `kustomization.yaml`: a default-deny policy for both `Ingress` and `Egress` selecting every pod, an egress allow to `kube-dns` on UDP and TCP 53 for all of them, and one policy per service with only its rows from the table. `front-end` accepts from the ingress controller's namespace on 8079 and may reach `carts`, `catalogue`, `orders` and `user` on 80 and `session-db` on 6379; each database accepts only from its one owner; `rabbitmq` accepts 5672 from `shipping` and `queue-master` and 9090 from the monitoring namespace, with the management port left closed. The four `ZIPKIN` services get an egress allow to the `jaeger` namespace with the port a reviewer must confirm, since the manifests give only a hostname. Namespace selectors use `kubernetes.io/metadata.name`, so the policies work without labelling namespaces by hand.

The proof is in two layers. The agent runs `kustomize build sock-shop` and checks with `kubeconform` that every rendered object, old and new, validates, and diffs the rendering against the original so the pull request shows only additions. Then, because a NetworkPolicy is only as good as the CNI that enforces it, it installs Docker in the sandbox (`sudo apt-get install -y docker.io`, then starts `dockerd`), creates a `kind` cluster with Calico, applies the rendered output, and runs connectivity checks from small pods carrying the same `name` labels: `front-end` to `catalogue` on 80 must connect, `front-end` to `user-db` on 27017 must time out, anything to `rabbitmq` on 15672 must time out. It does not pull the Sock Shop images; the labels and ports are what the policies see.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add Kubernetes NetworkPolicies to the Kustomize app in this repository. Read kustomization.yaml and every Deployment, Service and Ingress it lists first. Build a traffic table from the manifests: container ports, Service ports, environment variables that name another service, and the ingress controller in front of the frontend, and complete it from the application's own architecture for calls the manifests do not name, marking each row as read or inferred. Write base/network-policies.yaml with a default-deny policy for ingress and egress on all pods, an egress allow for DNS, and one policy per workload that allows only the rows in the table, using kubernetes.io/metadata.name namespace selectors for the ingress controller, monitoring and tracing namespaces; leave ports no Service publishes closed. Add the file to the kustomization. Render with kustomize build, validate with kubeconform, and diff against the current rendering to show only additions. Then install Docker, create a kind cluster with a policy-enforcing CNI, apply the rendering, and prove with labelled test pods that an allowed path connects and two denied paths time out. Open a pull request with the traffic table, the validation output and the connectivity results, and change nothing else.",
  "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 adds one file, `base/network-policies.yaml`, and one line to `kustomization.yaml`. No Deployment, Service or Ingress is modified, and the `kustomize build` diff in the description shows only new `NetworkPolicy` objects.

The description leads with the traffic table: each row names the source, the destination, the port and whether it was read from a manifest, taken from an environment variable or inferred from the application, so the review is a comparison of that table with what the team knows the app does. Under it are the `kubeconform` summary and the connectivity results from the kind cluster, with the pod labels and ports used for each check. Every command, from the kustomize build to each `kubectl exec`, is in the session transcript at `GET /o/{org}/sessions/{id}/messages`.

Before merging, a reviewer confirms the Zipkin port and the names of the ingress controller and monitoring namespaces in their cluster, since the manifests do not carry them, and checks the rows marked inferred against the services' real configuration. With Argo CD syncing this path, merging applies the policies on the next sync; a reviewer who wants a staged rollout can ask, in a follow-up message to the same session, for the default-deny policy to be split into a second overlay that is enabled after the per-service allows have been live for a day.

## Run it on every Linear issue labelled security

A team that files hardening work in Linear starts one run per issue from an automation with a `linear` trigger on the `security` label. The trigger adds the issue to the prompt after this text, so an issue that names the ingress controller's namespace, the tracing port or a scraper that must keep reaching `rabbitmq` on 9090 becomes part of what the agent reads, and the run starts in the repository the automation names.

Each run has its own sandbox, its own kind cluster and its own pull request, and an automation run has its own `max_runs_per_hour` cap. The pull request links back to the issue, so the traffic table and the connectivity results land where the request was made.

## Why run it on Rush?

Getting a NetworkPolicy wrong is quiet: a missing allow for the session store makes every login fail an hour after the sync, and nothing in `kubectl apply` says so. The agent builds the policies from the manifests it read, then stands up a cluster with an enforcing CNI in its sandbox and watches an allowed path connect and a denied path time out before it asks for a review. That is a check most teams skip on a laptop because it means Docker, kind, a CNI install and a half hour.

The run keeps every rendering, validation and connectivity command with its output, so the pull request's table is backed by evidence rather than by a diagram from memory. A platform team with several Kustomize apps can send one request per repository at once, each with its own cluster, and get one pull request per app written from that app's own manifests.

## Frequently asked questions

### How does the agent know orders calls payment if no manifest says so?

It does not know it from the manifests, and it says so. Rows that come from a container port, a Service port or an environment variable are marked read; rows that come from how the application's images are built are marked inferred. The reviewer confirms or strikes the inferred rows, and the connectivity checks cover the read ones.

### Will this break Prometheus scraping the RabbitMQ exporter?

Not if the monitoring namespace is the one the policy names. The `rabbitmq` Service publishes the exporter on 9090, so the policy allows that port from the monitoring namespace by its `kubernetes.io/metadata.name` label and closes the 15672 management port, which no Service publishes. The pull request asks the reviewer to confirm the namespace name.

### Does this change the Argo CD application?

No. The policies are one more resource in the same kustomization, so the existing Application renders and syncs them with everything else. Nothing in `apps/` or the Argo CD configuration is touched.

### What if our cluster's CNI does not enforce NetworkPolicy?

Then the objects apply and do nothing, which the pull request warns about. The kind cluster the agent tests on uses a CNI that enforces them so the checks mean something; on a cluster without one, the policies are documentation until the CNI is changed.

## More Kustomize jobs

- [Add Pod Disruption Budgets to a Kustomize App](https://getrush.ai/run/add-pod-disruption-budgets/kustomize/)
- [Add Resource Requests and Limits to a Kustomize App from a Linear Issue](https://getrush.ai/run/add-resource-limits/kustomize/)
- [Add Pod and Container Security Contexts to Kustomize Manifests](https://getrush.ai/run/add-security-context/kustomize/)

## The same job on other stacks

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

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