Skip to content

CNTRLPLANE-206: Add NetworkPolicy to shared-ingress #6173

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

muraee
Copy link
Contributor

@muraee muraee commented May 20, 2025

What this PR does / why we need it:
This restricts internal egress traffic to only the KAS of the HostedClusters and allow external internet access for the External DNS case.

Which issue(s) this PR fixes (optional, use fixes #<issue_number>(, fixes #<issue_number>, ...) format, where issue_number might be a GitHub issue, or a Jira story:
Fixes #

Checklist

  • Subject and description added to both, commit and PR.
  • Relevant issues have been referenced.
  • This change includes docs.
  • This change includes unit tests.

Summary by CodeRabbit

  • New Features

    • Introduced a network policy for router pods to control ingress and egress traffic, including specific rules for ports and destinations.
    • Shared Ingress controller now automatically manages router network policies based on cluster capabilities and network configuration.
  • Chores

    • Updated internal setup to provide cluster capability information to the Shared Ingress controller.

@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 20, 2025
Copy link

coderabbitai bot commented May 20, 2025

Walkthrough

A new Kubernetes NetworkPolicy for router pods was introduced, along with logic to reconcile and manage this policy based on cluster capabilities and network configuration. The SharedIngressReconciler struct now includes a capability checker, and the controller setup was updated to initialize this new field.

Changes

File(s) Change Summary
hypershift-operator/controllers/sharedingress/manifests.go Added RouterNetworkPolicy function returning a base NetworkPolicy object for the router namespace.
hypershift-operator/controllers/sharedingress/router.go Added ReconcileRouterNetworkPolicy to define and update router NetworkPolicy with ingress/egress rules based on cluster capabilities.
hypershift-operator/controllers/sharedingress/sharedingress_controller.go Extended SharedIngressReconciler with a capability checker and implemented router NetworkPolicy reconciliation logic.
hypershift-operator/main.go Updated SharedIngressReconciler instantiation to include the capability checker field.

Sequence Diagram(s)

sequenceDiagram
    participant Controller
    participant Capabilities
    participant ClusterConfig
    participant K8sAPI

    Controller->>Capabilities: Check for Networks/DNS capabilities
    alt Networks capability present
        Controller->>ClusterConfig: Fetch management cluster network config
    end
    Controller->>K8sAPI: Get or create Router NetworkPolicy
    Controller->>K8sAPI: Reconcile NetworkPolicy (ingress/egress rules)
    K8sAPI-->>Controller: Success/Error
Loading

Poem

A bunny hops through YAML fields and code,
Crafting network rules for routers on the road.
Capabilities checked, policies set with care,
Pods now protected from traffic unaware.
With every hop, security grows—
In Kubernetes gardens, the safe router flows!
🐇✨

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 13e0d5f and 28584a9.

📒 Files selected for processing (4)
  • hypershift-operator/controllers/sharedingress/manifests.go (2 hunks)
  • hypershift-operator/controllers/sharedingress/router.go (2 hunks)
  • hypershift-operator/controllers/sharedingress/sharedingress_controller.go (3 hunks)
  • hypershift-operator/main.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
hypershift-operator/controllers/sharedingress/sharedingress_controller.go (2)
hypershift-operator/controllers/sharedingress/manifests.go (1)
  • RouterNetworkPolicy (58-65)
hypershift-operator/controllers/sharedingress/router.go (1)
  • ReconcileRouterNetworkPolicy (310-419)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Red Hat Konflux / hypershift-operator-main-on-pull-request
  • GitHub Check: Red Hat Konflux / hypershift-operator-main-on-pull-request
  • GitHub Check: Red Hat Konflux / hypershift-operator-main-on-pull-request
  • GitHub Check: Red Hat Konflux / hypershift-operator-main-on-pull-request
🔇 Additional comments (8)
hypershift-operator/main.go (1)

468-470: Field addition aligns with capability-checking architecture

Adding the ManagementClusterCapabilities field to the SharedIngressReconciler allows the reconciler to make capability-aware decisions, which is necessary for the new NetworkPolicy implementation.

hypershift-operator/controllers/sharedingress/manifests.go (2)

6-6: LGTM: Adding required networking import

The network policy implementation requires this import.


58-65: Well-structured NetworkPolicy skeleton

The RouterNetworkPolicy function follows the existing pattern in the file, providing a skeleton NetworkPolicy with proper metadata, which will be populated by the reconciliation logic.

hypershift-operator/controllers/sharedingress/router.go (2)

10-21: LGTM: Adding required imports

The imports added are necessary for the NetworkPolicy implementation and capability checks.


310-419: Comprehensive NetworkPolicy implementation

The NetworkPolicy implementation effectively achieves the PR objective of restricting internal egress traffic to only the Kubernetes API Server while permitting external internet access. The policy:

  1. Allows ingress to the router on required ports (8080, 8443)
  2. Creates egress rules that:
    • Allow traffic to external destinations (0.0.0.0/0) while excluding cluster networks
    • Allow traffic to kube-apiserver pods in all namespaces
    • Support both OpenShift DNS and standard DNS configurations

This implementation carefully handles different cluster configurations by conditionally applying rules based on capabilities.

hypershift-operator/controllers/sharedingress/sharedingress_controller.go (3)

12-17: LGTM: Adding required imports

The imports added are necessary for the capability checks and network configuration.


38-39: Well-documented capability checker field

Good documentation on the purpose of the ManagementClusterCapabilities field.


250-267: Capability-aware NetworkPolicy reconciliation

The implementation effectively:

  1. Conditionally fetches the management cluster network configuration based on capabilities
  2. Creates/updates the NetworkPolicy with the appropriate configuration
  3. Handles error cases properly
  4. Includes appropriate logging

This approach ensures the NetworkPolicy is only applied with the correct configuration based on cluster capabilities.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@openshift-ci openshift-ci bot requested review from csrwng and sjenning May 20, 2025 10:08
@openshift-ci openshift-ci bot added the area/hypershift-operator Indicates the PR includes changes for the hypershift operator and API - outside an OCP release label May 20, 2025
Copy link
Contributor

openshift-ci bot commented May 20, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: muraee

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. and removed do-not-merge/needs-area labels May 20, 2025
@cwbotbot
Copy link

cwbotbot commented May 20, 2025

Test Results

e2e-aws

e2e-aks

@muraee
Copy link
Contributor Author

muraee commented May 20, 2025

/retest-required

1 similar comment
@muraee
Copy link
Contributor Author

muraee commented May 20, 2025

/retest-required

@muraee muraee changed the title WIP: Add NetworkPolicy to shared-ingress CNTRLPLANE-206: Add NetworkPolicy to shared-ingress May 21, 2025
@openshift-ci-robot
Copy link

openshift-ci-robot commented May 21, 2025

@muraee: This pull request references CNTRLPLANE-206 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the story to target the "4.20.0" version, but no target version was set.

In response to this:

What this PR does / why we need it:
This restricts internal egress traffic to only the KAS of the HostedClusters and allow external internet access for the External DNS case.

Which issue(s) this PR fixes (optional, use fixes #<issue_number>(, fixes #<issue_number>, ...) format, where issue_number might be a GitHub issue, or a Jira story:
Fixes #

Checklist

  • Subject and description added to both, commit and PR.
  • Relevant issues have been referenced.
  • This change includes docs.
  • This change includes unit tests.

Summary by CodeRabbit

  • New Features

  • Introduced a network policy for router pods to control ingress and egress traffic, including specific rules for ports and destinations.

  • Shared Ingress controller now automatically manages router network policies based on cluster capabilities and network configuration.

  • Chores

  • Updated internal setup to provide cluster capability information to the Shared Ingress controller.

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 openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci-robot openshift-ci-robot added the jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. label May 21, 2025
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 21, 2025
This restricts internal egress traffic to only the KAS of the HostedClusters and allow external internet access for the External DNS case.
@muraee muraee force-pushed the share-ingress-np branch from 28584a9 to 7a55217 Compare May 21, 2025 08:52
Copy link
Contributor

openshift-ci bot commented May 21, 2025

@muraee: all tests passed!

Full PR test history. Your PR dashboard.

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. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/hypershift-operator Indicates the PR includes changes for the hypershift operator and API - outside an OCP release jira/valid-reference Indicates that this PR references a valid Jira ticket of any type.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants