forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_quota_calculate_used.go
96 lines (84 loc) · 2.52 KB
/
resource_quota_calculate_used.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
}