Skip to content

fix(k8s): split the network policies whose egress skips their ingress - #6721

Merged
dan-petty merged 9 commits into
mainfrom
fix/6708-split-dual-type-netpols
Nov 22, 2026
Merged

dan-petty merged 9 commits into
mainfrom
fix/6708-split-dual-type-netpols

Conversation

@dan-petty

@dan-petty dan-petty commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

kube-router, the network policy controller of the cluster, does not apply the ingress rules of a NetworkPolicy with the types Ingress and Egress to traffic that an egress rule of that policy allows between two pods that it selects. By the same chain, an ingress rule can let those pods skip their egress rules.

Each policy with both types is now <name>-ingress and <name>-egress, with the same pod selector and rules: 23 policies in 13 namespaces, devops included. The in-namespace egress of gitea-default-perimeter-egress gets TCP 3000.

The rule dual_type_problems of the k8s row refuses each policy whose types, as the API server sets them, hold Ingress and Egress. No policy of k8s/ has both types. k8s/README.md has a section NetworkPolicy.

Closes #6708

Change: openspec/changes/archive/2026-11-22-6708-split-dual-type-netpols/

@dan-petty dan-petty added type/bug Type bug: the code does not do what it must scope/k8s Kubernetes commands, Helm charts, pods, services, and policies labels Nov 22, 2026
@dan-petty

dan-petty commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner Author

Live probes after Argo CD syncs main

Do these after the merge and the sync. The pull request applies nothing to the cluster.

  1. The old policies are gone, and no policy has both types (criterion 2, in the form of the refined design: no policy has both types):

    kubectl get networkpolicy -A -o json | jq -r '.items[] | select((.spec.policyTypes | index("Ingress")) and (.spec.policyTypes | index("Egress"))) | .metadata.namespace + "/" + .metadata.name'
    # expected: no output
  2. A pod that the ingress rules do not admit cannot connect to a limited port (criterion 3). Run each probe on the node of the server pod and on a second node:

    probe() { # <namespace> <node> <address> <port> [labels]
      kubectl -n "$1" run "netpol-probe-$RANDOM" --rm -i --restart=Never --image=busybox:1.37.0 \
        ${5:+--labels="$5"} --overrides="{\"spec\":{\"nodeName\":\"$2\"}}" -- nc -z -w 3 "$3" "$4"
      echo "exit=$?"
    }
    ip() { kubectl -n "$1" get pod -l "$2" --field-selector=status.phase=Running -o jsonpath='{.items[0].status.podIP}'; }
    node() { kubectl -n "$1" get pod -l "$2" --field-selector=status.phase=Running -o jsonpath='{.items[0].spec.nodeName}'; }
    kubectl create namespace netpol-probe   # no policy: the probe pod sends everywhere
    
    # In-namespace probes (the path that the split closes). Expected: exit=1, except the control.
    probe gitea "$(node gitea app.kubernetes.io/name=gitea)" "$(ip gitea app.kubernetes.io/name=gitea)" 2222
    probe gitea "$(node gitea app.kubernetes.io/name=gitea)" "$(ip gitea app.kubernetes.io/name=gitea)" 3000   # control: exit=0
    probe llm <node> "$(ip llm app.kubernetes.io/name=valkey)" 6379
    probe llm <node> "$(ip llm cnpg.io/cluster=llm-gateway-db)" 5432
    probe llm <node> "$(ip llm app.kubernetes.io/name=valkey-devops)" 6379
    probe object-store <node> "$(ip object-store app.kubernetes.io/component=admin)" 33646
    probe object-store <node> "$(ip object-store app.kubernetes.io/component=volume)" 9327
    probe cnpg-system <node> "$(ip cnpg-system app.kubernetes.io/name=plugin-barman-cloud)" 9090
    
    # Probes from another namespace. Expected: exit=1.
    probe netpol-probe <node> "$(ip argocd app.kubernetes.io/name=argocd-redis)" 6379
    probe netpol-probe <node> "$(ip logging app.kubernetes.io/name=loki)" 3100
    probe netpol-probe <node> "$(ip mattermost app.kubernetes.io/name=mattermost)" 8065
    probe netpol-probe <node> "$(ip monitoring app.kubernetes.io/name=grafana)" 3000
    probe netpol-probe <node> "$(ip otel app.kubernetes.io/name=tempo)" 3200
    probe netpol-probe <node> "$(ip argo-workflows app.kubernetes.io/name=argo-workflows-workflow-controller)" 9090
    probe netpol-probe <node> "$(ip cert-manager app.kubernetes.io/name=cert-manager)" 9402
    probe netpol-probe <node> "$(ip cloudflared app.kubernetes.io/name=cloudflared)" 2000
    probe netpol-probe <node> "$(ip devops app.kubernetes.io/name=crawl4ai)" 11235
    probe netpol-probe <node> "$(ip devops app.kubernetes.io/name=searxng)" 8080
    
    kubectl delete namespace netpol-probe

    Check each label selector with kubectl -n <namespace> get pods -l <selector> first: a selector that matches no pod gives an empty address. In devops, uv run devops k8s crawl4ai-check and uv run devops k8s searxng-check also exit 0.

  3. Each Application of the changed namespaces stays Synced and Healthy:

    for app in argocd argocd-image-updater base devops argo-workflows argo-workflows-manifests cert-manager cloudnative-pg plugin-barman-cloud gitea gitea-manifests llm open-webui qdrant logging loki mattermost monitoring grafana k8s-monitoring prometheus pyroscope dcgm-exporter prometheus-operator-crds object-store seaweedfs-operator otel otel-collector tempo; do
      uv run devops argo cd apps status "$app"
    done

