This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
quotas.go
415 lines (350 loc) · 9.64 KB
/
quotas.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package quotas
import (
"github.com/ciao-project/ciao/ciao-controller/types"
"github.com/ciao-project/ciao/payloads"
)
type quota struct {
limit int
consumed int
}
type tenantData struct {
quotas map[payloads.Resource]*quota
perInstanceVCPUs int
perInstanceMemory int
perVolumeSize int
}
// Quotas provides a quota and limit service
type Quotas struct {
ch chan interface{}
}
// Result provides a method for querying the result of a Consume operation.
type Result interface {
Allowed() bool
Reason() string
Resources() []payloads.RequestedResource
}
type consumeOp struct {
tenantID string
resources []payloads.RequestedResource
ch chan Result
}
type releaseOp struct {
tenantID string
resources []payloads.RequestedResource
}
type updateOp struct {
tenantID string
quotas []types.QuotaDetails
doneCh chan struct{}
}
type dumpOp struct {
tenantID string
ch chan []types.QuotaDetails
}
type deleteTenantOp struct {
tenantID string
doneCh chan struct{}
}
type result struct {
allowed bool
reason string
resources []payloads.RequestedResource
}
var supportedResources = [...]payloads.Resource{
payloads.VCPUs,
payloads.MemMB,
payloads.Volume,
payloads.SharedDiskGiB,
payloads.Instance,
payloads.Image,
payloads.ExternalIP,
}
func makeTentantData() *tenantData {
td := tenantData{}
td.quotas = make(map[payloads.Resource]*quota)
for _, resource := range supportedResources {
td.quotas[resource] = "a{-1, 0}
}
td.perInstanceMemory = -1
td.perInstanceVCPUs = -1
td.perVolumeSize = -1
return &td
}
func getTenantData(tenantDetails map[string]*tenantData, tenantID string) *tenantData {
td, ok := tenantDetails[tenantID]
if !ok {
td = makeTentantData()
tenantDetails[tenantID] = td
}
return td
}
func consumeQuota(tenantDetails map[string]*tenantData, op *consumeOp) Result {
td := getTenantData(tenantDetails, op.tenantID)
allowed := true
for _, r := range op.resources {
q, ok := td.quotas[r.Type]
if ok {
q.consumed += r.Value
if q.limit > -1 && q.consumed > q.limit {
allowed = false
}
}
}
res := &result{resources: op.resources}
res.allowed = allowed
if !allowed {
// TODO: produce more precise reason
res.reason = "Over quota"
}
return res
}
func checkLimit(tenantDetails map[string]*tenantData, op *consumeOp) Result {
td := getTenantData(tenantDetails, op.tenantID)
allowed := true
for _, r := range op.resources {
switch r.Type {
case payloads.VCPUs:
if td.perInstanceVCPUs > -1 && r.Value > td.perInstanceVCPUs {
allowed = false
}
case payloads.MemMB:
if td.perInstanceMemory > -1 && r.Value > td.perInstanceMemory {
allowed = false
}
case payloads.SharedDiskGiB:
if td.perVolumeSize > -1 && r.Value > td.perVolumeSize {
allowed = false
}
}
}
res := &result{resources: op.resources}
res.allowed = allowed
if !allowed {
// TODO: produce more precise reason
res.reason = "Over limit"
}
return res
}
func release(tenantDetails map[string]*tenantData, op *releaseOp) {
td := getTenantData(tenantDetails, op.tenantID)
for _, r := range op.resources {
q, ok := td.quotas[r.Type]
if ok {
q.consumed -= r.Value
if q.consumed < 0 {
q.consumed = 0
}
}
}
}
func quotaNameToResource(name string) payloads.Resource {
switch name {
case "tenant-vcpu-quota":
return payloads.VCPUs
case "tenant-mem-quota":
return payloads.MemMB
case "tenant-storage-quota":
return payloads.SharedDiskGiB
case "tenant-volumes-quota":
return payloads.Volume
case "tenant-instances-quota":
return payloads.Instance
case "tenant-images-quota":
return payloads.Image
case "tenant-external-ips-quota":
return payloads.ExternalIP
}
return ""
}
func resourceToQuotaName(r payloads.Resource) string {
switch r {
case payloads.VCPUs:
return "tenant-vcpu-quota"
case payloads.MemMB:
return "tenant-mem-quota"
case payloads.Volume:
return "tenant-volumes-quota"
case payloads.SharedDiskGiB:
return "tenant-storage-quota"
case payloads.Instance:
return "tenant-instances-quota"
case payloads.Image:
return "tenant-images-quota"
case payloads.ExternalIP:
return "tenant-external-ips-quota"
}
return ""
}
func update(tenantDetails map[string]*tenantData, op *updateOp) {
td := getTenantData(tenantDetails, op.tenantID)
for _, q := range op.quotas {
r := quotaNameToResource(q.Name)
if r != "" {
td.quotas[r].limit = q.Value
}
switch q.Name {
case "tenant-vcpu-per-instance-limit":
td.perInstanceVCPUs = q.Value
case "tenant-mem-per-instance-limit":
td.perInstanceMemory = q.Value
case "tenant-volume-size-limit":
td.perVolumeSize = q.Value
}
}
}
func deleteTenant(tenantDetails map[string]*tenantData, op *deleteTenantOp) {
delete(tenantDetails, op.tenantID)
}
func dump(tenantDetails map[string]*tenantData, op *dumpOp) []types.QuotaDetails {
td := getTenantData(tenantDetails, op.tenantID)
qds := []types.QuotaDetails{}
for r, q := range td.quotas {
name := resourceToQuotaName(r)
if name != "" {
qd := types.QuotaDetails{
Name: name,
Value: q.limit,
Usage: q.consumed,
}
qds = append(qds, qd)
}
}
qd := types.QuotaDetails{
Name: "tenant-vcpu-per-instance-limit",
Value: td.perInstanceVCPUs,
}
qds = append(qds, qd)
qd = types.QuotaDetails{
Name: "tenant-mem-per-instance-limit",
Value: td.perInstanceMemory,
}
qds = append(qds, qd)
qd = types.QuotaDetails{
Name: "tenant-volume-size-limit",
Value: td.perVolumeSize,
}
qds = append(qds, qd)
return qds
}
// Init is used to initialise the quota service.
func (qs *Quotas) Init() {
qs.ch = make(chan interface{})
go func() {
tenantDetails := make(map[string]*tenantData)
for {
data, more := <-qs.ch
if !more {
return
}
switch op := data.(type) {
case *consumeOp:
res := consumeQuota(tenantDetails, op)
if !res.Allowed() {
op.ch <- res
close(op.ch)
continue
}
res = checkLimit(tenantDetails, op)
op.ch <- res
close(op.ch)
case *releaseOp:
release(tenantDetails, op)
case *updateOp:
update(tenantDetails, op)
close(op.doneCh)
case *dumpOp:
op.ch <- dump(tenantDetails, op)
close(op.ch)
case *deleteTenantOp:
deleteTenant(tenantDetails, op)
close(op.doneCh)
}
}
}()
}
func copyResources(resources []payloads.RequestedResource) []payloads.RequestedResource {
copy := make([]payloads.RequestedResource, len(resources))
for i := range resources {
copy[i] = resources[i]
}
return copy
}
// Consume will update the quota records to indicate that the tenant is using
// all the resources specified. This method should usually be used on a
// per-instance/volume/image basis as it will also check against the limits.
// The exception to this is for initial import when disregarding the result.
//
// This method returns a Result channel indicating whether the consumption is
// allowed. The result of the Consume() is indicated by
// Result.Allowed(). The caller can choose to ignore this or reclaim the
// resources used (e.g. by terminating an instance.) If the caller chooses to
// reclaim the resource they must call Quotas.Release(). The resources used in
// the original request are available in the result by calling
// Result.Resources(). If Result.Allowed() returns false then then
// Result.Reason() returns an explanation that can be shared with the user.
func (qs *Quotas) Consume(tenantID string, resources ...payloads.RequestedResource) chan Result {
ch := make(chan Result, 1)
data := &consumeOp{tenantID, copyResources(resources), ch}
qs.ch <- data
return ch
}
// Release will update the quota records for a tenant to indicate that it is no
// longer using the supplied resources.
func (qs *Quotas) Release(tenantID string, resources ...payloads.RequestedResource) {
data := &releaseOp{tenantID, copyResources(resources)}
qs.ch <- data
}
// Shutdown will stop the quota service and should be called when it is no
// longer needed.
func (qs *Quotas) Shutdown() {
close(qs.ch)
}
// Update will populate the quota service with quota and limits information.
func (qs *Quotas) Update(tenantID string, quotas []types.QuotaDetails) {
ch := make(chan struct{})
op := &updateOp{tenantID, quotas, ch}
qs.ch <- op
<-ch
}
// DeleteTenant will delete the givent tenant from the quota service
func (qs *Quotas) DeleteTenant(tenantID string) {
ch := make(chan struct{})
op := &deleteTenantOp{tenantID, ch}
qs.ch <- op
<-ch
}
// DumpQuotas provides the list of quotas and limits along with usage
// for a given tenant
func (qs *Quotas) DumpQuotas(tenantID string) []types.QuotaDetails {
ch := make(chan []types.QuotaDetails, 1)
op := &dumpOp{tenantID, ch}
qs.ch <- op
qds := <-ch
return qds
}
// Allowed indicates whether the desired consumption should be permitted.
func (r *result) Allowed() bool {
return r.allowed
}
// Reason gives an explanation for why the request should be denied.
func (r *result) Reason() string {
return r.reason
}
// Resources gives the set of resources that made up the consumption request
// that this Result was associated with.
func (r *result) Resources() []payloads.RequestedResource {
return r.resources
}