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

Fix namespaceByLabel when multiple namespaces have same label #523

Merged
merged 3 commits into from
Aug 30, 2024

Conversation

martinhny
Copy link
Contributor

@martinhny martinhny commented Aug 29, 2024

https://kartverketgroup.slack.com/archives/C028ZEED280/p1724920375968729

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced namespace management allows for multiple namespaces to be processed for access policies.
    • Introduced a new Kubernetes NetworkPolicy to manage ingress and egress traffic based on labels and namespaces.
  • Bug Fixes

    • Improved error handling for scenarios where no namespaces are found.
  • Tests

    • Added new testing scenarios to cover multiple namespaces with the same label, expanding test coverage for access policies.

@martinhny martinhny requested a review from a team as a code owner August 29, 2024 09:40
Copy link
Contributor

coderabbitai bot commented Aug 29, 2024

Walkthrough

The changes involve significant modifications to the setPortsForRules method in the ReconcilerBase struct, enhancing namespace handling and error management. New test scenarios are introduced to validate functionality across multiple namespaces, supported by additional YAML configurations defining Kubernetes NetworkPolicies and applications. These updates improve the management of application deployments and access policies within a Kubernetes environment.

Changes

Files Change Summary
internal/controllers/common/reconciler.go Modified setPortsForRules method to change parameter from namespace to skipObjNamespace, allowing for multiple namespaces and improved error handling.
tests/application/access-policy/multiple-ns-same-label-assert.yaml Introduced a new NetworkPolicy for accesspolicy-app, defining ingress and egress rules based on labels and namespaces.
tests/application/access-policy/multiple-ns-same-label.yaml Created a new YAML file defining multiple namespaces and applications, including access policies for inter-application communication.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ReconcilerBase
    participant NamespaceManager
    participant PolicyManager

    User->>ReconcilerBase: Call setPortsForRules(rules, skipObjNamespace)
    ReconcilerBase->>NamespaceManager: Retrieve namespaces based on rules
    NamespaceManager-->>ReconcilerBase: Return namespaceList
    ReconcilerBase->>PolicyManager: Apply access policies for each namespace
    PolicyManager-->>ReconcilerBase: Confirm policy application
    ReconcilerBase-->>User: Return success or error
Loading

Poem

🐰 In the meadow, changes bloom,
New paths for rules, dispel the gloom.
With namespaces dancing, side by side,
Access policies take a joyful ride!
Hops of progress, leaps of cheer,
In our Kubernetes world, we persevere! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
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>.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @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.

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 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.

@martinhny martinhny force-pushed the fix_namespacebylabel_accesspolicy branch 2 times, most recently from 5e4e740 to 6f50d78 Compare August 29, 2024 09:42
@martinhny martinhny force-pushed the fix_namespacebylabel_accesspolicy branch from 6f50d78 to e721015 Compare August 29, 2024 09:48
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Outside diff range, codebase verification and nitpick comments (1)
internal/controllers/common/reconciler.go (1)

215-215: Update function documentation to reflect parameter change.

The parameter namespace has been replaced with skipObjNamespace. Ensure that the function documentation is updated to reflect this change.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 98d90cd and e721015.

Files selected for processing (4)
  • internal/controllers/common/reconciler.go (1 hunks)
  • tests/application/access-policy/chainsaw-test.yaml (1 hunks)
  • tests/application/access-policy/multiple-ns-same-label-assert.yaml (1 hunks)
  • tests/application/access-policy/multiple-ns-same-label.yaml (1 hunks)
Additional comments not posted (14)
tests/application/access-policy/multiple-ns-same-label-assert.yaml (3)

1-4: LGTM!

The metadata section is correctly defined.


5-35: LGTM!

The spec section is correctly defined.


12-35: LGTM!

The ingress and egress rules are correctly defined.

tests/application/access-policy/chainsaw-test.yaml (2)

Line range hint 1-8: LGTM!

The metadata section is correctly defined.


Line range hint 9-44: LGTM!

The spec section is correctly defined.

tests/application/access-policy/multiple-ns-same-label.yaml (5)

1-13: LGTM!

The namespace definitions are correctly defined.


15-31: LGTM!

The application definitions are correctly defined.


33-38: LGTM!

The namespace definition is correctly defined.


40-46: LGTM!

The application definition is correctly defined.


48-66: LGTM!

The access policy definition is correctly defined.

internal/controllers/common/reconciler.go (4)

222-224: LGTM!

The switch case for rule.Namespace is correctly implemented.


232-233: LGTM!

The loop for appending namespaces based on labels is correctly implemented.


235-236: LGTM!

The default case for appending skipObjNamespace is correctly implemented.


243-248: LGTM!

The loop for retrieving application ports for each namespace is correctly implemented.

internal/controllers/common/reconciler.go Show resolved Hide resolved
internal/controllers/common/reconciler.go Show resolved Hide resolved
internal/controllers/common/reconciler.go Outdated Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@martinhny martinhny requested a review from evenh August 29, 2024 10:41
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e721015 and 5fc1e29.

Files selected for processing (3)
  • internal/controllers/common/reconciler.go (1 hunks)
  • tests/application/access-policy/multiple-ns-same-label-assert.yaml (1 hunks)
  • tests/application/access-policy/multiple-ns-same-label.yaml (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • tests/application/access-policy/multiple-ns-same-label-assert.yaml
  • tests/application/access-policy/multiple-ns-same-label.yaml
Additional comments not posted (6)
internal/controllers/common/reconciler.go (6)

215-215: Review the updated function signature.

The function signature has been changed from using a single namespace parameter to skipObjNamespace. This change aligns with the PR's goal to handle multiple namespaces more effectively.

The change is approved as it supports the intended functionality enhancement.


222-239: Enhanced namespace determination logic.

The switch statement effectively handles different scenarios for namespace determination. It checks for direct namespace specification, namespaces by label, and defaults to skipObjNamespace if no other conditions are met.

The logic is robust and aligns with the PR's objectives to handle multiple namespaces.


227-230: Proper error handling for LabelSelectorAsSelector.

The error from metav1.LabelSelectorAsSelector is now correctly handled, which enhances the robustness of the function.

The error handling improvement is a positive change.


242-243: Improve error message.

The error message can be more informative by including the rule details.

- return fmt.Errorf("expected namespace, but found none for rule %s", rule.Application)
+ return fmt.Errorf("expected namespace, but found none for rule %s in application %s", rule, rule.Application)

This suggestion has been made in a previous review and is still valid.


246-251: Review the logic for appending ports.

The function iterates over each namespace in namespaceList to retrieve and append application ports. This approach ensures that ports are set correctly for each namespace, supporting the functionality when multiple namespaces share the same label.

The implementation is correct and supports the intended enhancements for handling multiple namespaces.


221-221: Initialize namespaceList with a capacity.

As previously suggested, initializing namespaceList with a capacity could improve performance if the number of namespaces can be estimated.

Consider initializing namespaceList with a capacity to optimize memory allocation:

- var namespaceList []string
+ namespaceList := make([]string, 0, estimatedCapacity)

Likely invalid or redundant comment.

@martinhny martinhny merged commit 0205584 into main Aug 30, 2024
9 checks passed
@martinhny martinhny deleted the fix_namespacebylabel_accesspolicy branch August 30, 2024 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants