Skip to content

Commit

Permalink
PANDARIA: Fix project validate which use storageclass quota
Browse files Browse the repository at this point in the history
  • Loading branch information
smallteeths authored and JacieChao committed Nov 26, 2024
1 parent addfde3 commit 76f7076
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions pkg/resources/management.cattle.io/v3/project/quota_validate.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
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"
"k8s.io/apimachinery/pkg/api/resource"
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
Expand All @@ -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
}

0 comments on commit 76f7076

Please sign in to comment.