forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_quota_calculate_used.go
104 lines (92 loc) · 2.78 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
97
98
99
100
101
102
103
104
package resourcequota
import (
"reflect"
"github.com/rancher/norman/types/convert"
"github.com/rancher/types/apis/management.cattle.io/v3"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
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
resourceQuotaTemplateLister v3.ResourceQuotaTemplateLister
clusterName string
}
func (c *calculateLimitController) calculateResourceQuotaUsed(key string, ns *corev1.Namespace) error {
if ns == nil {
return nil
}
projectID := getProjectID(ns)
if projectID == "" {
return nil
}
return c.calculateProjectResourceQuota(projectID)
}
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
}
templates, err := c.resourceQuotaTemplateLister.List(c.clusterName, labels.NewSelector())
if err != nil {
return err
}
templatesMap := map[string]*v3.ResourceQuotaTemplate{}
for _, template := range templates {
templatesMap[formatTemplateID(template)] = template
}
nssResourceList := api.ResourceList{}
for _, n := range namespaces {
ns := n.(*corev1.Namespace)
templateID := getTemplateID(ns)
if templateID == "" {
continue
}
nsLimit, err := getNamespaceLimit(ns, templatesMap, false)
if err != nil {
return err
}
nsResourceList, err := 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
}
func convertResourceListToLimit(rList api.ResourceList) (*v3.ProjectResourceLimit, error) {
converted, err := convert.EncodeToMap(rList)
if err != nil {
return nil, err
}
convertedMap := map[string]string{}
for key, value := range converted {
convertedMap[key] = convert.ToString(value)
}
toReturn := &v3.ProjectResourceLimit{}
err = convert.ToObj(convertedMap, toReturn)
return toReturn, err
}