Skip to content

Commit 145e476

Browse files
authored
refactor(device): remove dead QuotaManager.FitQuota (#3627)
Signed-off-by: G1en-114 <1328216639@qq.com>
1 parent 274d780 commit 145e476

5 files changed

Lines changed: 28 additions & 37 deletions

File tree

‎pkg/device/quota.go‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Quota struct {
3131
Limit int64
3232
// LimitSet distinguishes an explicitly configured limit (including an
3333
// explicit 0, which blocks all usage) from an entry auto-created by usage
34-
// tracking, which carries Limit 0 but means "no limit configured". FitQuota
34+
// tracking, which carries Limit 0 but means "no limit configured". fitQuotaLocked
3535
// gates on this rather than Limit != 0 so that a ResourceQuota of "0" is
3636
// honored as a hard block instead of being read as unlimited.
3737
LimitSet bool
@@ -104,20 +104,6 @@ func PodQuotaUsage(pod *corev1.Pod, allocated *PodDevices, devType string, tenta
104104
return mem, core
105105
}
106106

107-
// FitQuota checks a request against the quotas without scopes of the
108-
// namespace. Use FitQuotaForPod when the pod is known, so that the quotas
109-
// with scopes that match it are checked too.
110-
func (q *QuotaManager) FitQuota(ns string, memreq int64, coresreq int64, deviceName string) bool {
111-
devs, ok := GetDevices()[deviceName]
112-
if !ok {
113-
return true
114-
}
115-
resourceNames := devs.GetResourceNames()
116-
q.mutex.RLock()
117-
defer q.mutex.RUnlock()
118-
return q.fitQuotaLocked(ns, resourceNames.ResourceMemoryName, resourceNames.ResourceCoreName, memreq, coresreq)
119-
}
120-
121107
// FitQuotaForPod checks a request of pod against the quotas without scopes of
122108
// its namespace and against the quotas with scopes that match the pod.
123109
func (q *QuotaManager) FitQuotaForPod(pod *corev1.Pod, memreq int64, coresreq int64, deviceName string) bool {
@@ -306,7 +292,7 @@ func (q *QuotaManager) DelQuota(quota *corev1.ResourceQuota) {
306292

307293
// UpdateQuota swaps oldQuota's limits for newQuota's without releasing the lock
308294
// in between. Doing it as DelQuota followed by AddQuota would leave the limits
309-
// zeroed for the gap between the two, and FitQuota reads a zero limit as no
295+
// zeroed for the gap between the two, and FitQuotaForPod reads a zero limit as no
310296
// limit, so a check landing in that gap is waved through. The kube quota
311297
// controller rewrites status.used on every pod event, so updates arrive
312298
// constantly while pods are being scheduled.

‎pkg/device/quota_scope_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestScopedQuotaLimitsMatchingPods(t *testing.T) {
151151
if !qm.FitQuotaForPod(scopePod("new", "low"), 8000, 0, "NVIDIA") {
152152
t.Error("the high quota limits a low pod")
153153
}
154-
if !qm.FitQuota(scopeTestNS, 8000, 0, "NVIDIA") {
154+
if !qm.FitQuotaForPod(scopePod("new", ""), 8000, 0, "NVIDIA") {
155155
t.Error("a scoped quota became a namespace limit")
156156
}
157157

‎pkg/device/quota_test.go‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ func TestNewQuotaManagerSingleton(t *testing.T) {
134134
}
135135
}
136136

137+
// nsPod returns a pod in ns that no scoped quota matches by its fields alone.
138+
func nsPod(ns string) *corev1.Pod {
139+
return &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "fit", Namespace: ns}}
140+
}
141+
137142
func TestFitQuota(t *testing.T) {
138143
initTest()
139144
qm := NewQuotaManager()
@@ -148,24 +153,24 @@ func TestFitQuota(t *testing.T) {
148153
}
149154

150155
// Should fit
151-
if !qm.FitQuota(ns, 500, 100, deviceName) {
152-
t.Error("FitQuota should return true when within limits")
156+
if !qm.FitQuotaForPod(nsPod(ns), 500, 100, deviceName) {
157+
t.Error("FitQuotaForPod should return true when within limits")
153158
}
154159
// Should not fit memory
155-
if qm.FitQuota(ns, 1500, 100, deviceName) {
156-
t.Error("FitQuota should return false when memory exceeds limit")
160+
if qm.FitQuotaForPod(nsPod(ns), 1500, 100, deviceName) {
161+
t.Error("FitQuotaForPod should return false when memory exceeds limit")
157162
}
158163
// Should not fit core
159-
if qm.FitQuota(ns, 500, 300, deviceName) {
160-
t.Error("FitQuota should return false when core exceeds limit")
164+
if qm.FitQuotaForPod(nsPod(ns), 500, 300, deviceName) {
165+
t.Error("FitQuotaForPod should return false when core exceeds limit")
161166
}
162167
// Should fit if namespace not present
163-
if !qm.FitQuota("otherns", 1500, 100, deviceName) {
164-
t.Error("FitQuota should return true if namespace not present")
168+
if !qm.FitQuotaForPod(nsPod("otherns"), 1500, 100, deviceName) {
169+
t.Error("FitQuotaForPod should return true if namespace not present")
165170
}
166171
// Should fit if device not present
167-
if !qm.FitQuota(ns, 1000, 100, "unknown-device") {
168-
t.Error("FitQuota should return true if device not present")
172+
if !qm.FitQuotaForPod(nsPod(ns), 1000, 100, "unknown-device") {
173+
t.Error("FitQuotaForPod should return true if device not present")
169174
}
170175
}
171176

@@ -184,8 +189,8 @@ func TestFitQuotaExplicitZeroBlocks(t *testing.T) {
184189
qm.AddQuota(rq)
185190

186191
// The explicit zero must block any positive request, not be read as "unlimited".
187-
if qm.FitQuota(ns, 500, 0, "NVIDIA") {
188-
t.Error(`FitQuota admitted a 500 request under limits.nvidia.com/gpumem: "0"; an explicit zero quota must block`)
192+
if qm.FitQuotaForPod(nsPod(ns), 500, 0, "NVIDIA") {
193+
t.Error(`FitQuotaForPod admitted a 500 request under limits.nvidia.com/gpumem: "0"; an explicit zero quota must block`)
189194
}
190195
}
191196

@@ -207,12 +212,12 @@ func TestFitQuotaUsesScaledLimit(t *testing.T) {
207212

208213
// 100 units are 25600 MiB, and requests arrive already scaled
209214
// Should fit at the scaled limit
210-
if !qm.FitQuota(ns, 25600, 0, "MLU") {
211-
t.Error("FitQuota should return true when memory equals the scaled limit")
215+
if !qm.FitQuotaForPod(nsPod(ns), 25600, 0, "MLU") {
216+
t.Error("FitQuotaForPod should return true when memory equals the scaled limit")
212217
}
213218
// Should not fit above the scaled limit
214-
if qm.FitQuota(ns, 25601, 0, "MLU") {
215-
t.Error("FitQuota should return false when memory exceeds the scaled limit")
219+
if qm.FitQuotaForPod(nsPod(ns), 25601, 0, "MLU") {
220+
t.Error("FitQuotaForPod should return false when memory exceeds the scaled limit")
216221
}
217222
}
218223

@@ -469,7 +474,7 @@ func TestDelQuotaNonLimitsKey(t *testing.T) {
469474
}
470475

471476
// A ResourceQuota update must never leave the limits zeroed, even briefly.
472-
// FitQuota reads a zero limit as no limit, so a check landing mid-update would
477+
// FitQuotaForPod reads a zero limit as no limit, so a check landing mid-update would
473478
// be waved through. The kube quota controller rewrites status.used on every pod
474479
// event, so updates arrive exactly when pods are being scheduled.
475480
func TestUpdateQuotaKeepsLimitEnforced(t *testing.T) {
@@ -507,7 +512,7 @@ func TestUpdateQuotaKeepsLimitEnforced(t *testing.T) {
507512

508513
wg.Go(func() {
509514
for range 50000 {
510-
if qm.FitQuota(ns, 1, 0, "NVIDIA") {
515+
if qm.FitQuotaForPod(nsPod(ns), 1, 0, "NVIDIA") {
511516
atomic.AddInt64(&wrongAdmits, 1)
512517
}
513518
}

‎pkg/scheduler/scheduler.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ func (s *Scheduler) onAddQuota(obj any) {
880880

881881
// onUpdateQuota applies both halves of the change under a single lock. Doing
882882
// this as a delete followed by an add leaves the limits zeroed in between, and
883-
// FitQuota reads a zero limit as no limit, so quota goes unenforced for the gap.
883+
// FitQuotaForPod reads a zero limit as no limit, so quota goes unenforced for the gap.
884884
func (s *Scheduler) onUpdateQuota(oldObj, newObj any) {
885885
oldQuota, ok := asResourceQuota(oldObj)
886886
if !ok {

‎pkg/scheduler/scheduler_pod_lifecycle_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ func TestPodReplayRestoresInitContainerUsage(t *testing.T) {
627627
}
628628
require.Equal(t, wantMemory, replayQuotaUsage(s, pod.Namespace, "hami.io/gpumem"))
629629
require.Equal(t, wantCores, replayQuotaUsage(s, pod.Namespace, "hami.io/gpucores"))
630-
if fit := s.quotaManager.FitQuota(pod.Namespace, 10000, 0, nvidia.NvidiaGPUDevice); fit != (wantMemory+10000 <= 25000) {
630+
if fit := s.quotaManager.FitQuotaForPod(pod, 10000, 0, nvidia.NvidiaGPUDevice); fit != (wantMemory+10000 <= 25000) {
631631
t.Errorf("quota fit = %t with restored usage %d", fit, wantMemory)
632632
}
633633
}

0 commit comments

Comments
 (0)