forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_store.go
312 lines (268 loc) · 8.88 KB
/
project_store.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package project
import (
"encoding/json"
"fmt"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
"github.com/rancher/rancher/pkg/clustermanager"
"github.com/rancher/rancher/pkg/resourcequota"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
mgmtschema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
mgmtclient "github.com/rancher/types/client/management/v3"
"github.com/rancher/types/config"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
const roleTemplatesRequired = "authz.management.cattle.io/creator-role-bindings"
const quotaField = "resourceQuota"
const namespaceQuotaField = "namespaceDefaultResourceQuota"
type projectStore struct {
types.Store
projectLister v3.ProjectLister
roleTemplateLister v3.RoleTemplateLister
scaledContext *config.ScaledContext
clusterLister v3.ClusterLister
}
func SetProjectStore(schema *types.Schema, mgmt *config.ScaledContext) {
store := &projectStore{
Store: schema.Store,
projectLister: mgmt.Management.Projects("").Controller().Lister(),
roleTemplateLister: mgmt.Management.RoleTemplates("").Controller().Lister(),
scaledContext: mgmt,
clusterLister: mgmt.Management.Clusters("").Controller().Lister(),
}
schema.Store = store
}
func (s *projectStore) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
annotation, err := s.createProjectAnnotation()
if err != nil {
return nil, err
}
if err := s.validateResourceQuota(apiContext, data, ""); err != nil {
return nil, err
}
values.PutValue(data, annotation, "annotations", roleTemplatesRequired)
return s.Store.Create(apiContext, schema, data)
}
func (s *projectStore) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
if err := s.validateResourceQuota(apiContext, data, id); err != nil {
return nil, err
}
return s.Store.Update(apiContext, schema, data, id)
}
func (s *projectStore) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
parts := strings.Split(id, ":")
proj, err := s.projectLister.Get(parts[0], parts[len(parts)-1])
if err != nil {
return nil, err
}
if proj.Labels["authz.management.cattle.io/system-project"] == "true" {
return nil, httperror.NewAPIError(httperror.MethodNotAllowed, "System Project cannot be deleted")
}
return s.Store.Delete(apiContext, schema, id)
}
func (s *projectStore) createProjectAnnotation() (string, error) {
rt, err := s.roleTemplateLister.List("", labels.NewSelector())
if err != nil {
return "", err
}
annoMap := make(map[string][]string)
for _, role := range rt {
if role.ProjectCreatorDefault && !role.Locked {
annoMap["required"] = append(annoMap["required"], role.Name)
}
}
d, err := json.Marshal(annoMap)
if err != nil {
return "", err
}
return string(d), nil
}
func (s *projectStore) validateResourceQuota(apiContext *types.APIContext, data map[string]interface{}, id string) error {
quotaO, quotaOk := data[quotaField]
if quotaO == nil {
quotaOk = false
}
nsQuotaO, namespaceQuotaOk := data[namespaceQuotaField]
if nsQuotaO == nil {
namespaceQuotaOk = false
}
if quotaOk != namespaceQuotaOk {
if quotaOk {
return httperror.NewFieldAPIError(httperror.MissingRequired, namespaceQuotaField, "")
}
return httperror.NewFieldAPIError(httperror.MissingRequired, quotaField, "")
} else if !quotaOk {
return nil
}
var nsQuota mgmtclient.NamespaceResourceQuota
if err := convert.ToObj(nsQuotaO, &nsQuota); err != nil {
return err
}
var projectQuota mgmtclient.ProjectResourceQuota
if err := convert.ToObj(quotaO, &projectQuota); err != nil {
return err
}
projectQuotaLimit, err := limitToLimit(projectQuota.Limit)
if err != nil {
return err
}
nsQuotaLimit, err := limitToLimit(nsQuota.Limit)
if err != nil {
return err
}
// limits in namespace default quota should include all limits defined in the project quota
projectQuotaLimitMap, err := convert.EncodeToMap(projectQuotaLimit)
if err != nil {
return err
}
nsQuotaLimitMap, err := convert.EncodeToMap(nsQuotaLimit)
if err != nil {
return err
}
if len(nsQuotaLimitMap) != len(projectQuotaLimitMap) {
return httperror.NewFieldAPIError(httperror.MissingRequired, namespaceQuotaField, fmt.Sprintf("does not have all fields defined on a %s", quotaField))
}
for k := range projectQuotaLimitMap {
if _, ok := nsQuotaLimitMap[k]; !ok {
return httperror.NewFieldAPIError(httperror.MissingRequired, namespaceQuotaField, fmt.Sprintf("misses %s defined on a %s", k, quotaField))
}
}
return s.isQuotaFit(apiContext, nsQuotaLimit, projectQuotaLimit, id)
}
func (s *projectStore) isQuotaFit(apiContext *types.APIContext, nsQuotaLimit *v3.ResourceQuotaLimit,
projectQuotaLimit *v3.ResourceQuotaLimit, id string) error {
// check that namespace default quota is within project quota
isFit, msg, err := resourcequota.IsQuotaFit(nsQuotaLimit, []*v3.ResourceQuotaLimit{}, projectQuotaLimit)
if err != nil {
return err
}
if !isFit {
return httperror.NewFieldAPIError(httperror.MaxLimitExceeded, namespaceQuotaField, fmt.Sprintf("exceeds %s on fields: %s",
quotaField, msg))
}
if id == "" {
return nil
}
var project mgmtclient.Project
if err := access.ByID(apiContext, &mgmtschema.Version, mgmtclient.ProjectType, id, &project); err != nil {
return err
}
// check if fields were added or removed
// and update project's namespaces accordingly
defaultQuotaLimitMap, err := convert.EncodeToMap(nsQuotaLimit)
if err != nil {
return err
}
usedQuotaLimitMap := map[string]interface{}{}
if project.ResourceQuota != nil && project.ResourceQuota.UsedLimit != nil {
usedQuotaLimitMap, err = convert.EncodeToMap(project.ResourceQuota.UsedLimit)
if err != nil {
return err
}
}
limitToAdd := map[string]interface{}{}
limitToRemove := map[string]interface{}{}
for key, value := range defaultQuotaLimitMap {
if _, ok := usedQuotaLimitMap[key]; !ok {
limitToAdd[key] = value
}
}
for key, value := range usedQuotaLimitMap {
if _, ok := defaultQuotaLimitMap[key]; !ok {
limitToRemove[key] = value
}
}
// check that used quota is not bigger than the project quota
for key := range limitToRemove {
delete(usedQuotaLimitMap, key)
}
var usedLimitToCheck mgmtclient.ResourceQuotaLimit
err = convert.ToObj(usedQuotaLimitMap, &usedLimitToCheck)
if err != nil {
return err
}
usedQuotaLimit, err := limitToLimit(&usedLimitToCheck)
if err != nil {
return err
}
isFit, msg, err = resourcequota.IsQuotaFit(usedQuotaLimit, []*v3.ResourceQuotaLimit{}, projectQuotaLimit)
if err != nil {
return err
}
if !isFit {
return httperror.NewFieldAPIError(httperror.MaxLimitExceeded, quotaField, fmt.Sprintf("is below the used limit on fields: %s",
msg))
}
if len(limitToAdd) == 0 && len(limitToRemove) == 0 {
return nil
}
// check if default quota is enough to set on namespaces
toAppend := &mgmtclient.ResourceQuotaLimit{}
if err := mapstructure.Decode(limitToAdd, toAppend); err != nil {
return err
}
converted, err := limitToLimit(toAppend)
if err != nil {
return err
}
mu := resourcequota.GetProjectLock(id)
mu.Lock()
defer mu.Unlock()
namespacesCount, err := s.getNamespacesCount(apiContext, project)
if err != nil {
return err
}
var nsLimits []*v3.ResourceQuotaLimit
for i := 0; i < namespacesCount; i++ {
nsLimits = append(nsLimits, converted)
}
isFit, msg, err = resourcequota.IsQuotaFit(&v3.ResourceQuotaLimit{}, nsLimits, projectQuotaLimit)
if err != nil {
return err
}
if !isFit {
return httperror.NewFieldAPIError(httperror.MaxLimitExceeded, namespaceQuotaField,
fmt.Sprintf("exceeds project limit on fields %s when applied to all namespaces in a project",
msg))
}
return nil
}
func (s *projectStore) getNamespacesCount(apiContext *types.APIContext, project mgmtclient.Project) (int, error) {
cluster, err := s.clusterLister.Get("", project.ClusterID)
if err != nil {
return 0, err
}
kubeConfig, err := clustermanager.ToRESTConfig(cluster, s.scaledContext)
if kubeConfig == nil || err != nil {
return 0, err
}
clusterContext, err := config.NewUserContext(s.scaledContext, *kubeConfig, cluster.Name)
if err != nil {
return 0, err
}
namespaces, err := clusterContext.Core.Namespaces("").List(metav1.ListOptions{})
if err != nil {
return 0, err
}
count := 0
for _, n := range namespaces.Items {
if n.Annotations == nil {
continue
}
if n.Annotations["field.cattle.io/projectId"] == project.ID {
count++
}
}
return count, nil
}
func limitToLimit(from *mgmtclient.ResourceQuotaLimit) (*v3.ResourceQuotaLimit, error) {
var to v3.ResourceQuotaLimit
err := convert.ToObj(from, &to)
return &to, err
}