-
Notifications
You must be signed in to change notification settings - Fork 4.3k
[Granular resource limits] Add support for granular resource quotas #8662
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
Merged
k8s-ci-robot
merged 1 commit into
kubernetes:master
from
norbertcyran:new-resource-limits
Nov 20, 2025
+1,391
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| Copyright 2025 The Kubernetes Authors. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package resourcequotas | ||
|
|
||
| import ( | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/autoscaler/cluster-autoscaler/context" | ||
| "k8s.io/autoscaler/cluster-autoscaler/processors/customresources" | ||
| ) | ||
|
|
||
| // TrackerFactory builds quota trackers. | ||
| type TrackerFactory struct { | ||
| crp customresources.CustomResourcesProcessor | ||
| quotasProvider Provider | ||
| usageCalculator *usageCalculator | ||
| } | ||
|
|
||
| // TrackerOptions stores configuration for quota tracking. | ||
| type TrackerOptions struct { | ||
| CustomResourcesProcessor customresources.CustomResourcesProcessor | ||
| QuotaProvider Provider | ||
| NodeFilter NodeFilter | ||
| } | ||
|
|
||
| // NewTrackerFactory creates a new TrackerFactory. | ||
| func NewTrackerFactory(opts TrackerOptions) *TrackerFactory { | ||
| uc := newUsageCalculator(opts.CustomResourcesProcessor, opts.NodeFilter) | ||
| return &TrackerFactory{ | ||
| crp: opts.CustomResourcesProcessor, | ||
| quotasProvider: opts.QuotaProvider, | ||
| usageCalculator: uc, | ||
| } | ||
| } | ||
|
|
||
| // NewQuotasTracker builds a new Tracker. | ||
| // | ||
| // NewQuotasTracker calculates resources used by the nodes for every | ||
| // quota returned by the Provider. Then, based on usages and limits it calculates | ||
| // how many resources can be still added to the cluster. Returns a Tracker object. | ||
| func (f *TrackerFactory) NewQuotasTracker(autoscalingCtx *context.AutoscalingContext, nodes []*corev1.Node) (*Tracker, error) { | ||
| quotas, err := f.quotasProvider.Quotas() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| usages, err := f.usageCalculator.calculateUsages(autoscalingCtx, nodes, quotas) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var quotaStatuses []*quotaStatus | ||
| for _, rq := range quotas { | ||
| limitsLeft := make(resourceList) | ||
| limits := rq.Limits() | ||
| for resourceType, limit := range limits { | ||
| usage := usages[rq.ID()][resourceType] | ||
| limitsLeft[resourceType] = max(0, limit-usage) | ||
| } | ||
| quotaStatuses = append(quotaStatuses, "aStatus{ | ||
| quota: rq, | ||
| limitsLeft: limitsLeft, | ||
| }) | ||
| } | ||
| tracker := newTracker(f.crp, quotaStatuses) | ||
| return tracker, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| /* | ||
| Copyright 2025 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package resourcequotas | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| apiv1 "k8s.io/api/core/v1" | ||
| "k8s.io/autoscaler/cluster-autoscaler/cloudprovider" | ||
| cptest "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/test" | ||
| "k8s.io/autoscaler/cluster-autoscaler/context" | ||
| "k8s.io/autoscaler/cluster-autoscaler/processors/customresources" | ||
| "k8s.io/autoscaler/cluster-autoscaler/utils/test" | ||
| "k8s.io/autoscaler/cluster-autoscaler/utils/units" | ||
| ) | ||
|
|
||
| type nodeExcludeFn func(node *apiv1.Node) bool | ||
|
|
||
| func (n nodeExcludeFn) ExcludeFromTracking(node *apiv1.Node) bool { | ||
| return n(node) | ||
| } | ||
|
|
||
| func TestNewQuotasTracker(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| crp customresources.CustomResourcesProcessor | ||
| nodeFilter NodeFilter | ||
| nodes []*apiv1.Node | ||
| limits map[string]int64 | ||
| newNode *apiv1.Node | ||
| nodeDelta int | ||
| wantResult *CheckDeltaResult | ||
| }{ | ||
| { | ||
| name: "default config allowed operation", | ||
| nodes: []*apiv1.Node{ | ||
| test.BuildTestNode("n1", 1000, 2*units.GiB), | ||
| test.BuildTestNode("n2", 2000, 4*units.GiB), | ||
| test.BuildTestNode("n3", 3000, 8*units.GiB), | ||
| }, | ||
| limits: map[string]int64{ | ||
| "cpu": 12, | ||
| "memory": 32 * units.GiB, | ||
| }, | ||
| newNode: test.BuildTestNode("n4", 2000, 4*units.GiB), | ||
| nodeDelta: 2, | ||
| wantResult: &CheckDeltaResult{ | ||
| AllowedDelta: 2, | ||
| }, | ||
| }, | ||
| { | ||
| name: "default config exceeded operation", | ||
| nodes: []*apiv1.Node{ | ||
| test.BuildTestNode("n1", 1000, 2*units.GiB), | ||
| test.BuildTestNode("n2", 2000, 4*units.GiB), | ||
| test.BuildTestNode("n3", 3000, 8*units.GiB), | ||
| }, | ||
| limits: map[string]int64{ | ||
| "cpu": 6, | ||
| "memory": 16 * units.GiB, | ||
| }, | ||
| newNode: test.BuildTestNode("n4", 2000, 4*units.GiB), | ||
| nodeDelta: 2, | ||
| wantResult: &CheckDeltaResult{ | ||
| AllowedDelta: 0, | ||
| ExceededQuotas: []ExceededQuota{ | ||
| {ID: "cluster-wide", ExceededResources: []string{"cpu", "memory"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "default config partially allowed operation", | ||
| nodes: []*apiv1.Node{ | ||
| test.BuildTestNode("n1", 1000, 2*units.GiB), | ||
| test.BuildTestNode("n2", 2000, 4*units.GiB), | ||
| test.BuildTestNode("n3", 3000, 8*units.GiB), | ||
| }, | ||
| limits: map[string]int64{ | ||
| "cpu": 7, | ||
| "memory": 16 * units.GiB, | ||
| }, | ||
| newNode: test.BuildTestNode("n4", 2000, 4*units.GiB), | ||
| nodeDelta: 2, | ||
| wantResult: &CheckDeltaResult{ | ||
| AllowedDelta: 0, | ||
| ExceededQuotas: []ExceededQuota{ | ||
| {ID: "cluster-wide", ExceededResources: []string{"cpu", "memory"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "custom resource config allowed operation", | ||
| crp: &fakeCustomResourcesProcessor{ | ||
| NodeResourceTargets: func(n *apiv1.Node) []customresources.CustomResourceTarget { | ||
| if n.Name == "n1" { | ||
| return []customresources.CustomResourceTarget{ | ||
| { | ||
| ResourceType: "gpu", | ||
| ResourceCount: 1, | ||
| }, | ||
| } | ||
| } | ||
| return nil | ||
| }, | ||
| }, | ||
| nodes: []*apiv1.Node{ | ||
| test.BuildTestNode("n1", 1000, 2*units.GiB), | ||
| test.BuildTestNode("n2", 2000, 4*units.GiB), | ||
| test.BuildTestNode("n3", 3000, 8*units.GiB), | ||
| }, | ||
| limits: map[string]int64{ | ||
| "cpu": 12, | ||
| "memory": 32 * units.GiB, | ||
| "gpu": 6, | ||
| }, | ||
| newNode: test.BuildTestNode("n4", 2000, 4*units.GiB), | ||
| nodeDelta: 2, | ||
| wantResult: &CheckDeltaResult{ | ||
| AllowedDelta: 2, | ||
| }, | ||
| }, | ||
| { | ||
| name: "custom resource config exceeded operation", | ||
| crp: &fakeCustomResourcesProcessor{ | ||
| NodeResourceTargets: func(n *apiv1.Node) []customresources.CustomResourceTarget { | ||
| if n.Name == "n1" || n.Name == "n4" { | ||
| return []customresources.CustomResourceTarget{ | ||
| { | ||
| ResourceType: "gpu", | ||
| ResourceCount: 1, | ||
| }, | ||
| } | ||
| } | ||
| return nil | ||
| }, | ||
| }, | ||
| nodes: []*apiv1.Node{ | ||
| test.BuildTestNode("n1", 1000, 2*units.GiB), | ||
| test.BuildTestNode("n2", 2000, 4*units.GiB), | ||
| test.BuildTestNode("n3", 3000, 8*units.GiB), | ||
| }, | ||
| limits: map[string]int64{ | ||
| "cpu": 12, | ||
| "memory": 32 * units.GiB, | ||
| "gpu": 1, | ||
| }, | ||
| newNode: test.BuildTestNode("n4", 2000, 4*units.GiB), | ||
| nodeDelta: 2, | ||
| wantResult: &CheckDeltaResult{ | ||
| AllowedDelta: 0, | ||
| ExceededQuotas: []ExceededQuota{ | ||
| {ID: "cluster-wide", ExceededResources: []string{"gpu"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "node filter config allowed operation", | ||
| nodeFilter: nodeExcludeFn(func(node *apiv1.Node) bool { | ||
| return node.Name == "n3" | ||
| }), | ||
| nodes: []*apiv1.Node{ | ||
| test.BuildTestNode("n1", 1000, 2*units.GiB), | ||
| test.BuildTestNode("n2", 2000, 4*units.GiB), | ||
| test.BuildTestNode("n3", 3000, 8*units.GiB), | ||
| }, | ||
| limits: map[string]int64{ | ||
| "cpu": 4, | ||
| "memory": 8 * units.GiB, | ||
| }, | ||
| newNode: test.BuildTestNode("n4", 1000, 2*units.GiB), | ||
| nodeDelta: 1, | ||
| wantResult: &CheckDeltaResult{ | ||
| AllowedDelta: 1, | ||
| }, | ||
| }, | ||
| { | ||
| name: "node filter config exceeded operation", | ||
| nodeFilter: nodeExcludeFn(func(node *apiv1.Node) bool { | ||
| return node.Name == "n3" | ||
| }), | ||
| nodes: []*apiv1.Node{ | ||
| test.BuildTestNode("n1", 1000, 2*units.GiB), | ||
| test.BuildTestNode("n2", 2000, 4*units.GiB), | ||
| test.BuildTestNode("n3", 3000, 8*units.GiB), | ||
| }, | ||
| limits: map[string]int64{ | ||
| "cpu": 4, | ||
| "memory": 8 * units.GiB, | ||
| }, | ||
| newNode: test.BuildTestNode("n4", 2000, 4*units.GiB), | ||
| nodeDelta: 1, | ||
| wantResult: &CheckDeltaResult{ | ||
| AllowedDelta: 0, | ||
| ExceededQuotas: []ExceededQuota{ | ||
| {ID: "cluster-wide", ExceededResources: []string{"cpu", "memory"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| cloudProvider := cptest.NewTestCloudProviderBuilder().Build() | ||
| resourceLimiter := cloudprovider.NewResourceLimiter(nil, tc.limits) | ||
| cloudProvider.SetResourceLimiter(resourceLimiter) | ||
| ctx := &context.AutoscalingContext{CloudProvider: cloudProvider} | ||
| crp := tc.crp | ||
| if crp == nil { | ||
| crp = &fakeCustomResourcesProcessor{} | ||
| } | ||
| factory := NewTrackerFactory(TrackerOptions{ | ||
| CustomResourcesProcessor: crp, | ||
| QuotaProvider: NewCloudQuotasProvider(cloudProvider), | ||
| NodeFilter: tc.nodeFilter, | ||
| }) | ||
| tracker, err := factory.NewQuotasTracker(ctx, tc.nodes) | ||
| if err != nil { | ||
| t.Errorf("failed to create tracker: %v", err) | ||
| } | ||
| var ng cloudprovider.NodeGroup | ||
| result, err := tracker.CheckDelta(ctx, ng, tc.newNode, tc.nodeDelta) | ||
| if err != nil { | ||
| t.Errorf("failed to check delta: %v", err) | ||
| } | ||
| opts := []cmp.Option{ | ||
| cmpopts.SortSlices(func(a, b string) bool { return a < b }), | ||
| cmpopts.EquateEmpty(), | ||
| } | ||
| if diff := cmp.Diff(tc.wantResult, result, opts...); diff != "" { | ||
| t.Errorf("CheckDelta() mismatch (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is the idea that this will be recreated every loop?
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.
for now yes, see #8662 (comment)