Visitar URL original
NetworkPolicy tests for blocking north/south traffic · Issue #114369 · kubernetes/kubernetes · GitHub
Skip to content

NetworkPolicy tests for blocking north/south traffic #114369

Description

@danwinship

The NP docs point out that NP's semantics for north/south traffic are not very clearly defined:

Cluster ingress and egress mechanisms often require rewriting the source or destination IP of packets. In cases where this happens, it is not defined whether this happens before or after NetworkPolicy processing, and the behavior may be different for different combinations of network plugin, cloud provider, Service implementation, etc.

In the case of ingress, this means that in some cases you may be able to filter incoming packets based on the actual original source IP, while in other cases, the "source IP" that the NetworkPolicy acts on may be the IP of a LoadBalancer or of the Pod's node, etc.

For egress, this means that connections from pods to Service IPs that get rewritten to cluster-external IPs may or may not be subject to ipBlock-based policies.

However, what's not ambiguous is that a pod which is fully isolated for ingress should not accept E/W or N/S traffic. Regardless of how the ingress/LB/cloud handles the traffic, it should end up getting blocked. (Well, unless it gets masqueraded to the pod's node IP, because then it hits that exception to the rules.)

Unfortunately we can't say the same for egress; I think we assume that a pod-to-service-IP connection will be allowed to reach kube-proxy even if the pod is fully isolated-for-egress, but we explicitly don't require that ipBlock policies get applied after service proxying.

Anyway, we should be able to add a test to test/e2e/network/netpol/network_policy.go that confirms that cluster-ingress traffic to a fully isolated-for-ingress pod is not allowed. In particular, if we create a LoadBalancer Service with externalTrafficPolicy: Local, and a NetworkPolicy blocking all ingress to that Service's Pods, then we should not be able to connect to the service via either the LoadBalancer IP/name or via its NodePort (on one of the correct nodes).

/sig network
/area network-policy
/priority backlog
/help
/good-first-issue

