Skip to content

Commit

Permalink
Count storage requests as whole byte values in quota
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Sep 8, 2020
1 parent 73375fb commit 9ee6b7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pkg/quota/v1/evaluator/core/persistent_volume_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ func (p *pvcEvaluator) Usage(item runtime.Object) (corev1.ResourceList, error) {

// charge for storage
if request, found := pvc.Spec.Resources.Requests[corev1.ResourceStorage]; found {
roundedRequest := request.DeepCopy()
if !roundedRequest.RoundUp(0) {
// Ensure storage requests are counted as whole byte values, to pass resourcequota validation.
// See http://issue.k8s.io/94313
request = roundedRequest
}

result[corev1.ResourceRequestsStorage] = request
// charge usage to the storage class (if present)
if len(storageClassRef) > 0 {
Expand Down
27 changes: 26 additions & 1 deletion pkg/quota/v1/evaluator/core/persistent_volume_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func TestPersistentVolumeClaimEvaluatorUsage(t *testing.T) {
StorageClassName: &classGold,
})

validClaimWithNonIntegerStorage := validClaim.DeepCopy()
validClaimWithNonIntegerStorage.Spec.Resources.Requests[api.ResourceName(api.ResourceStorage)] = resource.MustParse("1001m")

validClaimByStorageClassWithNonIntegerStorage := validClaimByStorageClass.DeepCopy()
validClaimByStorageClassWithNonIntegerStorage.Spec.Resources.Requests[api.ResourceName(api.ResourceStorage)] = resource.MustParse("1001m")

evaluator := NewPersistentVolumeClaimEvaluator(nil)
testCases := map[string]struct {
pvc *api.PersistentVolumeClaim
Expand All @@ -100,14 +106,33 @@ func TestPersistentVolumeClaimEvaluatorUsage(t *testing.T) {
generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "persistentvolumeclaims"}): resource.MustParse("1"),
},
},

"pvc-usage-rounded": {
pvc: validClaimWithNonIntegerStorage,
usage: corev1.ResourceList{
corev1.ResourceRequestsStorage: resource.MustParse("2"), // 1001m -> 2
corev1.ResourcePersistentVolumeClaims: resource.MustParse("1"),
generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "persistentvolumeclaims"}): resource.MustParse("1"),
},
},
"pvc-usage-by-class-rounded": {
pvc: validClaimByStorageClassWithNonIntegerStorage,
usage: corev1.ResourceList{
corev1.ResourceRequestsStorage: resource.MustParse("2"), // 1001m -> 2
corev1.ResourcePersistentVolumeClaims: resource.MustParse("1"),
V1ResourceByStorageClass(classGold, corev1.ResourceRequestsStorage): resource.MustParse("2"), // 1001m -> 2
V1ResourceByStorageClass(classGold, corev1.ResourcePersistentVolumeClaims): resource.MustParse("1"),
generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "persistentvolumeclaims"}): resource.MustParse("1"),
},
},
}
for testName, testCase := range testCases {
actual, err := evaluator.Usage(testCase.pvc)
if err != nil {
t.Errorf("%s unexpected error: %v", testName, err)
}
if !quota.Equals(testCase.usage, actual) {
t.Errorf("%s expected: %v, actual: %v", testName, testCase.usage, actual)
t.Errorf("%s expected:\n%v\n, actual:\n%v", testName, testCase.usage, actual)
}
}
}

0 comments on commit 9ee6b7f

Please sign in to comment.