代码拉取完成,页面将自动刷新
package resourcequota
import (
"sort"
"strings"
"sync"
"time"
"github.com/rancher/norman/types/convert"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/cache"
api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/quota"
)
var (
projectLockCache = cache.NewLRUExpireCache(1000)
)
func GetProjectLock(projectID string) *sync.Mutex {
val, ok := projectLockCache.Get(projectID)
if !ok {
projectLockCache.Add(projectID, &sync.Mutex{}, time.Hour)
val, _ = projectLockCache.Get(projectID)
}
mu := val.(*sync.Mutex)
return mu
}
func IsQuotaFit(nsLimit *v3.ResourceQuotaLimit, nsLimits []*v3.ResourceQuotaLimit, projectLimit *v3.ResourceQuotaLimit) (bool, string, error) {
nssResourceList := api.ResourceList{}
nsResourceList, err := ConvertLimitToResourceList(nsLimit)
if err != nil {
return false, "", err
}
nssResourceList = quota.Add(nssResourceList, nsResourceList)
for _, nsLimit := range nsLimits {
nsResourceList, err := ConvertLimitToResourceList(nsLimit)
if err != nil {
return false, "", err
}
nssResourceList = quota.Add(nssResourceList, nsResourceList)
}
projectResourceList, err := ConvertLimitToResourceList(projectLimit)
if err != nil {
return false, "", err
}
allowed, exceeded := quota.LessThanOrEqual(nssResourceList, projectResourceList)
if allowed {
return true, "", nil
}
failedHard := quota.Mask(nssResourceList, exceeded)
return false, prettyPrint(failedHard), nil
}
func ConvertLimitToResourceList(limit *v3.ResourceQuotaLimit) (api.ResourceList, error) {
toReturn := api.ResourceList{}
converted, err := convert.EncodeToMap(limit)
if err != nil {
return nil, err
}
for key, value := range converted {
q, err := resource.ParseQuantity(convert.ToString(value))
if err != nil {
return nil, err
}
toReturn[api.ResourceName(key)] = q
}
return toReturn, nil
}
func prettyPrint(item api.ResourceList) string {
parts := []string{}
keys := []string{}
for key := range item {
keys = append(keys, string(key))
}
sort.Strings(keys)
for _, key := range keys {
value := item[api.ResourceName(key)]
constraint := key + "=" + value.String()
parts = append(parts, constraint)
}
return strings.Join(parts, ",")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。