37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
resource_quota_calculate_used.go 2.52 KB
一键复制 编辑 原始数据 按行查看 历史
package resourcequota
import (
"reflect"
"k8s.io/apimachinery/pkg/runtime"
"fmt"
namespaceutil "github.com/rancher/rancher/pkg/namespace"
validate "github.com/rancher/rancher/pkg/resourcequota"
"github.com/rancher/types/apis/management.cattle.io/v3"
corev1 "k8s.io/api/core/v1"
clientcache "k8s.io/client-go/tools/cache"
api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/quota"
)
/*
collectController is responsible for calculate the combined limit set on the project's Namespaces,
and setting this information in the project
*/
type calculateLimitController struct {
projectLister v3.ProjectLister
projects v3.ProjectInterface
nsIndexer clientcache.Indexer
clusterName string
}
func (c *calculateLimitController) calculateResourceQuotaUsed(key string, ns *corev1.Namespace) (runtime.Object, error) {
if ns == nil {
return nil, nil
}
projectID := getProjectID(ns)
if projectID == "" {
return nil, nil
}
return nil, c.calculateProjectResourceQuota(projectID)
}
func (c *calculateLimitController) calculateResourceQuotaUsedProject(key string, p *v3.Project) (runtime.Object, error) {
if p == nil || p.DeletionTimestamp != nil {
return nil, nil
}
return nil, c.calculateProjectResourceQuota(fmt.Sprintf("%s:%s", c.clusterName, p.Name))
}
func (c *calculateLimitController) calculateProjectResourceQuota(projectID string) error {
projectNamespace, projectName := getProjectNamespaceName(projectID)
project, err := c.projectLister.Get(projectNamespace, projectName)
if err != nil || project.Spec.ResourceQuota == nil {
return err
}
namespaces, err := c.nsIndexer.ByIndex(nsByProjectIndex, projectID)
if err != nil {
return err
}
nssResourceList := api.ResourceList{}
for _, n := range namespaces {
ns := n.(*corev1.Namespace)
if ns.DeletionTimestamp != nil {
continue
}
set, err := namespaceutil.IsNamespaceConditionSet(ns, ResourceQuotaValidatedCondition, true)
if err != nil {
return err
}
if !set {
continue
}
nsLimit, err := getNamespaceResourceQuotaLimit(ns)
if err != nil {
return err
}
nsResourceList, err := validate.ConvertLimitToResourceList(nsLimit)
if err != nil {
return err
}
nssResourceList = quota.Add(nssResourceList, nsResourceList)
}
limit, err := convertResourceListToLimit(nssResourceList)
if err != nil {
return err
}
if reflect.DeepEqual(project.Spec.ResourceQuota.UsedLimit, limit) {
return nil
}
toUpdate := project.DeepCopy()
toUpdate.Spec.ResourceQuota.UsedLimit = *limit
_, err = c.projects.Update(toUpdate)
return err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.4-rc9

搜索帮助