From 76f7076d5a70e3211cf700f1a86a74571f6ab6d4 Mon Sep 17 00:00:00 2001 From: smallteeths <503630985@qq.com> Date: Thu, 28 Dec 2023 18:15:56 +0800 Subject: [PATCH] PANDARIA: Fix project validate which use storageclass quota --- .../v3/project/quota_validate.go | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/pkg/resources/management.cattle.io/v3/project/quota_validate.go b/pkg/resources/management.cattle.io/v3/project/quota_validate.go index cca283cc4..433fdc9da 100644 --- a/pkg/resources/management.cattle.io/v3/project/quota_validate.go +++ b/pkg/resources/management.cattle.io/v3/project/quota_validate.go @@ -1,6 +1,8 @@ package project import ( + "fmt" + mgmtv3 "github.com/rancher/rancher/pkg/apis/management.cattle.io/v3" "github.com/rancher/wrangler/v3/pkg/data/convert" corev1 "k8s.io/api/core/v1" @@ -8,6 +10,14 @@ import ( quotav1 "k8s.io/apiserver/pkg/quota/v1" ) +const ( + // PANDARIA + StorageClassPVCQuotaSuffix = "storageclass.storage.k8s.io/persistentvolumeclaims" + StorageClassStorageQuotaSuffix = "storageclass.storage.k8s.io/requests.storage" + StorageClassPVCQuotaKey = "requestsStorageClassPVC" + StorageClassStorageQuotaKey = "requestsStorageClassStorage" +) + // quotaFits checks whether the quota in the second argument is sufficient for the requested quota in the first argument. // If it is not sufficient, a list of the resources that exceed the allotment is returned. // The ResourceList to be checked can be compiled by passing a @@ -33,11 +43,37 @@ func convertLimitToResourceList(limit *mgmtv3.ResourceQuotaLimit) (corev1.Resour return nil, err } for key, value := range converted { - q, err := resource.ParseQuantity(convert.ToString(value)) - if err != nil { - return nil, err + switch value.(type) { + case string: + q, err := resource.ParseQuantity(convert.ToString(value)) + if err != nil { + return nil, err + } + toReturn[corev1.ResourceName(key)] = q + case map[string]interface{}: + valuemaps := value.(map[string]interface{}) + for k, v := range valuemaps { + valueString, ok := v.(string) + if ok { + q, err := resource.ParseQuantity(valueString) + if err != nil { + return nil, err + } + var rn corev1.ResourceName + if key == StorageClassStorageQuotaKey { + resourceNameStr := fmt.Sprintf("%s.%s", k, StorageClassStorageQuotaSuffix) + rn = corev1.ResourceName(resourceNameStr) + } else if key == StorageClassPVCQuotaKey { + resourceNameStr := fmt.Sprintf("%s.%s", k, StorageClassPVCQuotaSuffix) + rn = corev1.ResourceName(resourceNameStr) + } else { + rn = corev1.ResourceName(key) + } + toReturn[rn] = q + } + } } - toReturn[corev1.ResourceName(key)] = q } + return toReturn, nil }