Activity

  1. k8s-ci-robot commented on Dec 8, 2022

    @k8s-ci-robot
    Contributor

    @danwinship:
    This request has been marked as suitable for new contributors.

    Guidelines

    Please ensure that the issue body includes answers to the following questions:

    • Why are we solving this issue?
    • To address this issue, are there any code changes? If there are code changes, what needs to be done in the code and what places can the assignee treat as reference points?
    • Does this issue have zero to low barrier of entry?
    • How can the assignee reach out to you for help?

    For more details on the requirements of such an issue, please see here and ensure that they are met.

    If this request no longer meets these requirements, the label can be removed
    by commenting with the /remove-good-first-issue command.

    Details

    In response to this:

    The NP docs point out that NP's semantics for north/south traffic are not very clearly defined:

    Cluster ingress and egress mechanisms often require rewriting the source or destination IP of packets. In cases where this happens, it is not defined whether this happens before or after NetworkPolicy processing, and the behavior may be different for different combinations of network plugin, cloud provider, Service implementation, etc.

    In the case of ingress, this means that in some cases you may be able to filter incoming packets based on the actual original source IP, while in other cases, the "source IP" that the NetworkPolicy acts on may be the IP of a LoadBalancer or of the Pod's node, etc.

    For egress, this means that connections from pods to Service IPs that get rewritten to cluster-external IPs may or may not be subject to ipBlock-based policies.

    However, what's not ambiguous is that a pod which is fully isolated for ingress should not accept E/W or N/S traffic. Regardless of how the ingress/LB/cloud handles the traffic, it should end up getting blocked. (Well, unless it gets masqueraded to the pod's node IP, because then it hits that exception to the rules.)

    Unfortunately we can't say the same for egress; I think we assume that a pod-to-service-IP connection will be allowed to reach kube-proxy even if the pod is fully isolated-for-egress, but we explicitly don't require that ipBlock policies get applied after service proxying.

    Anyway, we should be able to add a test to test/e2e/network/netpol/network_policy.go that confirms that cluster-ingress traffic to a fully isolated-for-ingress pod is not allowed. In particular, if we create a LoadBalancer Service with externalTrafficPolicy: Local, and a NetworkPolicy blocking all ingress to that Service's Pods, then we should not be able to connect to the service via either the LoadBalancer IP/name or via its NodePort (on one of the correct nodes).

    /sig network
    /area network-policy
    /priority backlog
    /help
    /good-first-issue

    Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

  2. added
    sig/networkCategorizes an issue or PR as relevant to SIG Network.
    good first issueDenotes an issue ready for a new contributor, according to the "help wanted" guidelines.
    area/network-policyIssues or PRs related to Network Policy subproject
    priority/backlogHigher priority than priority/awaiting-more-evidence.
    help wantedDenotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.
    needs-triageIndicates an issue or PR lacks a `triage/foo` label and requires one.
    on Dec 8, 2022
  3. added
    triage/acceptedIndicates an issue or PR is ready to be actively worked on.
    and removed
    needs-triageIndicates an issue or PR lacks a `triage/foo` label and requires one.
    on Dec 8, 2022
  4. MohnishMishra commented on Dec 28, 2022

    @MohnishMishra

    Hello, I am Mohnish Mishra. I am a 2nd year student in BTech in Computer Science and Engineering.
    I am new to open-source coding and trying understand the same.
    I am interested in contributing to this project.
    If you could please guide me on what is to be done and what are the pre-requisites for the same, it will be of great help.

  5. sftim commented on Dec 29, 2022

    @sftim
    Contributor

    @danwinship this feels to me like an issue that (right now) justifies the help label, but maybe not good-first-issue. WDYT?

  6. astoycos commented on Jan 2, 2023

    @astoycos
    Contributor

    This has been on my TODO list, and I'm hoping to either get to it early in the new year or have a member of the network policy api subgroup tackle this. @MohnishMishra Feel free to come to that meeting to learn more :) [Note we are changing the time this year see this pr and slack conversation for more information]

    But yeah @sftim I'd say this is slightly above good first issue :)

  7. farahInTheZone commented on Jan 3, 2023

    @farahInTheZone

    Hey. If this is not being actively worked on, I would like to pick this up.

  8. MohnishMishra commented on Jan 4, 2023

    @MohnishMishra

    Thank you @astoycos.
    I have joined the mailing group and hope to join the meetings and learn.
    Thank you for the opportunity.

  9. 47 remaining items

  10. kunalworldwide commented on May 26, 2026

    @kunalworldwide

    @danwinship before I open a new PR — @aroradaman raised whether this should go in network-policy-api instead of test/e2e/network/netpol/network_policy.go. This is a v1 NetworkPolicy test so my assumption is it stays in netpol/, but wanted to check before putting work into the wrong place.

    Also want to confirm I'm reading your latest comments right: the test should create a pod with deny-all-ingress, expose it via NodePort with externalTrafficPolicy: Local, connect from the e2e host (not from inside a pod), and just fail if the connection succeeds. No SNAT detection needed. Should I include a LoadBalancer variant too or keep it narrow for now?

  11. danwinship commented on May 26, 2026

    @danwinship
    ContributorAuthor

    @kunalworldwide there is already a PR open adding a test for this. It just hasn't merged yet.

  12. kunalworldwide commented on Jun 29, 2026

    @kunalworldwide

    @danwinship good to know — is the open PR #137390, or a different one? I saw #137390 was closed earlier; if there is a replacement PR it would be useful to link it here so others do not duplicate the work. Happy to help review or test if it is stuck on something.

  13. danwinship commented on Jun 29, 2026

    @danwinship
    ContributorAuthor

    There have been multiple PRs opened at various points; you can see them all in the sidebar on the right. One of them is currently open.

  14. kunalworldwide commented on Jul 15, 2026

    @kunalworldwide

    @danwinship thanks — I see the open PR now. Is it stuck on anything specific (review bandwidth, rebase, or a design question)? Happy to help review or run the e2e tests if that unblocks it.

  15. added 10 commits that reference this issue on Aug 31, 2026
    c47de13
    26fc39e
    5705278
    f043edc
    7de6fab
    f796ce5
    245e938
    9c3b2d0
    87fee81
    2fe970d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/network-policyIssues or PRs related to Network Policy subprojectgood first issueDenotes an issue ready for a new contributor, according to the "help wanted" guidelines.help wantedDenotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.priority/backlogHigher priority than priority/awaiting-more-evidence.sig/networkCategorizes an issue or PR as relevant to SIG Network.triage/acceptedIndicates an issue or PR is ready to be actively worked on.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions