forked from project-codeflare/multi-cluster-app-dispatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquota_rest_manager.go
419 lines (365 loc) · 13.2 KB
/
quota_rest_manager.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
416
417
418
419
//go:build !private
// +build !private
// ------------------------------------------------------ {COPYRIGHT-TOP} ---
// Copyright 2022, 2023 The Multi-Cluster App Dispatcher Authors.
//
// 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.
// ------------------------------------------------------ {COPYRIGHT-END} ---
package quotamanager
import (
"bytes"
"encoding/json"
"fmt"
"github.com/project-codeflare/multi-cluster-app-dispatcher/cmd/kar-controllers/app/options"
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
listersv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/client/listers/controller/v1beta1"
clusterstateapi "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/controller/clusterstate/api"
"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/controller/quota"
"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/util"
"io/ioutil"
"math"
"net/http"
"net/http/httputil"
"reflect"
"strings"
"time"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
)
// QuotaManager implements a QuotaManagerInterface.
type QuotaManager struct {
url string
appwrapperLister listersv1.AppWrapperLister
preemptionEnabled bool
}
type QuotaGroup struct {
GroupContext string `json:"groupcontext"`
GroupId string `json:"groupid"`
}
type Request struct {
Id string `json:"id"`
Groups []QuotaGroup `json:"groups"`
Demand []int `json:"demand"`
Priority int `json:"priority"`
Preemptable bool `json:"preemptable"`
}
type QuotaResponse struct {
Id string `json:"id"`
Groups []QuotaGroup `json:"groups"`
Demand []int `json:"demand"`
Priority int `json:"priority"`
Preemptable bool `json:"preemptable"`
PreemptIds []string `json:"preemptedIds"`
CreateDate string `json:"dateCreated"`
}
type TreeNode struct {
Allocation string `json:"allocation"`
Quota string `json:"quota"`
Name string `json:"name"`
Hard bool `json:"hard"`
Children []TreeNode `json:"children"`
Parent string `json:"parent"`
}
// Making sure that PriorityQueue implements SchedulingQueue.
var _ = quota.QuotaManagerInterface(&QuotaManager{})
func parseId(id string) (string, string) {
ns := ""
n := ""
// Extract the namespace seperator
nspSplit := strings.Split(id, util.NamespacePrefix)
if len(nspSplit) == 2 {
// Extract the appwrapper seperator
awnpSplit := strings.Split(nspSplit[1], util.AppWrapperNamePrefix)
if len(awnpSplit) == 2 {
// What is left if the namespace value in the first slice
if len(awnpSplit[0]) > 0 {
ns = awnpSplit[0]
}
// And the names value in the second slice
if len(awnpSplit[1]) > 0 {
n = awnpSplit[1]
}
}
}
return ns, n
}
func createId(ns string, n string) string {
id := ""
if len(ns) > 0 && len(n) > 0 {
id = fmt.Sprintf("%s%s%s%s", util.NamespacePrefix, ns, util.AppWrapperNamePrefix, n)
}
return id
}
func NewQuotaManager(dispatchedAWDemands map[string]*clusterstateapi.Resource, dispatchedAWs map[string]*arbv1.AppWrapper,
awJobLister listersv1.AppWrapperLister, config *rest.Config,
serverOptions *options.ServerOption) (*QuotaManager, error) {
if !serverOptions.QuotaEnabled {
klog.Infof("[NewQuotaManager] Quota management is not enabled.")
return nil, nil
}
qm := &QuotaManager{
url: serverOptions.QuotaRestURL,
appwrapperLister: awJobLister,
preemptionEnabled: serverOptions.Preemption,
}
return qm, nil
}
// Recrusive call to add names of Tree
func (qm *QuotaManager) addChildrenNodes(parentNode TreeNode, treeIDs []string) []string {
if len(parentNode.Children) > 0 {
for _, childNode := range parentNode.Children {
klog.V(10).Infof("[getQuotaTreeIDs] Quota tree response child node from quota mananger: %s", childNode.Name)
treeIDs = qm.addChildrenNodes(childNode, treeIDs)
}
}
treeIDs = append(treeIDs, parentNode.Name)
return treeIDs
}
func (qm *QuotaManager) getQuotaTreeIDs() []string {
var treeIDs []string
// If a url does not exists then assume fits quota
if len(qm.url) < 1 {
return treeIDs
}
uri := qm.url + "/json"
klog.V(10).Infof("[getQuotaTreeIDs] Sending GET request to uri: %s", uri)
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
klog.Errorf("[getQuotaTreeIDs] Fail to create client to access quota manager: %s, err=%#v.", uri, err)
return treeIDs
}
quotaRestClient := http.Client{
Timeout: time.Second * 2, // Timeout after 2 seconds
}
response, err := quotaRestClient.Do(req)
if err != nil {
klog.Errorf("[getQuotaTreeIDs] Fail to access quota manager: %s, err=%#v.", uri, err)
return treeIDs
} else {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
klog.Errorf("[getQuotaTreeIDs] Failed to read quota tree from the quota manager body: %s, error=%#v",
string(body), err)
return treeIDs
}
var quotaTreesResponse []TreeNode
if err := json.Unmarshal(body, "aTreesResponse); err != nil {
klog.Errorf("[getQuotaTreeIDs] Failed to decode json from quota manager body: %s, error=%#v", string(body), err)
} else {
// Loop over root nodes of trees and add names of each node in tree.
for _, treeroot := range quotaTreesResponse {
klog.V(6).Infof("[getQuotaTreeIDs] Quota tree response root node from quota mananger: %s", treeroot.Name)
treeIDs = qm.addChildrenNodes(treeroot, treeIDs)
}
}
klog.V(10).Infof("[getQuotaTreeIDs] Response from quota mananger status: %s", response.Status)
statusCode := response.StatusCode
klog.V(8).Infof("[getQuotaTreeIDs] Response from quota mananger status code: %v", statusCode)
}
return treeIDs
}
func isValidQuota(quotaGroup QuotaGroup, qmTreeIDs []string) bool {
for _, treeNodeID := range qmTreeIDs {
if treeNodeID == quotaGroup.GroupId {
return true
}
}
return false
}
func (qm *QuotaManager) getQuotaDesignation(aw *arbv1.AppWrapper) []QuotaGroup {
var groups []QuotaGroup
// Get list of quota management tree IDs
qmTreeIDs := qm.getQuotaTreeIDs()
if len(qmTreeIDs) < 1 {
klog.Warningf("[getQuotaDesignation] No valid quota management IDs found for AppWrapper Job: %s/%s",
aw.Namespace, aw.Name)
}
labels := aw.GetLabels()
if labels != nil {
keys := reflect.ValueOf(labels).MapKeys()
for _, key := range keys {
strkey := key.String()
quotaGroup := QuotaGroup{
GroupContext: strkey,
GroupId: labels[strkey],
}
if isValidQuota(quotaGroup, qmTreeIDs) {
groups = append(groups, quotaGroup)
klog.V(8).Infof("[getQuotaDesignation] AppWrapper: %s/%s quota label: %v found.",
aw.Namespace, aw.Name, quotaGroup)
} else {
klog.V(10).Infof("[getQuotaDesignation] AppWrapper: %s/%s label: %v ignored. Not a valid quota ID.",
aw.Namespace, aw.Name, quotaGroup)
}
}
} else {
klog.V(4).Infof("[getQuotaDesignation] AppWrapper: %s/%s does not any context quota labels.",
aw.Namespace, aw.Name)
}
if len(groups) > 0 {
klog.V(6).Infof("[getQuotaDesignation] AppWrapper: %s/%s quota labels: %v.", aw.Namespace,
aw.Name, groups)
} else {
klog.V(4).Infof("[getQuotaDesignation] AppWrapper: %s/%s does not have any quota labels, using default.",
aw.Namespace, aw.Name)
var defaultGroup = QuotaGroup{
GroupContext: "DEFAULTCONTEXT",
GroupId: "DEFAULT",
}
groups = append(groups, defaultGroup)
}
return groups
}
func (qm *QuotaManager) Fits(aw *arbv1.AppWrapper, awResDemands *clusterstateapi.Resource,
proposedPreemptions []*arbv1.AppWrapper) (bool, []*arbv1.AppWrapper, string) {
// Handle uninitialized quota manager
// If a url does not exists then assume fits quota
if len(qm.url) == 0 {
klog.V(4).Infof("[Fits] No quota manager exists, %#v meets quota by default.", awResDemands)
return true, proposedPreemptions, ""
}
awId := createId(aw.Namespace, aw.Name)
if len(awId) <= 0 {
klog.Errorf("[Fits] Request failed due to invalid AppWrapper due to empty namespace: %s or name:%s.", aw.Namespace, aw.Name)
return false, nil, ""
}
groups := qm.getQuotaDesignation(aw)
preemptable := qm.preemptionEnabled
awCPU_Demand := int(math.Trunc(awResDemands.MilliCPU))
awMem_Demand := int(math.Trunc(awResDemands.Memory) / 1000000)
var demand []int
demand = append(demand, awCPU_Demand)
demand = append(demand, awMem_Demand)
priority := int(aw.Spec.Priority)
req := Request{
Id: awId,
Groups: groups,
Demand: demand,
Priority: priority,
Preemptable: preemptable,
}
doesFit := false
uri := qm.url + "/quota/alloc"
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(req)
if err != nil {
klog.Errorf("[Fits] Failed encoding of request: %v, err=%#v.", req, err)
return doesFit, nil, ""
}
var preemptIds []*arbv1.AppWrapper
klog.V(4).Infof("[Fits] Sending request: %v in buffer: %v, buffer size: %v, to uri: %s", req, buf, buf.Len(), uri)
response, err := http.Post(uri, "application/json; charset=utf-8", buf)
if err != nil {
klog.Errorf("[Fits] Fail to add access quotaforestmanager: %s, err=%#v.", uri, err)
preemptIds = nil
return false, preemptIds, err.Error()
}
defer response.Body.Close()
dump, err := httputil.DumpResponse(response, true)
klog.V(10).Infof("[getQuotaTreeIDs] POST Response dump: %q", dump)
if err != nil {
klog.Errorf("[Fits] Fail to add access quotaforestmanager: %s, err=%#v.", uri, err)
preemptIds = nil
} else {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
klog.Errorf("[Fits] Failed to read preemption Ids from the quota manager body: %s, error=%#v", string(body), err)
}
var quotaResponse QuotaResponse
if err := json.Unmarshal(body, "aResponse); err != nil {
klog.Errorf("[Fits] Failed to decode json from quota manager body: %s, error=%#v", string(body), err)
} else {
klog.V(6).Infof("[Fits] Quota response object from quota mananger body: %v", quotaResponse)
}
klog.V(4).Infof("[Fits] Response from quota mananger status: %s", response.Status)
statusCode := response.StatusCode
klog.V(4).Infof("[Fits] Response from quota mananger status code: %v", statusCode)
if statusCode == 200 {
doesFit = true
preemptIds = qm.getAppWrappers(quotaResponse.PreemptIds)
}
}
return doesFit, preemptIds, ""
}
func (qm *QuotaManager) getAppWrappers(preemptIds []string) []*arbv1.AppWrapper {
var aws []*arbv1.AppWrapper
if len(preemptIds) <= 0 {
return nil
}
for _, preemptId := range preemptIds {
awNamespace, awName := parseId(preemptId)
if len(awNamespace) <= 0 || len(awName) <= 0 {
klog.Errorf("[getAppWrappers] Failed to parse AppWrapper id from quota manager, parse string: %s. Preemption of this Id will be ignored.", preemptId)
continue
}
aw, e := qm.appwrapperLister.AppWrappers(awNamespace).Get(awName)
if e != nil {
klog.Errorf("[getAppWrappers] Failed to get AppWrapper from API Cache %s/%s, err=%v. Preemption of this Id will be ignored.",
awNamespace, awName, e)
continue
}
aws = append(aws, aw)
}
// Final validation check
if len(preemptIds) != len(aws) {
klog.Warningf("[getAppWrappers] Preemption list size of %d from quota manager does not match size of generated list of AppWrapper: %d", len(preemptIds), len(aws))
}
return aws
}
func (qm *QuotaManager) Release(aw *arbv1.AppWrapper) bool {
// Handle uninitialized quota manager
if len(qm.url) <= 0 {
return true
}
released := false
awId := createId(aw.Namespace, aw.Name)
if len(awId) <= 0 {
klog.Errorf("[Release] Request failed due to invalid AppWrapper due to empty namespace: %s or name:%s.", aw.Namespace, aw.Name)
return false
}
uri := qm.url + "/quota/release/" + awId
klog.V(4).Infof("[Release] Sending request to release resources for: %s ", uri)
// Create client
client := &http.Client{}
// Create request
req, err := http.NewRequest("DELETE", uri, nil)
if err != nil {
klog.Errorf("[Release] Failed to create http delete request for : %s, err=%#v.", awId, err)
return released
}
// Fetch Request
resp, err := client.Do(req)
if err != nil {
klog.Errorf("[Release] Failed http delete request for: %s, err=%#v.", awId, err)
return released
}
defer resp.Body.Close()
// Read Response Body
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
klog.V(4).Infof("[Release] Failed to aquire response from http delete request id: %s, err=%#v.", awId, err)
} else {
klog.V(4).Infof("[Release] Response from quota mananger body: %s", string(respBody))
}
klog.V(4).Infof("[Release] Response from quota mananger status: %s", resp.Status)
statusCode := resp.StatusCode
klog.V(4).Infof("[Release] Response from quota mananger status code: %v", statusCode)
if statusCode == 204 {
released = true
}
return released
}
func (qm *QuotaManager) GetValidQuotaLabels() []string {
return qm.getQuotaTreeIDs()
}