-
Notifications
You must be signed in to change notification settings - Fork 562
/
api_quotas.go
296 lines (261 loc) · 8.05 KB
/
api_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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* 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 daemon
import (
"net/http"
"sort"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/jsonutil"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/servicestate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap/naming"
"github.com/snapcore/snapd/snap/quota"
)
var (
quotaGroupsCmd = &Command{
Path: "/v2/quotas",
GET: getQuotaGroups,
POST: postQuotaGroup,
WriteAccess: rootAccess{},
ReadAccess: openAccess{},
}
quotaGroupInfoCmd = &Command{
Path: "/v2/quotas/{group}",
GET: getQuotaGroupInfo,
ReadAccess: openAccess{},
}
)
type postQuotaGroupData struct {
// Action can be "ensure" or "remove"
Action string `json:"action"`
GroupName string `json:"group-name"`
Parent string `json:"parent,omitempty"`
Snaps []string `json:"snaps,omitempty"`
Services []string `json:"services,omitempty"`
Constraints client.QuotaValues `json:"constraints,omitempty"`
}
var (
servicestateCreateQuota = servicestate.CreateQuota
servicestateUpdateQuota = servicestate.UpdateQuota
servicestateRemoveQuota = servicestate.RemoveQuota
)
var getQuotaUsage = func(grp *quota.Group) (*client.QuotaValues, error) {
var currentUsage client.QuotaValues
if grp.MemoryLimit != 0 {
mem, err := grp.CurrentMemoryUsage()
if err != nil {
return nil, err
}
currentUsage.Memory = mem
}
if grp.ThreadLimit != 0 {
threads, err := grp.CurrentTaskUsage()
if err != nil {
return nil, err
}
currentUsage.Threads = threads
}
return ¤tUsage, nil
}
func createQuotaValues(grp *quota.Group) *client.QuotaValues {
var constraints client.QuotaValues
constraints.Memory = grp.MemoryLimit
constraints.Threads = grp.ThreadLimit
if grp.CPULimit != nil {
constraints.CPU = &client.QuotaCPUValues{
Count: grp.CPULimit.Count,
Percentage: grp.CPULimit.Percentage,
}
constraints.CPUSet = &client.QuotaCPUSetValues{
CPUs: grp.CPULimit.CPUSet,
}
}
if grp.JournalLimit != nil {
constraints.Journal = &client.QuotaJournalValues{
Size: grp.JournalLimit.Size,
}
if grp.JournalLimit.RateEnabled {
constraints.Journal.QuotaJournalRate = &client.QuotaJournalRate{
RateCount: grp.JournalLimit.RateCount,
RatePeriod: grp.JournalLimit.RatePeriod,
}
}
}
return &constraints
}
// getQuotaGroups returns all quota groups sorted by name.
func getQuotaGroups(c *Command, r *http.Request, _ *auth.UserState) Response {
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
quotas, err := servicestate.AllQuotas(st)
if err != nil {
return InternalError(err.Error())
}
i := 0
names := make([]string, len(quotas))
for name := range quotas {
names[i] = name
i++
}
sort.Strings(names)
results := make([]client.QuotaGroupResult, len(quotas))
for i, name := range names {
group := quotas[name]
currentUsage, err := getQuotaUsage(group)
if err != nil {
return InternalError(err.Error())
}
results[i] = client.QuotaGroupResult{
GroupName: group.Name,
Parent: group.ParentGroup,
Subgroups: group.SubGroups,
Snaps: group.Snaps,
Services: group.Services,
Constraints: createQuotaValues(group),
Current: currentUsage,
}
}
return SyncResponse(results)
}
// getQuotaGroupInfo returns details of a single quota Group.
func getQuotaGroupInfo(c *Command, r *http.Request, _ *auth.UserState) Response {
vars := muxVars(r)
groupName := vars["group"]
if err := naming.ValidateQuotaGroup(groupName); err != nil {
return BadRequest(err.Error())
}
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
group, err := servicestate.GetQuota(st, groupName)
if err == servicestate.ErrQuotaNotFound {
return NotFound("cannot find quota group %q", groupName)
}
if err != nil {
return InternalError(err.Error())
}
currentUsage, err := getQuotaUsage(group)
if err != nil {
return InternalError(err.Error())
}
res := client.QuotaGroupResult{
GroupName: group.Name,
Parent: group.ParentGroup,
Snaps: group.Snaps,
Services: group.Services,
Subgroups: group.SubGroups,
Constraints: createQuotaValues(group),
Current: currentUsage,
}
return SyncResponse(res)
}
func quotaValuesToResources(values client.QuotaValues) quota.Resources {
resourcesBuilder := quota.NewResourcesBuilder()
if values.Memory != 0 {
resourcesBuilder.WithMemoryLimit(values.Memory)
}
if values.CPU != nil {
if values.CPU.Count != 0 {
resourcesBuilder.WithCPUCount(values.CPU.Count)
}
if values.CPU.Percentage != 0 {
resourcesBuilder.WithCPUPercentage(values.CPU.Percentage)
}
}
if values.CPUSet != nil && len(values.CPUSet.CPUs) != 0 {
resourcesBuilder.WithCPUSet(values.CPUSet.CPUs)
}
if values.Threads != 0 {
resourcesBuilder.WithThreadLimit(values.Threads)
}
if values.Journal != nil {
resourcesBuilder.WithJournalNamespace()
if values.Journal.Size != 0 {
resourcesBuilder.WithJournalSize(values.Journal.Size)
}
if values.Journal.QuotaJournalRate != nil {
resourcesBuilder.WithJournalRate(values.Journal.RateCount, values.Journal.RatePeriod)
}
}
return resourcesBuilder.Build()
}
// postQuotaGroup creates quota resource group or updates an existing group.
func postQuotaGroup(c *Command, r *http.Request, _ *auth.UserState) Response {
var data postQuotaGroupData
if err := jsonutil.DecodeWithNumber(r.Body, &data); err != nil {
return BadRequest("cannot decode quota action from request body: %v", err)
}
if err := naming.ValidateQuotaGroup(data.GroupName); err != nil {
return BadRequest(err.Error())
}
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
chgSummary := ""
var ts *state.TaskSet
switch data.Action {
case "ensure":
// pack constraints into a resource limits struct
resourceLimits := quotaValuesToResources(data.Constraints)
// check if the quota group exists first, if it does then we need to
// update it instead of create it
_, err := servicestate.GetQuota(st, data.GroupName)
if err != nil && err != servicestate.ErrQuotaNotFound {
return InternalError(err.Error())
}
if err == servicestate.ErrQuotaNotFound {
// then we need to create the quota
ts, err = servicestateCreateQuota(st, data.GroupName, servicestate.CreateQuotaOptions{
ParentName: data.Parent,
Snaps: data.Snaps,
Services: data.Services,
ResourceLimits: resourceLimits,
})
if err != nil {
return errToResponse(err, nil, BadRequest, "cannot create quota group: %v")
}
chgSummary = "Create quota group"
} else if err == nil {
// the quota group already exists, update it
updateOpts := servicestate.UpdateQuotaOptions{
AddSnaps: data.Snaps,
AddServices: data.Services,
NewResourceLimits: resourceLimits,
}
ts, err = servicestateUpdateQuota(st, data.GroupName, updateOpts)
if err != nil {
return errToResponse(err, nil, BadRequest, "cannot update quota group: %v")
}
chgSummary = "Update quota group"
}
case "remove":
var err error
ts, err = servicestateRemoveQuota(st, data.GroupName)
if err != nil {
return errToResponse(err, nil, BadRequest, "cannot remove quota group: %v")
}
chgSummary = "Remove quota group"
default:
return BadRequest("unknown quota action %q", data.Action)
}
chg := newChange(st, "quota-control", chgSummary, []*state.TaskSet{ts}, data.Snaps)
ensureStateSoon(st)
return AsyncResponse(nil, chg.ID())
}