# Add Network Policies to a GKE Guestbook App

Yes. An agent writes default-deny and per-tier NetworkPolicy manifests for your GKE app's frontend and Redis pods, validates them offline, and opens a PR.

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

## What the agent does

The agent reads the labels before it writes a selector. In a GKE app like the `quickstarts/guestbook` sample in GoogleCloudPlatform/kubernetes-engine-samples, `frontend-deployment.yaml` runs three replicas of `us-docker.pkg.dev/google-samples/containers/gke/gb-frontend:v5` labelled `app: guestbook, tier: frontend` on port 80 with `GET_HOSTS_FROM=dns`, and `frontend-service.yaml` exposes it as a `LoadBalancer` on port 80. `redis-leader-deployment.yaml` runs one `docker.io/redis:6.0.5` labelled `app: redis, role: leader, tier: backend` on 6379 behind the `redis-leader` service, and `redis-follower-deployment.yaml` runs two `gb-redis-follower:v2` pods labelled `role: follower` behind `redis-follower`, which replicate from the leader. The `all-in-one/` directory repeats the same objects in combined files, `php-redis/` and `redis-follower/` hold the Dockerfiles and `cloudbuild.yaml` that publish the images, and nothing in the directory is a `NetworkPolicy` or a `Namespace`: every pod can talk to every pod. The agent also notes that both Redis deployments select only `app: redis` in `matchLabels` and reports it in the pull request as an overlap worth fixing separately, without changing it.

The agent adds `quickstarts/guestbook/network-policies/` with one file per intent and a `kustomization.yaml` listing them. `default-deny.yaml` selects every pod (`podSelector: {}`) with `policyTypes: [Ingress, Egress]`. `allow-dns-egress.yaml` lets every pod reach `kube-dns` in `kube-system` on UDP and TCP 53, because a deny-all egress policy without it breaks the `GET_HOSTS_FROM=dns` lookups the frontend relies on. `frontend-ingress.yaml` allows ingress to `tier: frontend` on TCP 80 from any `ipBlock`, since the LoadBalancer forwards external clients, and names the load-balancer health-check ranges Google documents so the reviewer can tighten the block without breaking health checks. `redis-ingress.yaml` allows ingress to `tier: backend` on TCP 6379 only from pods labelled `tier: frontend`, plus from `role: follower` pods to the leader for replication. `frontend-egress.yaml` allows egress from `tier: frontend` to `tier: backend` on 6379 and nothing else, and `redis-follower-egress.yaml` allows the followers to reach the leader on 6379, because the default deny covers egress too; the leader gets no egress beyond DNS.

Enforcement is the part that bites on GKE: a Standard cluster created without `--enable-network-policy` and without Dataplane V2 stores `NetworkPolicy` objects and ignores them, while Autopilot and Dataplane V2 clusters enforce them. The agent writes a `README.md` in the new directory that says this, gives the `gcloud container clusters update … --enable-network-policy` and `describe` commands to check, and lists the apply order: the app first, the policies after, with a verification from a frontend pod (`nc -zv redis-leader 6379` should connect) and from a throwaway `busybox` pod with no labels (the same should time out).

Validation happens without a cluster. The agent installs `kubectl` and `kubeconform` in its sandbox, runs `kubectl apply --dry-run=client -k network-policies/` and `kubeconform -strict` against the `networking.k8s.io/v1` schema, renders `kubectl kustomize network-policies/` and keeps the output. It does not create or touch a GKE cluster and says so. The deployments, services, `all-in-one/` files and the Cloud Build configs are untouched.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add Kubernetes NetworkPolicy manifests to this GKE app. Read every deployment and service manifest first and take the exact pod labels and ports from them. Write a default deny for ingress and egress, an egress allowance to kube-dns, an ingress rule for the public frontend on its container port from any address with the load balancer health-check ranges named for the reviewer, an ingress rule for the data tier only from the frontend tier plus replication between its own roles, and an egress rule from the frontend to the data tier on its port. Put them in one directory with a kustomization and a README that explains that GKE Standard clusters enforce policies only with network policy enablement or Dataplane V2, how to check, the apply order and how to verify from a pod. Report any overlapping deployment selectors you notice without changing them. Validate with kubectl dry run and kubeconform, render with kustomize, include the output, and do not touch any cluster. Open a pull request.",
  "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 that adds `quickstarts/guestbook/network-policies/` with `default-deny.yaml`, `allow-dns-egress.yaml`, `frontend-ingress.yaml`, `redis-ingress.yaml`, `frontend-egress.yaml`, `redis-follower-egress.yaml`, `kustomization.yaml` and a `README.md`. No existing manifest, Dockerfile or Cloud Build file is changed.

