Skip to content

add validation webhooks for netfilter nic selector#998

Open
SchSeba wants to merge 1 commit intok8snetworkplumbingwg:masterfrom
SchSeba:virtual_webhook
Open

add validation webhooks for netfilter nic selector#998
SchSeba wants to merge 1 commit intok8snetworkplumbingwg:masterfrom
SchSeba:virtual_webhook

Conversation

@SchSeba
Copy link
Collaborator

@SchSeba SchSeba commented Dec 23, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Added validation rules for NetFilter configuration to enforce mutual exclusivity constraints, prevent incompatible EswitchMode and Bridge settings, and restrict LinkType values.
  • Tests

    • Added comprehensive unit tests for NetFilter validation scenarios and error handling.

✏️ Tip: You can customize this high-level summary in your review settings.

Signed-off-by: Sebastian Sch <sebassch@gmail.com>
@github-actions
Copy link

Thanks for your PR,
To run vendors CIs, Maintainers can use one of:

  • /test-all: To run all tests for all vendors.
  • /test-e2e-all: To run all E2E tests for all vendors.
  • /test-e2e-nvidia-all: To run all E2E tests for NVIDIA vendor.

To skip the vendors CIs, Maintainers can use one of:

  • /skip-all: To skip all tests for all vendors.
  • /skip-e2e-all: To skip all E2E tests for all vendors.
  • /skip-e2e-nvidia-all: To skip all E2E tests for NVIDIA vendor.
    Best regards.

@github-actions github-actions bot added the tests label Dec 23, 2025
@coderabbitai
Copy link

coderabbitai bot commented Dec 23, 2025

Walkthrough

Added NetFilter-specific validation rules to staticValidateSriovNetworkNodePolicy function that enforce mutual exclusivity of nicSelector fields, forbid EswitchMode and Bridge changes, and restrict LinkType to "eth"/"ETH" when NetFilter is configured. Corresponding unit tests validate these validation rules across multiple scenarios.

Changes

Cohort / File(s) Change Summary
NetFilter validation logic
pkg/webhook/validate.go
Added NetFilter-specific validations that execute early in staticValidateSriovNetworkNodePolicy: enforce mutual exclusivity of Vendor, DeviceID, PfNames, and RootDevices fields; disallow EswitchMode and Bridge configurations; restrict LinkType to "eth"/"ETH"; added minor whitespace formatting
NetFilter validation tests
pkg/webhook/validate_test.go
Added multiple unit tests covering NetFilter conflict scenarios: NetFilter with prohibited fields (Vendor, DeviceID, PfNames, RootDevices), NetFilter with EswitchMode, NetFilter with Bridge configurations, LinkType case sensitivity and empty values, and NetFilter-only configurations

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding validation webhooks for NetFilter NIC selector fields in the pull request.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 42d4fc0 and 40e65f2.

📒 Files selected for processing (2)
  • pkg/webhook/validate.go
  • pkg/webhook/validate_test.go
🧰 Additional context used
🧬 Code graph analysis (2)
pkg/webhook/validate.go (2)
api/v1/sriovnetworknodepolicy_types.go (1)
  • Bridge (87-90)
pkg/consts/constants.go (1)
  • LinkTypeETH (51-51)
pkg/webhook/validate_test.go (1)
api/v1/sriovnetworknodepolicy_types.go (5)
  • SriovNetworkNicSelector (70-84)
  • SriovNetworkNodePolicy (148-154)
  • SriovNetworkNodePolicySpec (27-68)
  • Bridge (87-90)
  • OVSConfig (98-103)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: test-coverage
  • GitHub Check: test
  • GitHub Check: build
  • GitHub Check: Golangci-lint
  • GitHub Check: Analyze (go)
🔇 Additional comments (7)
pkg/webhook/validate.go (2)

156-176: LGTM! NetFilter validations are well-structured and comprehensive.

The validation logic correctly enforces:

  1. Mutual exclusivity between NetFilter and other nicSelector fields
  2. Prohibition of EswitchMode when NetFilter is set
  3. Prohibition of Bridge configuration with NetFilter
  4. LinkType restriction to "eth"/"ETH" (case-insensitive)

The placement before other nicSelector validations is appropriate, and error messages are clear.


152-154: Existing NetFilter-only workflows are properly tested and will continue to work.

Comprehensive test coverage in pkg/webhook/validate_test.go confirms that policies using NetFilter as the sole nicSelector parameter pass validation (e.g., TestStaticValidateSriovNetworkNodePolicyWithNetFilterOnly, TestValidatePolicyForNodeStateWithValidNetFilter). Additionally, the validation enforces that NetFilter is mutually exclusive with other nicSelector fields—mixing NetFilter with vendor, deviceID, pfNames, or rootDevices correctly fails validation with appropriate error messages, as verified by test cases at lines 1193-1248.

pkg/webhook/validate_test.go (5)

1193-1249: LGTM! Comprehensive test coverage for mutual exclusivity.

The table-driven test approach effectively validates that NetFilter is mutually exclusive with Vendor, DeviceID, PfNames, and RootDevices. The error message validation ensures consistency with the implementation.


1251-1267: LGTM! EswitchMode validation is properly tested.

The test correctly verifies that combining NetFilter with EswitchMode is rejected.


1269-1285: LGTM! Bridge configuration validation is properly tested.

The test correctly verifies that Bridge configuration cannot be used with NetFilter.


1287-1345: LGTM! Excellent coverage of LinkType validation including case sensitivity.

The test cases comprehensively validate:

  • Rejection of "ib"/"IB" LinkType values
  • Acceptance of "eth"/"ETH" LinkType values (case-insensitive)
  • Acceptance of empty LinkType

This matches the case-insensitive validation logic using strings.EqualFold in the implementation.


