-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
181 lines (158 loc) · 5.56 KB
/
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
package namespace
import (
"fmt"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
"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"
clusterclient "github.com/rancher/types/client/cluster/v3"
mgmtclient "github.com/rancher/types/client/management/v3"
)
const quotaField = "resourceQuota"
const containerRecsourceLimitField = "containerDefaultResourceLimit"
func New(store types.Store) types.Store {
t := &transform.Store{
Store: store,
Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
anns, _ := data["annotations"].(map[string]interface{})
if anns["management.cattle.io/system-namespace"] == "true" {
return nil, nil
}
return data, nil
},
}
return &Store{
Store: t,
}
}
type Store struct {
types.Store
}
func (p *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
if _, ok := data["resourceQuota"]; ok {
values.PutValue(data, "{\"conditions\": [{\"type\": \"InitialRolesPopulated\", \"status\": \"Unknown\", \"message\": \"Populating initial roles\"},{\"type\": \"ResourceQuotaValidated\", \"status\": \"Unknown\", \"message\": \"Validating resource quota\"}]}",
"annotations", "cattle.io/status")
} else {
values.PutValue(data, "{\"conditions\": [{\"type\": \"InitialRolesPopulated\", \"status\": \"Unknown\", \"message\": \"Populating initial roles\"}]}",
"annotations", "cattle.io/status")
}
if err := p.validateResourceQuota(apiContext, schema, data, "", false); err != nil {
return nil, err
}
return p.Store.Create(apiContext, schema, data)
}
func (p *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
if err := p.validateResourceQuota(apiContext, schema, data, id, true); err != nil {
return nil, err
}
return p.Store.Update(apiContext, schema, data, id)
}
func (p *Store) validateResourceQuota(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string, update bool) error {
quota := data[quotaField]
projectID := convert.ToString(data["projectId"])
if update {
var ns clusterclient.Namespace
if err := access.ByID(apiContext, &schema.Version, clusterclient.NamespaceType, id, &ns); err != nil {
return err
}
projectID = ns.ProjectID
}
if projectID == "" {
return nil
}
var project mgmtclient.Project
if err := access.ByID(apiContext, &mgmtschema.Version, mgmtclient.ProjectType, projectID, &project); err != nil {
return err
}
if project.ResourceQuota == nil {
return nil
}
var nsQuota mgmtclient.NamespaceResourceQuota
if quota == nil {
if project.NamespaceDefaultResourceQuota == nil {
return nil
}
nsQuota = *project.NamespaceDefaultResourceQuota
} else {
if err := convert.ToObj(quota, &nsQuota); err != nil {
return err
}
}
projectQuotaLimit, err := limitToLimit(project.ResourceQuota.Limit)
if err != nil {
return err
}
nsQuotaLimit, err := limitToLimit(nsQuota.Limit)
if err != nil {
return err
}
// limits in namespace should include all limits defined on a project
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, quotaField, "does not have all fields defined on a project quota")
}
for k := range projectQuotaLimitMap {
if _, ok := nsQuotaLimitMap[k]; !ok {
return httperror.NewFieldAPIError(httperror.MissingRequired, quotaField, fmt.Sprintf("misses %s defined on a project quota", k))
}
}
// validate resource quota
mu := resourcequota.GetProjectLock(projectID)
mu.Lock()
defer mu.Unlock()
var nsLimits []*v3.ResourceQuotaLimit
var namespaces []clusterclient.Namespace
options := &types.QueryOptions{
Conditions: []*types.QueryCondition{
types.NewConditionFromString("projectId", types.ModifierEQ, convert.ToString(data["projectId"])),
},
}
if err := access.List(apiContext, &schema.Version, clusterclient.NamespaceType, options, &namespaces); err != nil {
return err
}
for _, n := range namespaces {
if n.ID == id {
continue
}
if n.ResourceQuota == nil {
continue
}
nsLimit, err := limitToLimitCluster(n.ResourceQuota.Limit)
if err != nil {
return err
}
nsLimits = append(nsLimits, nsLimit)
}
// set default resource limit
limit := data[containerRecsourceLimitField]
if limit == nil {
data[containerRecsourceLimitField] = project.ContainerDefaultResourceLimit
}
isFit, msg, err := resourcequota.IsQuotaFit(nsQuotaLimit, nsLimits, projectQuotaLimit)
if err != nil || isFit {
return err
}
return httperror.NewFieldAPIError(httperror.MaxLimitExceeded, quotaField, fmt.Sprintf("exceeds projectLimit on fields: %s", msg))
}
func limitToLimit(from *mgmtclient.ResourceQuotaLimit) (*v3.ResourceQuotaLimit, error) {
var to v3.ResourceQuotaLimit
err := convert.ToObj(from, &to)
return &to, err
}
func limitToLimitCluster(from *clusterclient.ResourceQuotaLimit) (*v3.ResourceQuotaLimit, error) {
var to v3.ResourceQuotaLimit
err := convert.ToObj(from, &to)
return &to, err
}