1
1
// b private
2
2
// ------------------------------------------------------ {COPYRIGHT-TOP} ---
3
3
// Copyright 2019, 2021, 2022, 2023 The Multi-Cluster App Dispatcher Authors.
4
- //
4
+ //
5
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
6
// you may not use this file except in compliance with the License.
7
7
// You may obtain a copy of the License at
8
- //
8
+ //
9
9
// http://www.apache.org/licenses/LICENSE-2.0
10
10
//
11
11
// Unless required by applicable law or agreed to in writing, software
@@ -20,6 +20,8 @@ package quotaforestmanager
20
20
import (
21
21
"bytes"
22
22
"fmt"
23
+ "strings"
24
+
23
25
"github.com/project-codeflare/multi-cluster-app-dispatcher/cmd/kar-controllers/app/options"
24
26
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
25
27
listersv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/client/listers/controller/v1"
@@ -30,11 +32,11 @@ import (
30
32
qmbackendutils "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/quota-forest/quota-manager/quota/utils"
31
33
"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/util"
32
34
"k8s.io/client-go/rest"
33
- "strings"
34
35
35
- "k8s.io/klog/v2"
36
36
"math"
37
37
"reflect"
38
+
39
+ "k8s.io/klog/v2"
38
40
)
39
41
40
42
const (
@@ -48,7 +50,6 @@ const (
48
50
DefaultQuotaNodeName = "UNKNOWNTREENODENAME"
49
51
50
52
MaxInt = int (^ uint (0 ) >> 1 )
51
-
52
53
)
53
54
54
55
// QuotaManager implements a QuotaManagerInterface.
@@ -62,8 +63,8 @@ type QuotaManager struct {
62
63
}
63
64
64
65
type QuotaGroup struct {
65
- GroupContext string `json:"groupcontext"`
66
- GroupId string `json:"groupid"`
66
+ GroupContext string `json:"groupcontext"`
67
+ GroupId string `json:"groupid"`
67
68
}
68
69
69
70
type Request struct {
@@ -85,12 +86,12 @@ type QuotaResponse struct {
85
86
}
86
87
87
88
type TreeNode struct {
88
- Allocation string `json:"allocation"`
89
- Quota string `json:"quota"`
90
- Name string `json:"name"`
91
- Hard bool `json:"hard"`
92
- Children []TreeNode `json:"children"`
93
- Parent string `json:"parent"`
89
+ Allocation string `json:"allocation"`
90
+ Quota string `json:"quota"`
91
+ Name string `json:"name"`
92
+ Hard bool `json:"hard"`
93
+ Children []TreeNode `json:"children"`
94
+ Parent string `json:"parent"`
94
95
}
95
96
96
97
// Making sure that QuotaManager implements QuotaManager.
@@ -106,7 +107,7 @@ func getDispatchedAppWrapper(dispatchedAWs map[string]*arbv1.AppWrapper, awId st
106
107
}
107
108
108
109
func NewQuotaManager (dispatchedAWDemands map [string ]* clusterstateapi.Resource , dispatchedAWs map [string ]* arbv1.AppWrapper ,
109
- awJobLister listersv1.AppWrapperLister , config * rest.Config , serverOptions * options.ServerOption ) (* QuotaManager , error ) {
110
+ awJobLister listersv1.AppWrapperLister , config * rest.Config , serverOptions * options.ServerOption ) (* QuotaManager , error ) {
110
111
111
112
if serverOptions .QuotaEnabled == false {
112
113
klog .
@@ -139,7 +140,7 @@ func NewQuotaManager(dispatchedAWDemands map[string]*clusterstateapi.Resource, d
139
140
err2 := qm .loadDispatchedAWs (dispatchedAWDemands , dispatchedAWs )
140
141
if err2 != nil {
141
142
klog .Errorf ("[dispatchedAWDemands] Failure during Quota Manager Backend Cache refresh, err=%#v" ,
142
- err2 )
143
+ err2 )
143
144
// Combine errors for function return
144
145
if err != nil {
145
146
err = fmt .Errorf ("%w; Next error %s" , err , err2 .Error ())
@@ -161,29 +162,28 @@ func NewQuotaManager(dispatchedAWDemands map[string]*clusterstateapi.Resource, d
161
162
}
162
163
163
164
func (qm * QuotaManager ) loadDispatchedAWs (dispatchedAWDemands map [string ]* clusterstateapi.Resource ,
164
- dispatchedAWs map [string ]* arbv1.AppWrapper ) error {
165
+ dispatchedAWs map [string ]* arbv1.AppWrapper ) error {
165
166
166
167
// Nothing to do
167
168
if dispatchedAWDemands == nil || len (dispatchedAWDemands ) <= 0 {
168
169
klog .V (4 ).Infof ("[loadDispatchedAWs] No dispatched AppWrappers found to preload." )
169
170
return nil
170
171
}
171
172
172
-
173
173
// Process list of AppWrappers that are already dispatched
174
174
var err error
175
175
err = nil
176
176
177
- for k , v := range dispatchedAWDemands {
177
+ for k , v := range dispatchedAWDemands {
178
178
aw := getDispatchedAppWrapper (dispatchedAWs , k )
179
179
if aw != nil {
180
180
181
- doesFit , preemptionIds , err2 := qm .Fits (aw , v , nil )
181
+ doesFit , preemptionIds , err2 := qm .Fits (aw , v , nil )
182
182
if doesFit == false {
183
183
klog .Errorf ("[loadDispatchedAWs] Loading of AppWrapper %s/%s failed." ,
184
- aw .Namespace , aw .Name )
184
+ aw .Namespace , aw .Name )
185
185
err = fmt .Errorf ("Loading of AppWrapper %s/%s failed, %#v \n " ,
186
- aw .Namespace , aw .Name , err2 )
186
+ aw .Namespace , aw .Name , err2 )
187
187
}
188
188
189
189
if preemptionIds != nil && len (preemptionIds ) > 0 {
@@ -232,7 +232,7 @@ func (qm *QuotaManager) updateForestFromCache() error {
232
232
}
233
233
234
234
// Recrusive call to add names of Tree
235
- func (qm * QuotaManager ) addChildrenNodes (parentNode TreeNode , treeIDs []string ) ( []string ) {
235
+ func (qm * QuotaManager ) addChildrenNodes (parentNode TreeNode , treeIDs []string ) []string {
236
236
if len (parentNode .Children ) > 0 {
237
237
for _ , childNode := range parentNode .Children {
238
238
klog .V (10 ).Infof ("[getQuotaTreeIDs] Quota tree response child node from quota mananger: %s" , childNode .Name )
@@ -265,13 +265,13 @@ func (qm *QuotaManager) getQuotaDesignation(aw *arbv1.AppWrapper) ([]QuotaGroup,
265
265
}
266
266
267
267
labels := aw .GetLabels ()
268
- if ( labels != nil ) {
268
+ if labels != nil {
269
269
keys := reflect .ValueOf (labels ).MapKeys ()
270
- for _ , key := range keys {
270
+ for _ , key := range keys {
271
271
strkey := key .String ()
272
272
quotaGroup := QuotaGroup {
273
273
GroupContext : strkey ,
274
- GroupId : labels [strkey ],
274
+ GroupId : labels [strkey ],
275
275
}
276
276
if isValidQuota (quotaGroup , qmTreeIDs ) {
277
277
// Save the quota designation ain return var
@@ -288,7 +288,7 @@ func (qm *QuotaManager) getQuotaDesignation(aw *arbv1.AppWrapper) ([]QuotaGroup,
288
288
}
289
289
} else {
290
290
klog .V (4 ).Infof ("[getQuotaDesignation] AppWrapper: %s/%s does not any context quota labels." ,
291
- aw .Namespace , aw .Name )
291
+ aw .Namespace , aw .Name )
292
292
}
293
293
294
294
// Figure out which quota tree allocation is missing and produce an error
@@ -342,7 +342,7 @@ func (qm *QuotaManager) getQuotaDesignation(aw *arbv1.AppWrapper) ([]QuotaGroup,
342
342
return groups , treeNameToResourceTypes , nil
343
343
}
344
344
345
- func (qm * QuotaManager ) convertInt64Demand (int64Demand int64 ) (int , error ) {
345
+ func (qm * QuotaManager ) convertInt64Demand (int64Demand int64 ) (int , error ) {
346
346
var err error
347
347
err = nil
348
348
if int64Demand > int64 (MaxInt ) {
@@ -354,7 +354,7 @@ func (qm *QuotaManager) convertInt64Demand (int64Demand int64) (int, error) {
354
354
}
355
355
}
356
356
357
- func (qm * QuotaManager ) convertFloat64Demand (floatDemand float64 ) (int , error ) {
357
+ func (qm * QuotaManager ) convertFloat64Demand (floatDemand float64 ) (int , error ) {
358
358
var err error
359
359
err = nil
360
360
if floatDemand > float64 (MaxInt ) {
@@ -366,7 +366,7 @@ func (qm *QuotaManager) convertFloat64Demand (floatDemand float64) (int, error)
366
366
}
367
367
}
368
368
369
- func (qm * QuotaManager ) getQuotaTreeResourceTypesDemands (awResDemands * clusterstateapi.Resource , treeToResourceTypes []string ) (map [string ]int , error ) {
369
+ func (qm * QuotaManager ) getQuotaTreeResourceTypesDemands (awResDemands * clusterstateapi.Resource , treeToResourceTypes []string ) (map [string ]int , error ) {
370
370
demands := map [string ]int {}
371
371
var err error
372
372
err = nil
@@ -394,7 +394,7 @@ func (qm *QuotaManager) getQuotaTreeResourceTypesDemands(awResDemands *clusterst
394
394
// Memory Demands
395
395
if strings .Contains (strings .ToLower (treeResourceType ), "memory" ) {
396
396
// Handle type conversions
397
- demand , converErr := qm .convertFloat64Demand (awResDemands .Memory / 1000000 )
397
+ demand , converErr := qm .convertFloat64Demand (awResDemands .Memory )
398
398
if converErr != nil {
399
399
if err == nil {
400
400
err = fmt .Errorf ("resource type: %s %s" ,
@@ -443,7 +443,7 @@ func (qm *QuotaManager) getQuotaTreeResourceTypesDemands(awResDemands *clusterst
443
443
}
444
444
445
445
func (qm * QuotaManager ) buildRequest (aw * arbv1.AppWrapper ,
446
- awResDemands * clusterstateapi.Resource ) (* qmbackend.ConsumerInfo , error ) {
446
+ awResDemands * clusterstateapi.Resource ) (* qmbackend.ConsumerInfo , error ) {
447
447
awId := util .CreateId (aw .Namespace , aw .Name )
448
448
if len (awId ) <= 0 {
449
449
err := fmt .Errorf ("[buildRequest] Request failed due to invalid AppWrapper due to empty namespace: %s or name:%s." , aw .Namespace , aw .Name )
@@ -471,7 +471,7 @@ func (qm *QuotaManager) buildRequest(aw *arbv1.AppWrapper,
471
471
472
472
priority := int (aw .Spec .Priority )
473
473
474
- consumerTreeSpec := & qmbackendutils.JConsumerTreeSpec {
474
+ consumerTreeSpec := & qmbackendutils.JConsumerTreeSpec {
475
475
ID : awId ,
476
476
TreeName : quotaTreeDesignation .GroupContext ,
477
477
GroupID : quotaTreeDesignation .GroupId ,
@@ -486,13 +486,13 @@ func (qm *QuotaManager) buildRequest(aw *arbv1.AppWrapper,
486
486
}
487
487
488
488
// Add quota demands per tree to quota allocation request
489
- consumerSpec := & qmbackendutils.JConsumerSpec {
490
- ID : awId ,
491
- Trees : consumerTrees ,
489
+ consumerSpec := & qmbackendutils.JConsumerSpec {
490
+ ID : awId ,
491
+ Trees : consumerTrees ,
492
492
}
493
493
494
494
// JConsumer : JSON consumer
495
- consumer := & qmbackendutils.JConsumer {
495
+ consumer := & qmbackendutils.JConsumer {
496
496
Kind : qmbackendutils .DefaultConsumerKind ,
497
497
Spec : * consumerSpec ,
498
498
}
@@ -510,14 +510,14 @@ func (qm *QuotaManager) refreshQuotaDefiniions() error {
510
510
}
511
511
512
512
func (qm * QuotaManager ) Fits (aw * arbv1.AppWrapper , awResDemands * clusterstateapi.Resource ,
513
- proposedPreemptions []* arbv1.AppWrapper ) (bool , []* arbv1.AppWrapper , string ) {
513
+ proposedPreemptions []* arbv1.AppWrapper ) (bool , []* arbv1.AppWrapper , string ) {
514
514
515
515
doesFit := false
516
516
517
517
// If a Quota Manager Backend instance does not exists then assume quota failed
518
518
if qm .quotaManagerBackend == nil {
519
519
klog .V (4 ).Infof ("[Fits] No quota manager backend exists, %#v fails quota by default." ,
520
- awResDemands )
520
+ awResDemands )
521
521
return doesFit , nil , "No quota manager backend exists"
522
522
}
523
523
@@ -559,11 +559,11 @@ func (qm *QuotaManager) Fits(aw *arbv1.AppWrapper, awResDemands *clusterstateapi
559
559
if allocResponse != nil && len (allocResponse .GetMessage ()) > 0 {
560
560
klog .Errorf ("[Fits] Error allocating consumer: %s/%s, msg=%s, err=%#v." ,
561
561
aw .Namespace , aw .Name , allocResponse .GetMessage (), err )
562
- return doesFit , nil , allocResponse .GetMessage ()
562
+ return doesFit , nil , allocResponse .GetMessage ()
563
563
} else {
564
564
klog .Errorf ("[Fits] Error allocating consumer: %s/%s, err=%#v." ,
565
565
aw .Namespace , aw .Name , err )
566
- return doesFit , nil , err .Error ()
566
+ return doesFit , nil , err .Error ()
567
567
568
568
}
569
569
}
@@ -578,8 +578,7 @@ func (qm *QuotaManager) Fits(aw *arbv1.AppWrapper, awResDemands *clusterstateapi
578
578
return doesFit , preemptIds , allocResponse .GetMessage ()
579
579
}
580
580
581
-
582
- func (qm * QuotaManager ) getAppWrappers (preemptIds []string ) []* arbv1.AppWrapper {
581
+ func (qm * QuotaManager ) getAppWrappers (preemptIds []string ) []* arbv1.AppWrapper {
583
582
var aws []* arbv1.AppWrapper
584
583
if len (preemptIds ) <= 0 {
585
584
return nil
@@ -613,7 +612,7 @@ func (qm *QuotaManager) Release(aw *arbv1.AppWrapper) bool {
613
612
// Handle uninitialized quota manager
614
613
if qm .quotaManagerBackend == nil {
615
614
klog .Errorf ("[Release] No quota manager backend exists, Quota release %s/%s fails quota by default." ,
616
- aw .Name , aw .Namespace )
615
+ aw .Name , aw .Namespace )
617
616
return released
618
617
}
619
618
0 commit comments