-
Notifications
You must be signed in to change notification settings - Fork 947
Create federated-resource-quota-enforcement-controller #6367
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
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #6367 +/- ##
=========================================
Coverage ? 49.21%
=========================================
Files ? 684
Lines ? 55325
Branches ? 0
=========================================
Hits ? 27228
Misses ? 26324
Partials ? 1773
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
return []reconcile.Request{{ | ||
NamespacedName: types.NamespacedName{ | ||
Namespace: obj.GetNamespace(), | ||
Name: obj.GetName(), | ||
}, |
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.
When a deletion event of ResourceBinding is detected, it's possible to return a QuotaList instead of the ResourceBinding itself, allowing the reconcile logic to focus only on FederatedResourceQuota.
Here is an example:
karmada/pkg/controllers/federatedresourcequota/federated_resource_quota_sync_controller.go
Lines 104 to 131 in e2f0e6e
fn := handler.MapFunc( | |
func(ctx context.Context, obj client.Object) []reconcile.Request { | |
var requests []reconcile.Request | |
var clusterName string | |
switch obj.(type) { | |
case *clusterv1alpha1.Cluster: | |
clusterName = obj.GetName() | |
default: | |
return nil | |
} | |
FederatedResourceQuotaList := &policyv1alpha1.FederatedResourceQuotaList{} | |
if err := c.Client.List(ctx, FederatedResourceQuotaList); err != nil { | |
klog.Errorf("Failed to list FederatedResourceQuota, error: %v", err) | |
} | |
for _, federatedResourceQuota := range FederatedResourceQuotaList.Items { | |
hard := extractClusterHardResourceList(federatedResourceQuota.Spec, clusterName) | |
if hard == nil { | |
continue | |
} | |
requests = append(requests, reconcile.Request{ | |
NamespacedName: types.NamespacedName{ | |
Namespace: federatedResourceQuota.Namespace, | |
Name: federatedResourceQuota.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.
Oh nice! I'll add this in. Thanks
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.
Thought about this more - I think we do want to make a distinction between ResourceBinding and FederatedResourceQuota reconciles.
When reconciling the quota, we will only update the Overall
field of the quota. When reconciling a ResourceBinding deletion, we will update both Overall
and OverallUsed
. That way the validation webhook is entirely responsible for updating the used resources, and there is a separation of concerns.
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.
When reconciling the quota, we will only update the
Overall
field of the quota. When reconciling a ResourceBinding deletion, we will update both Overall and OverallUsed.
It's not entirely the case; when the quota's hard limits change, it will also be necessary to update the OverallUsed.
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.
Good thinking! But not the pattern of how the controller works.
The reconciliation logic is fixed and only pushes the target object(FedeeratedResourceQuota) to the desired state. For this controller, it will list all ResourceBindings calculate the in-use quota, and then update the OverallUsed
.
Changes to ResourceBinding are one of the factors that trigger the reconciliation of FederatedResourceQuoa.
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.
Could there be cases where the validation webhook and the controller both update the overallUsed at the same time? I suppose the only time this would happen is if a few RBs are simultaneously created and deleted, or if RB are created around the same time the FRQ is updated, but could this cause conflicts?
Nevermind - if we trigger another reconciliation in response to failed updates, then these type of conflicts should not happen.
Signed-off-by: mszacillo <[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.
/assign
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 introduces the new federated-resource-quota-enforcement controller to reconcile FederatedResourceQuota resources and ResourceBinding deletions. Key updates include:
- Adding a new helper function in binding.go to list ResourceBindings by namespace.
- Implementing the QuotaEnforcementController in its own package along with corresponding tests for resource usage calculations.
- Registering the new controller in the controller manager.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
pkg/util/helper/binding.go | Added GetResourceBindingsByNamespace to list ResourceBindings filtered by namespace. |
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller_test.go | Added tests for calculating resource usage from ResourceBinding instances. |
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go | Introduced the controller logic including reconciliation and status update methods. |
cmd/controller-manager/app/controllermanager.go | Registered the new federated-resource-quota-enforcement controller. |
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
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.
Thanks for your great work! @mszacillo
My opinion is for reference only; open to discussion 😄
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Show resolved
Hide resolved
83133c1
to
56e5ef1
Compare
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
pkg/controllers/federatedresourcequota/federated_resource_quota_enforcement_controller.go
Outdated
Show resolved
Hide resolved
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.
Looks great!
6f053aa
to
0d42f6e
Compare
Signed-off-by: mszacillo <[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.
/lgtm
@zhzhuang-zju Do you have any further comments?
/lgtm |
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.
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: RainbowMango 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 |
What type of PR is this?
Adds the federated-resource-quota-enforcement controller.
Reconciles in response to one of the following events:
At the moment I did not include the third planned reconcile:
Periodically recalculate FRQ status
, since I was worried about usingRequeueAfter
and hammering the controller with too many reconcile events. Perhaps we can add this as a follow-up iteration task./kind feature
What this PR does / why we need it:
Which issue(s) this PR fixes:
Part of #6350.
Special notes for your reviewer:
Does this PR introduce a user-facing change?: