-
Notifications
You must be signed in to change notification settings - Fork 11
fix(webhook): check nad labels for associated nad #77
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
Conversation
Signed-off-by: Zespre Chang <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request fixes the VirtualMachineNetworkConfig (vmnetcfg) validating webhook to properly handle scenarios where the IPPool name differs from its associated NetworkAttachmentDefinition (NAD) name. The fix retrieves the IPPool reference from NAD labels instead of assuming the names match.
Key Changes
- Modified the webhook validator to read IPPool namespace and name from NAD labels (
network.harvesterhci.io/ippool-namespaceandnetwork.harvesterhci.io/ippool-name) - Added nadCache dependency to the validator to look up NAD resources
- Added comprehensive unit tests covering different naming scenarios
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| pkg/webhook/vmnetcfg/validator.go | Updated validation logic to retrieve IPPool information from NAD labels instead of directly from network name, added NAD cache dependency |
| pkg/webhook/vmnetcfg/validator_test.go | Added new test file with test cases for IPPool/NAD name matching scenarios and error conditions |
| cmd/webhook/run.go | Updated validator constructor call to include nadCache parameter |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| given: input{ | ||
| vmNetCfg: newTestVirtualMachineNetworkConfigBuilder(). | ||
| WithNetworkConfig("", "", testNetworkName).Build(), | ||
| ipPool: ippool.NewIPPoolBuilder(testNADNamespace, testNADName).Build(), |
Copilot
AI
Dec 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The IPPool created in this test is missing the NetworkName configuration. The IPPool should be associated with the network by setting its NetworkName to testNetworkName, similar to how it's done in other tests. Without this, the test doesn't properly reflect the real-world scenario where IPPool references the NAD through its NetworkName field.
| ipPool: ippool.NewIPPoolBuilder(testNADNamespace, testNADName).Build(), | |
| ipPool: ippool.NewIPPoolBuilder(testNADNamespace, testNADName). | |
| WithNetworkName(testNetworkName).Build(), |
| return fmt.Errorf( | ||
| webhook.CreateErr, | ||
| vmNetCfg.Kind, | ||
| vmNetCfg.Namespace, | ||
| vmNetCfg.Name, fmt.Errorf("%s label not found", util.IPPoolNameLabelKey), |
Copilot
AI
Dec 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error formatting here has an issue. The fmt.Errorf call wraps another fmt.Errorf as the last argument, but the format string webhook.CreateErr expects a %w verb, not a nested error. This should be simplified to pass the error message string directly or use a properly formatted error. The current code may not produce the expected error message format.
| return fmt.Errorf( | |
| webhook.CreateErr, | |
| vmNetCfg.Kind, | |
| vmNetCfg.Namespace, | |
| vmNetCfg.Name, fmt.Errorf("%s label not found", util.IPPoolNameLabelKey), | |
| err := fmt.Errorf("%s label not found", util.IPPoolNameLabelKey) | |
| return fmt.Errorf( | |
| webhook.CreateErr, | |
| vmNetCfg.Kind, | |
| vmNetCfg.Namespace, | |
| vmNetCfg.Name, | |
| err, |
pkg/webhook/vmnetcfg/validator.go
Outdated
| } | ||
| ipPoolNamespace, ok := nad.Labels[util.IPPoolNamespaceLabelKey] | ||
| if !ok { | ||
| ipPoolNamespace = nadNamespace |
Copilot
AI
Dec 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an inconsistency with how the controller handles the IPPoolNamespaceLabelKey. In the controller's getIPPoolFromNetworkName function, if the IPPoolNamespaceLabelKey label is missing, it returns an error. However, in this validator, if the label is missing, it defaults to nadNamespace. This inconsistency could lead to the validator accepting configurations that the controller will reject. The validator should either match the controller's strict behavior or the controller should adopt this more lenient approach.
| ipPoolNamespace = nadNamespace | |
| return fmt.Errorf( | |
| webhook.CreateErr, | |
| vmNetCfg.Kind, | |
| vmNetCfg.Namespace, | |
| vmNetCfg.Name, fmt.Errorf("%s label not found", util.IPPoolNamespaceLabelKey), | |
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we support nad and ipool in different namespaces or does it have to be in same namespace. Do we restrict this somewhere ? Any reason behind using nad namespace as ipool namespace in case of namespace does not exist.
Can we return error instead ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we support nad and ipool in different namespaces or does it have to be in same namespace. Do we restrict this somewhere ?
yes we currently support cross-namespace referencing between nads and ippools. but i gradually realize that this shouldn't be allowed. restricting ippools that can only reference the same namespace nads should be the norm. however, this will be a breaking change for users who have already done so (cross-namespace referencing), and need to be planned carefully.
Any reason behind using nad namespace as ipool namespace in case of namespace does not exist.
Can we return error instead ?
sure we can return an error here. i initially thought it could be handy to set it to the nad's namespace if the namespace label on the nad is missing.
| shouldErr: true, | ||
| }, | ||
| }, | ||
| } |
Copilot
AI
Dec 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test suite is missing a test case for when a NAD exists but lacks the required IPPoolNameLabelKey label. This is an important validation scenario that should be tested to ensure the validator properly rejects VirtualMachineNetworkConfig creation when the NAD is not properly labeled. Consider adding a test case similar to the existing ones that validates this error path.
| given: input{ | ||
| vmNetCfg: newTestVirtualMachineNetworkConfigBuilder(). | ||
| WithNetworkConfig("", "", testNetworkName).Build(), | ||
| ipPool: newTestIPPoolBuilder().Build(), |
Copilot
AI
Dec 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The IPPool created in this test is missing the NetworkName configuration. The IPPool should be associated with the network by setting its NetworkName to testNetworkName, similar to how it's done in other tests. Without this, the test doesn't properly reflect the real-world scenario where IPPool references the NAD through its NetworkName field.
| given: input{ | ||
| vmNetCfg: newTestVirtualMachineNetworkConfigBuilder(). | ||
| WithNetworkConfig("", "", testNetworkName).Build(), | ||
| ipPool: newTestIPPoolBuilder().Build(), |
Copilot
AI
Dec 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The IPPool created in this test is missing the NetworkName configuration. The IPPool should be associated with the network by setting its NetworkName to testNetworkName. Without this, the test doesn't properly reflect the real-world scenario where IPPool references the NAD through its NetworkName field.
cf14971 to
c8f1130
Compare
Signed-off-by: Zespre Chang <[email protected]>
c8f1130 to
ea6c375
Compare
Signed-off-by: Zespre Chang <[email protected]>
|
LGTM,Thanks |
ibrokethecloud
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. thanks for the fix.
|
@mergify backport v1.7 |
✅ Backports have been createdDetails
|
IMPORTANT: Please do not create a Pull Request without creating an issue first.
Problem:
If the name of the IPPool CR is not the same as its associated NetworkAttachmentDefinition (NAD) CR, all the VirtualMachineNetworkConfig (vmnetcfg) CR referencing the IPPool cannot be created automatically by the controller.
Solution:
Fix the vmnetcfg validating webhook logic.
Related Issue:
harvester/harvester#9710
Test plan:
test-nettest-pool(the name must be different from its associated NAD's name)test-vmand attach it to thetest-netNAD