The description lists each policy with the selector and port it uses and the deployment it was read from, quotes the `kubectl --dry-run=client` and `kubeconform` results and the rendered output, states the enforcement prerequisite, and flags the overlapping `app: redis` selectors. The commands and full output are in the transcript at `GET /o/{org}/sessions/{id}/messages`.

Before merging, a reviewer confirms the cluster enforces policies, decides whether the frontend ingress should be narrowed to the health-check ranges and the load balancer's path, and applies the directory to a staging cluster to run the two `nc` checks. A follow-up message to the same session can add a `Namespace` and namespace selectors, or policies for the `all-in-one/` variant.

## Run it on every Kubernetes workload with no NetworkPolicy

A platform team starts one run per application directory or repository with one `POST /o/{org}/sessions` each, the repository in `resources` and this prompt as `input`. Each run reads its own labels and ports, so a three-tier app gets rules per tier and a single-pod app gets a default deny with one ingress rule.

Send a `request_id`, a UUID v5 of the repository's full name, so a retried call returns the run it already started. The key's `webhook_url` reports when each run the key starts finishes, with the run's final answer in `result`.

## Why run it on Rush?

Network policies fail quietly: a label typo selects nothing, a deny-all egress silently kills DNS, and on a GKE Standard cluster without enforcement the objects sit there doing nothing. The agent takes every selector and port from the deployments rather than typing them, adds the DNS rule a deny-all needs, validates against the API schema in a sandbox of its own, and writes the enforcement caveat where the reviewer will read it.

The pull request carries the policies, the validation output and the verification steps, so review is a diff and two `nc` commands on a staging cluster rather than a debugging session after an outage. Several applications can be done at once, each in its own sandbox with its own pull request.

## Frequently asked questions

### Will applying these break the app?

Not if the cluster enforces policies and the labels match, which is why every selector is copied from the deployments and the DNS egress rule is included. Apply to a staging cluster first and run the two `nc` checks from the README; the frontend should reach Redis and an unlabelled pod should not.

### Why does the frontend allow ingress from any address?

It is behind a `LoadBalancer` service that forwards external clients, and the health checks come from Google's documented ranges. The agent names those ranges in the file so you can narrow the `ipBlock` once you know how traffic reaches the nodes; narrowing blindly would make the backends unhealthy.

### Do the Redis followers still replicate?

Yes. The data-tier ingress rule allows `role: follower` pods to reach the leader on 6379 alongside the frontend, and a separate egress rule lets the followers open that connection, since the default deny blocks egress as well as ingress. Replication is checked with the same `nc` test from a follower pod.

### Does this work on Autopilot?

Yes. Autopilot clusters run Dataplane V2, which enforces `NetworkPolicy` without a flag. On Standard clusters check `gcloud container clusters describe` for `networkPolicy` or `datapathProvider: ADVANCED_DATAPATH` before relying on them.

## More GKE jobs

- [Add Pod Disruption Budgets to a GKE App](https://getrush.ai/run/add-pod-disruption-budgets/gke/)
- [Set Up Workload Identity on GKE with Terraform](https://getrush.ai/run/add-workload-identity/gke/)
- [Review Kubernetes Manifest Changes for GKE in Pull Requests](https://getrush.ai/run/review-kubernetes-manifests/gke/)

## The same job on other stacks

- [Add Kubernetes NetworkPolicies to a Multi-Service App](https://getrush.ai/run/add-network-policies/kubernetes/)
- [Add NetworkPolicies to a Kustomize App](https://getrush.ai/run/add-network-policies/kustomize/)

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

- [Add a Nix Flake to a Go CLI](https://getrush.ai/run/add-nix-flake/go-cli/)

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