Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify ports behavior in NetworkPolicy to and from blocks #49556

Open
krishnamanaiducloud opened this issue Jan 26, 2025 · 1 comment
Open
Labels
kind/feature Categorizes issue or PR as related to a new feature. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.

Comments

@krishnamanaiducloud
Copy link

What is the problem?
The current Kubernetes documentation does not explicitly mention that the ports field in the to and from blocks of a NetworkPolicyPeer is optional. This omission leads to confusion for users who may assume that omitting ports blocks traffic by default. Instead, the actual behavior is that omitting ports allows traffic to all ports for the specified destination(s).


Proposed Solution
Update the Kubernetes documentation to explicitly state:

  1. ports is optional in the to and from blocks of a NetworkPolicyPeer.
  2. Default behavior: If ports is omitted, all ports are allowed for the specified destinations (ipBlock, namespaceSelector, or podSelector).
  3. Include examples that demonstrate:
    • Traffic behavior without ports (allowing all ports).
    • Traffic behavior with ports (restricting to specific ports).

Examples for Documentation:

  • Without ports:

    egress:
    - to:
        - podSelector:
            matchLabels:
              app: database
    • Behavior: All ports are allowed to pods labeled app=database.
  • With ports:

    egress:
    - to:
        - podSelector:
            matchLabels:
              app: database
          ports:
          - protocol: TCP
            port: 3306
    • Behavior: Only TCP port 3306 is allowed to pods labeled app=database.

Where to Update
The following sections of the Kubernetes documentation should be updated:

  1. [Network Policies Documentation](https://kubernetes.io/docs/concepts/services-networking/network-policies/):

    • Add clarification about ports being optional and its default behavior.
    • Include updated examples that show the behavior when ports is omitted or specified.
  2. [NetworkPolicyPeer API Reference](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.32/#networkpolicypeer-v1-networking-k8s-io):

    • Update the to and from blocks to include the optional ports field.
    • Clarify the behavior when ports is omitted.

Why This Is Important

  • Explicitly documenting the behavior of ports will:
    • Improve clarity for users configuring NetworkPolicies.
    • Align user expectations with actual Kubernetes behavior.
    • Reduce confusion for both ingress and egress rules where ports may not be specified.

Additional Context
This clarification aligns with Kubernetes behavior verified through testing. Below is an additional example demonstrating traffic allowed to specific namespaces and pods when ports is omitted:

egress:
- to:
    - namespaceSelector:
        matchLabels:
          team: backend
      podSelector:
        matchLabels:
          app: redis
  • Behavior: All ports are allowed to pods labeled app=redis in namespaces labeled team=backend.
@krishnamanaiducloud krishnamanaiducloud added the kind/feature Categorizes issue or PR as related to a new feature. label Jan 26, 2025
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Jan 26, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

SIG Docs takes a lead on issue triage for this website, but any Kubernetes member can accept issues by applying the triage/accepted label.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.
Projects
None yet
Development

No branches or pull requests

2 participants