forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource.go
135 lines (118 loc) · 3.71 KB
/
resource.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// WUTONG, Application Management Platform
// Copyright (C) 2020-2020 Wutong Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Wutong,
// one or multiple Commercial Licenses authorized by Wutong Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package store
import (
"sync"
v1 "github.com/wutong-paas/wutong/worker/appm/types/v1"
corev1 "k8s.io/api/core/v1"
)
// ResourceCache resource cache
type ResourceCache struct {
lock sync.Mutex
resources map[string]*NamespaceResource
}
// NewResourceCache new resource cache
func NewResourceCache() *ResourceCache {
return &ResourceCache{
resources: make(map[string]*NamespaceResource),
}
}
// NamespaceResource namespace resource
type NamespaceResource map[string]*v1.PodResource
// SetPodResource set pod resource
func (r *NamespaceResource) SetPodResource(podName string, pr *v1.PodResource) {
(*r)[podName] = pr
}
// RemovePod remove pod resource
func (r *NamespaceResource) RemovePod(podName string) {
delete(*r, podName)
}
// TenantEnvResource tenant env resource
type TenantEnvResource struct {
Namespace string
MemoryRequest int64
MemoryLimit int64
CPURequest int64
CPULimit int64
}
// SetPodResource set pod resource
func (r *ResourceCache) SetPodResource(pod *corev1.Pod) {
r.lock.Lock()
defer r.lock.Unlock()
namespace := pod.Namespace
re := v1.CalculatePodResource(pod)
// Compatible with resources with tenantEnvID as namespace
nsKeys := []string{namespace}
labels := pod.Labels
if tenantEnvID, ok := labels["tenant_env_id"]; ok && tenantEnvID != namespace {
nsKeys = append(nsKeys, tenantEnvID)
}
for _, ns := range nsKeys {
if nr, ok := r.resources[ns]; ok && nr != nil {
nr.SetPodResource(pod.Name, re)
} else {
nameR := make(NamespaceResource)
nameR.SetPodResource(pod.Name, re)
r.resources[ns] = &nameR
}
}
}
// RemovePod remove pod resource
func (r *ResourceCache) RemovePod(pod *corev1.Pod) {
r.lock.Lock()
defer r.lock.Unlock()
namespace := pod.Namespace
nsKeys := []string{namespace}
labels := pod.Labels
if tenantEnvID, ok := labels["tenant_env_id"]; ok && tenantEnvID != namespace {
nsKeys = append(nsKeys, tenantEnvID)
}
for _, ns := range nsKeys {
if nr, ok := r.resources[ns]; ok && nr != nil {
nr.RemovePod(pod.Name)
}
}
}
// GetTenantEnvResource get tenant env resource
func (r *ResourceCache) GetTenantEnvResource(namespace string) (tr TenantEnvResource) {
r.lock.Lock()
defer r.lock.Unlock()
tr = r.getTenantEnvResource(r.resources[namespace])
tr.Namespace = namespace
return tr
}
func (r *ResourceCache) getTenantEnvResource(namespaceRe *NamespaceResource) (tr TenantEnvResource) {
if namespaceRe == nil {
return
}
for _, v := range *namespaceRe {
tr.CPULimit += v.CPULimit
tr.MemoryLimit += v.MemoryLimit
tr.CPURequest += v.CPURequest
tr.MemoryRequest += v.MemoryRequest
}
return
}
// GetAllTenantEnvResource get all tenant env resources
func (r *ResourceCache) GetAllTenantEnvResource() (trs []TenantEnvResource) {
r.lock.Lock()
defer r.lock.Unlock()
for k := range r.resources {
tr := r.getTenantEnvResource(r.resources[k])
tr.Namespace = k
trs = append(trs, tr)
}
return
}