forked from snapcore/snapd
/
api_quotas.go
216 lines (188 loc) · 5.84 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
// -*- 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/gadget/quantity"
"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"`
Constraints client.QuotaValues `json:"constraints,omitempty"`
}
var (
servicestateCreateQuota = servicestate.CreateQuota
servicestateUpdateQuota = servicestate.UpdateQuota
servicestateRemoveQuota = servicestate.RemoveQuota
)
var getQuotaMemUsage = func(grp *quota.Group) (quantity.Size, error) {
return grp.CurrentMemoryUsage()
}
// 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]
memoryUsage, err := getQuotaMemUsage(group)
if err != nil {
return InternalError(err.Error())
}
results[i] = client.QuotaGroupResult{
GroupName: group.Name,
Parent: group.ParentGroup,
Subgroups: group.SubGroups,
Snaps: group.Snaps,
Constraints: &client.QuotaValues{
Memory: group.MemoryLimit,
},
Current: &client.QuotaValues{
Memory: memoryUsage,
},
}
}
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())
}
memoryUsage, err := getQuotaMemUsage(group)
if err != nil {
return InternalError(err.Error())
}
res := client.QuotaGroupResult{
GroupName: group.Name,
Parent: group.ParentGroup,
Snaps: group.Snaps,
Subgroups: group.SubGroups,
Constraints: &client.QuotaValues{
Memory: group.MemoryLimit,
},
Current: &client.QuotaValues{
Memory: memoryUsage,
},
}
return SyncResponse(res)
}
// 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":
// 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, data.Parent, data.Snaps, data.Constraints.Memory)
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.QuotaGroupUpdate{
AddSnaps: data.Snaps,
NewMemoryLimit: data.Constraints.Memory,
}
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())
}