Person steps

  • Send the private report of the controller behavior to the private vulnerability reporting of kube-router and to the security contact of k3s (issue, Proposed remedy).
  • A cluster without Argo CD: devops k8s deploy-stack applies the new policies and does not delete the old ones. Delete each old policy there: kubectl -n <namespace> delete networkpolicy <old name>. The Argo CD Applications have prune: true, so the homelab sync deletes them.

Open points for review

  1. Scope: each policy with both types. The row refuses each NetworkPolicy whose types, as the API server sets them, hold Ingress and Egress (SetDefaults_NetworkPolicy: with no policyTypes, Ingress, and Egress when the policy has an egress rule). The first form of the row refused only an egress rule that reaches the own pods on a port that no ingress rule admits from them. It was about 120 lines, it did not cover the other direction, and it could miss a case. So the 4 policies of devops (devops-default-perimeter, crawl4ai-perimeter, renovate-perimeter, searxng-perimeter) are split too, and no policy of k8s/ has both types. The 4 policies of argo-workflows, cert-manager, cloudflared and cnpg-system are split as before: their egress ipBlock 0.0.0.0/0 holds the pod addresses, which kube-router matches (k8s/llm/gateway/networkpolicy.yaml, fix(k8s): deploy-stack applies the llm gateway policy and narrows its egress #795).
  2. Criterion 2 of the issue body. The query of the Problem section ("an egress rule whose only peer is a podSelector and that has no ports") still finds the Egress-only policies of argocd, llm (default), logging, mattermost, monitoring and otel. That is by the item notes: their in-namespace ingress rules admit each pod of the namespace on each port, so no exact port list exists, and single-type policies are enforced correctly. Probe 1 above is the query of the refined design, and it prints nothing.
  3. Where the split changes a path. In argocd, logging, mattermost, monitoring, otel and for the pods of the llm default perimeter, the old policy already admitted each pod of the namespace on each port, so nothing changes there. The issue examples argocd-redis and grafana-image-renderer-ingress did not limit those pods, because policies add up. valkey-perimeter, valkey-devops-perimeter, valkey-runs-perimeter and llm-gateway-db-perimeter were not bypassed: the llm default perimeter does not select those pods. In devops, the 4 policies send only to other namespaces and to public addresses, and each ingress peer is a pod that the same policy does not select, so nothing changes there. The real changes: in gitea, the pods reach each other on TCP 3000 only; in llm, a gateway pod no longer reaches another gateway pod on 4000, and a Portkey pod no longer reaches another Portkey pod on 8787. Neither workload calls itself through its Service (k8s/llm/gateway/config.yaml, k8s/llm/values-open-webui.yaml). No path that the workloads use today is cut.
  4. The other direction. By the same chain, an ingress rule of a policy with both types can let the pods that it selects skip their own egress rules. The row now refuses each policy with both types, so it covers this direction too.
  5. Value tests. tests/k8s/test_k8s_argo_workflows.py, tests/ai/benchmark/test_k8s_llm_gateway.py, tests/ai/test_ai_gateway_portkey.py and tests/server/routes/test_k8s_roadmap_service.py read manifest values. I changed only the names and the document lookup, so they pass. They stay for the test(k8s): Drop the tests that repeat third-party version pins #6060 cleanup.
  6. feat(argocd): read the environment from the homelab repo #5134. The item merges before plan step 10 of feat(argocd): read the environment from the homelab repo #5134, so it stays in this repository.
  7. Sandbox policies. to_k8s_network_policy in src/devops_cli/sandbox/models.py still makes a policy with both types for each sandbox. The policy is made at run time and is not in k8s/, so the row does not read it. This pull request does not change it.

#6708)

The egress policy of the llm default perimeter allows every destination
on every port. The test read only the first policy, so a Valkey that the
egress policy selected was not found. The test now reads both policies
by name.
@dan-petty

Copy link
Copy Markdown
Owner Author

Correction to the live probes comment above: the k8s row now refuses each policy with both types, so the 4 policies of devops (devops-default-perimeter, crawl4ai-perimeter, renovate-perimeter, searxng-perimeter) are split too. After the sync, probe 1 prints no policy. The old comment said that the 4 devops policies stay with both types; that is not true now. I edited the comment: probe 1, two devops probes in probe 2, the Application devops in probe 3, and open points 1, 3, 4 and 7.

@dan-petty

Copy link
Copy Markdown
Owner Author

Correction to the live-probe comments: do not use the probe helper as written

An independent review found that the in-namespace probes can report a false pass:

  • kubectl run --image=busybox:1.37.0 with no securityContext is refused by admission in the namespaces that enforce Pod Security restricted (gitea, object-store, cnpg-system). kubectl then exits 1, which looks like the expected reject.
  • Nothing listens on gitea TCP 2222 (DISABLE_SSH: true), so a refused connect and a policy reject both give exit 1.

The coordinator runs the probes after the merge and the sync, with a restricted pod spec (runAsNonRoot, drop ALL, seccomp RuntimeDefault, the form of src/devops_cli/k8s/service_probe.py), a control probe to an admitted port for each namespace, and targets that have a listener. The results go on #6708.

@dan-petty

Copy link
Copy Markdown
Owner Author

The private upstream report (a person step of this change) is tracked in #6725, so it stays open after this PR closes #6708. The sandbox policies that the code makes at run time are #6726, and the old error of the SearXNG spec is #6727.

@dan-petty
dan-petty marked this pull request as ready for review November 22, 2026 02:45
@dan-petty
dan-petty merged commit 235fea0 into main Nov 22, 2026
32 checks passed
@dan-petty
dan-petty deleted the fix/6708-split-dual-type-netpols branch November 22, 2026 02:46
dan-petty pushed a commit that referenced this pull request Nov 22, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.192.0](v0.191.1...v0.192.0)
(2026-11-22)


### Features

* **k8s:** apply the Application path builds in deploy-stack
([#6713](#6713))
([d905dbd](d905dbd))
* **k8s:** render a credential-free planner ConfigMap
([#6723](#6723))
([745108e](745108e))


### Bug Fixes

* **k8s:** split the network policies whose egress skips their ingress
([#6721](#6721))
([235fea0](235fea0))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope/k8s Kubernetes commands, Helm charts, pods, services, and policies type/bug Type bug: the code does not do what it must

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(k8s): an in-namespace egress allow skips the ingress rules

1 participant