1347-1362: LGTM! Validates that NetFilter can be used alone.

This test confirms that NetFilter is a valid standalone nicSelector option.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/webhook/validate_test.go (1)

1364-1426: Test combines NetFilter with PfNames, violating mutual exclusivity validation rules.

The test TestValidatePolicyForNodeStateWithValidVFAndNetFilter uses both NetFilter and PfNames together, which violates the static validation rule enforced in staticValidateSriovNetworkNodePolicy that prohibits using NetFilter with vendor, deviceID, pfNames, or rootDevices. The test passes only because it calls validatePolicyForNodeState (dynamic validation) directly, bypassing static validation. In practice, such policies would be rejected at the static validation stage before reaching dynamic validation.

This test should be removed or updated to align with the documented mutual exclusivity requirement.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 42d4fc0 and 40e65f2.

📒 Files selected for processing (2)
  • pkg/webhook/validate.go
  • pkg/webhook/validate_test.go
🧰 Additional context used
🧬 Code graph analysis (2)
pkg/webhook/validate.go (2)
api/v1/sriovnetworknodepolicy_types.go (1)
  • Bridge (87-90)
pkg/consts/constants.go (1)
  • LinkTypeETH (51-51)
pkg/webhook/validate_test.go (1)
api/v1/sriovnetworknodepolicy_types.go (5)
  • SriovNetworkNicSelector (70-84)
  • SriovNetworkNodePolicy (148-154)
  • SriovNetworkNodePolicySpec (27-68)
  • Bridge (87-90)
  • OVSConfig (98-103)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: test-coverage
  • GitHub Check: test
  • GitHub Check: build
  • GitHub Check: Golangci-lint
  • GitHub Check: Analyze (go)
🔇 Additional comments (7)
pkg/webhook/validate.go (2)

156-176: LGTM! NetFilter validations are well-structured and comprehensive.

The validation logic correctly enforces:

  1. Mutual exclusivity between NetFilter and other nicSelector fields
  2. Prohibition of EswitchMode when NetFilter is set
  3. Prohibition of Bridge configuration with NetFilter
  4. LinkType restriction to "eth"/"ETH" (case-insensitive)

The placement before other nicSelector validations is appropriate, and error messages are clear.


152-154: Existing NetFilter-only workflows are properly tested and will continue to work.

Comprehensive test coverage in pkg/webhook/validate_test.go confirms that policies using NetFilter as the sole nicSelector parameter pass validation (e.g., TestStaticValidateSriovNetworkNodePolicyWithNetFilterOnly, TestValidatePolicyForNodeStateWithValidNetFilter). Additionally, the validation enforces that NetFilter is mutually exclusive with other nicSelector fields—mixing NetFilter with vendor, deviceID, pfNames, or rootDevices correctly fails validation with appropriate error messages, as verified by test cases at lines 1193-1248.

pkg/webhook/validate_test.go (5)

1193-1249: LGTM! Comprehensive test coverage for mutual exclusivity.

The table-driven test approach effectively validates that NetFilter is mutually exclusive with Vendor, DeviceID, PfNames, and RootDevices. The error message validation ensures consistency with the implementation.


1251-1267: LGTM! EswitchMode validation is properly tested.

The test correctly verifies that combining NetFilter with EswitchMode is rejected.


1269-1285: LGTM! Bridge configuration validation is properly tested.

The test correctly verifies that Bridge configuration cannot be used with NetFilter.


1287-1345: LGTM! Excellent coverage of LinkType validation including case sensitivity.

The test cases comprehensively validate:

  • Rejection of "ib"/"IB" LinkType values
  • Acceptance of "eth"/"ETH" LinkType values (case-insensitive)
  • Acceptance of empty LinkType

This matches the case-insensitive validation logic using strings.EqualFold in the implementation.


1347-1362: LGTM! Validates that NetFilter can be used alone.

This test confirms that NetFilter is a valid standalone nicSelector option.

@SchSeba
Copy link
Collaborator Author

SchSeba commented Feb 18, 2026

Hi @adrianchiris @zeeke let me know if we can merge this one please

Copy link
Member

@zeeke zeeke left a comment

Choose a reason for hiding this comment

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

LGTM

return false, fmt.Errorf("nicSelector fields vendor, deviceID, pfNames, and rootDevices are not allowed when netFilter is specified")
}
// 2. do not support changing the EswitchMode when NetFilter is specified
if cr.Spec.EswitchMode != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

netfilter is when we use VMs right ? so we cant really configure sriov

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

right that is why we don't want to allow this

return false, fmt.Errorf("bridge configuration is not supported when netFilter is specified")
}
// 4. LinkType only "eth", "ETH" allowed when NetFilter is specified
if cr.Spec.LinkType != "" && !strings.EqualFold(cr.Spec.LinkType, consts.LinkTypeETH) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do we limit this just to eth ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

because that is what we tested right now and there is a request.
if someone will test and request IB device we can later remove this validation

Copy link
Collaborator Author

@SchSeba SchSeba left a comment

Choose a reason for hiding this comment

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

Thanks for the comments @adrianchiris I answer on both

return false, fmt.Errorf("nicSelector fields vendor, deviceID, pfNames, and rootDevices are not allowed when netFilter is specified")
}
// 2. do not support changing the EswitchMode when NetFilter is specified
if cr.Spec.EswitchMode != "" {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

right that is why we don't want to allow this

return false, fmt.Errorf("bridge configuration is not supported when netFilter is specified")
}
// 4. LinkType only "eth", "ETH" allowed when NetFilter is specified
if cr.Spec.LinkType != "" && !strings.EqualFold(cr.Spec.LinkType, consts.LinkTypeETH) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

because that is what we tested right now and there is a request.
if someone will test and request IB device we can later remove this validation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants