Change:
} } <ins> func TestPodEvaluatorUsageStats(t *testing.T) { cpu1 := api.ResourceList{api.ResourceCPU: resource.MustParse("1")} tests := []struct { name               string objs               []runtime.Object quotaScopes        []corev1.ResourceQuotaScope quotaScopeSelector *corev1.ScopeSelector want               corev1.ResourceList }{ { name: "nil case", }, { name: "all pods in running state", objs: []runtime.Object{ makePod("p1", "", cpu1, api.PodRunning), makePod("p2", "", cpu1, api.PodRunning), }, want: corev1.ResourceList{ corev1.ResourcePods:               resource.MustParse("2"), corev1.ResourceName("count/pods"): resource.MustParse("2"), corev1.ResourceCPU:                resource.MustParse("2"), corev1.ResourceRequestsCPU:        resource.MustParse("2"), corev1.ResourceLimitsCPU:          resource.MustParse("2"), }, }, { name: "one pods in terminal state", objs: []runtime.Object{ makePod("p1", "", cpu1, api.PodRunning), makePod("p2", "", cpu1, api.PodSucceeded), }, want: corev1.ResourceList{ corev1.ResourcePods:               resource.MustParse("1"), corev1.ResourceName("count/pods"): resource.MustParse("2"), corev1.ResourceCPU:                resource.MustParse("1"), corev1.ResourceRequestsCPU:        resource.MustParse("1"), corev1.ResourceLimitsCPU:          resource.MustParse("1"), }, }, { name: "partial pods matching quotaScopeSelector", objs: []runtime.Object{ makePod("p1", "high-priority", cpu1, api.PodRunning), makePod("p2", "high-priority", cpu1, api.PodSucceeded), makePod("p3", "low-priority", cpu1, api.PodRunning), }, quotaScopeSelector: &corev1.ScopeSelector{ MatchExpressions: []corev1.ScopedResourceSelectorRequirement{ { ScopeName: corev1.ResourceQuotaScopePriorityClass, Operator:  corev1.ScopeSelectorOpIn, Values:    []string{"high-priority"}, }, }, }, want: corev1.ResourceList{ corev1.ResourcePods:               resource.MustParse("1"), corev1.ResourceName("count/pods"): resource.MustParse("2"), corev1.ResourceCPU:                resource.MustParse("1"), corev1.ResourceRequestsCPU:        resource.MustParse("1"), corev1.ResourceLimitsCPU:          resource.MustParse("1"), }, }, { name: "partial pods matching quotaScopeSelector - w/ scopeName specified", objs: []runtime.Object{ makePod("p1", "high-priority", cpu1, api.PodRunning), makePod("p2", "high-priority", cpu1, api.PodSucceeded), makePod("p3", "low-priority", cpu1, api.PodRunning), }, quotaScopes: []corev1.ResourceQuotaScope{ corev1.ResourceQuotaScopePriorityClass, }, quotaScopeSelector: &corev1.ScopeSelector{ MatchExpressions: []corev1.ScopedResourceSelectorRequirement{ { ScopeName: corev1.ResourceQuotaScopePriorityClass, Operator:  corev1.ScopeSelectorOpIn, Values:    []string{"high-priority"}, }, }, }, want: corev1.ResourceList{ corev1.ResourcePods:               resource.MustParse("1"), corev1.ResourceName("count/pods"): resource.MustParse("2"), corev1.ResourceCPU:                resource.MustParse("1"), corev1.ResourceRequestsCPU:        resource.MustParse("1"), corev1.ResourceLimitsCPU:          resource.MustParse("1"), }, }, { name: "partial pods matching quotaScopeSelector - w/ multiple scopeNames specified", objs: []runtime.Object{ makePod("p1", "high-priority", cpu1, api.PodRunning), makePod("p2", "high-priority", cpu1, api.PodSucceeded), makePod("p3", "low-priority", cpu1, api.PodRunning), makePod("p4", "high-priority", nil, api.PodFailed), }, quotaScopes: []corev1.ResourceQuotaScope{ corev1.ResourceQuotaScopePriorityClass, corev1.ResourceQuotaScopeBestEffort, }, quotaScopeSelector: &corev1.ScopeSelector{ MatchExpressions: []corev1.ScopedResourceSelectorRequirement{ { ScopeName: corev1.ResourceQuotaScopePriorityClass, Operator:  corev1.ScopeSelectorOpIn, Values:    []string{"high-priority"}, }, }, }, want: corev1.ResourceList{ corev1.ResourceName("count/pods"): resource.MustParse("1"), }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gvr := corev1.SchemeGroupVersion.WithResource("pods") listerForPod := map[schema.GroupVersionResource]cache.GenericLister{ gvr: newGenericLister(gvr.GroupResource(), tt.objs), } evaluator := NewPodEvaluator(mockListerForResourceFunc(listerForPod), testingclock.NewFakeClock(time.Now())) usageStatsOption := quota.UsageStatsOptions{ Scopes:        tt.quotaScopes, ScopeSelector: tt.quotaScopeSelector, } actual, err := evaluator.UsageStats(usageStatsOption) if err != nil { t.Error(err) } if !quota.Equals(tt.want, actual.Used) { t.Errorf("expected: %v, actual: %v", tt.want, actual.Used) } }) } } </ins> func TestPodEvaluatorMatchingScopes(t *testing.T) { fakeClock := testingclock.NewFakeClock(time.Now()) evaluator := NewPodEvaluator(nil, fakeClock)

Discussion:
If a Pod is subjected to a with "PriortyClass" as its scope, the quota's calculation is incorrect. See repo steps for more details. with "PriortyClass" as its scope can be calculated at all times. Create a PriorityClass named "p1"Create a quota with as its scope. Note: w/o specifying , this issue won't happenCreate a Job and wait for its completion.Check quota's usage calculation Here is incorrect - it should be . Worse calculation may happen if I create a regular pod and then delete the Job:The eventual result is totally incorrect. No response master, and possibly all versions. kind <details</details<details</details<details</details<details</details
/sig api-machinery /kind bug
/triage accepted
I would like to take this up!
is already in flight for this