-
Notifications
You must be signed in to change notification settings - Fork 30
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: do not allow user to pick the same resource twice #1098
base: main
Are you sure you want to change the base?
fix: do not allow user to pick the same resource twice #1098
Conversation
172c13a
to
25efa7c
Compare
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 PR prevents users from selecting the same resource more than once by detecting duplicates and returning an error, and updates related tests to verify the behavior.
- Introduces duplicate resource detection logic in the resource selector to error out on repeated selections.
- Updates e2e tests and resource override tests to reflect the requirement of unique resource selections.
- Adjusts error handling in the controller to requeue after a delay when a duplicate is detected.
Reviewed Changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
test/e2e/placement_selecting_resources_test.go | Adds a focused test case to verify that duplicate resource selection produces the expected status update. |
test/e2e/placement_ro_test.go | Adjusts test messages and injects Placement references for resource override scenarios. |
test/e2e/actuals_test.go | Improves error messaging by including cluster name details. |
pkg/controllers/clusterresourceplacement/resource_selector_test.go | Updates tests to work with unstructured objects and maintain order for resource selection. |
pkg/controllers/clusterresourceplacement/resource_selector.go | Implements duplicate detection in resource selection and refines resource sorting logic. |
pkg/controllers/clusterresourceplacement/controller.go | Modifies error handling to requeue updates after a duplicate resource error is encountered. |
Files not reviewed (1)
- Makefile: Language not supported
Comments suppressed due to low confidence (2)
pkg/controllers/clusterresourceplacement/resource_selector.go:105
- [nitpick] Enhance the duplicate resource error message to include details such as the resource's name and kind to improve debuggability for users.
if _, exist := resourceMap[ri]; exist {
pkg/controllers/clusterresourceplacement/resource_selector_test.go:924
- [nitpick] Consider adding a dedicated unit test case for gatherSelectedResource to verify that the function returns an error when duplicate resources are provided.
tests := map[string]struct {
90ee8bc
to
12d903b
Compare
12d903b
to
e6d2395
Compare
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 PR prevents users from selecting the same resource more than once by detecting duplicates in the resource selectors and updating the CRP status accordingly. Key changes include:
- Adding an end-to-end test to verify that duplicated resource selection is rejected.
- Updating tests in resourceOverride flows to include the Placement reference and improve error messages.
- Refactoring resource selection logic to return unstructured objects and check for duplicate resource identifiers.
Reviewed Changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
test/e2e/placement_selecting_resources_test.go | Adds a new e2e test case for duplicated resource selection, expecting an invalid CRP status. |
test/e2e/placement_ro_test.go | Updates tests by adding Placement refs and revising test descriptions for clarity in resourceOverride scenarios. |
test/e2e/actuals_test.go | Improves error message details by including the member cluster name when a namespace is not removed. |
pkg/controllers/clusterresourceplacement/resource_selector_test.go | Modifies tests to use unstructured objects instead of runtime.Object for consistency. |
pkg/controllers/clusterresourceplacement/resource_selector.go | Refactors resource gathering to enforce uniqueness and sorts resources with simplified code. |
pkg/controllers/clusterresourceplacement/controller.go | Adjusts the error handling by requeuing after a fixed delay to allow user correction. |
Files not reviewed (1)
- Makefile: Language not supported
Co-authored-by: Copilot <[email protected]>
Titlefix: do not allow user to pick the same resource twice User descriptionDescription of your changesFixes # I have:
How has this code been testedSpecial notes for your reviewerPR TypeEnhancement Description
Changes walkthrough 📝
|
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
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
@@ -177,7 +177,8 @@ func (r *Reconciler) handleUpdate(ctx context.Context, crp *fleetv1beta1.Cluster | |||
klog.ErrorS(updateErr, "Failed to update the status", "clusterResourcePlacement", crpKObj) | |||
return ctrl.Result{}, controller.NewUpdateIgnoreConflictError(updateErr) | |||
} | |||
return ctrl.Result{}, err | |||
// no need to retry faster, the user needs to fix the resource selectors |
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 could be some api server errors returned from selectResourcesForPlacement func. Should we keep the existing backoff?
} | ||
if _, exist := resourceMap[ri]; exist { | ||
err = fmt.Errorf("found duplicate resource %+v", ri) | ||
klog.ErrorS(err, "user selected one resource more than once", "resource", ri, "placement", placement) |
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.
klog.ErrorS(err, "user selected one resource more than once", "resource", ri, "placement", placement) | |
klog.ErrorS(err, "User selected one resource more than once", "resource", ri, "placement", placement) |
@@ -201,6 +201,9 @@ var _ = Context("creating resourceOverride with multiple jsonPatchOverrides to o | |||
Namespace: roNamespace, | |||
}, | |||
Spec: placementv1alpha1.ResourceOverrideSpec{ | |||
Placement: &placementv1alpha1.PlacementRef{ | |||
Name: crpName, // assigned CRP name |
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.
it may work as expected to test the override without configuring the CRP.
}, | ||
Spec: placementv1beta1.ClusterResourcePlacementSpec{ | ||
ResourceSelectors: []placementv1beta1.ClusterResourceSelector{ | ||
{ |
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.
should we block this case via the validation webhook?
Description of your changes
I have:
make reviewable
to ensure this PR is ready for review.How has this code been tested
Special notes for your reviewer