11// b private
22// ------------------------------------------------------ {COPYRIGHT-TOP} ---
33// Copyright 2019, 2021, 2022, 2023 The Multi-Cluster App Dispatcher Authors.
4- //
4+ //
55// Licensed under the Apache License, Version 2.0 (the "License");
66// you may not use this file except in compliance with the License.
77// You may obtain a copy of the License at
8- //
8+ //
99// http://www.apache.org/licenses/LICENSE-2.0
1010//
1111// Unless required by applicable law or agreed to in writing, software
@@ -20,6 +20,8 @@ package quotaforestmanager
2020import (
2121 "bytes"
2222 "fmt"
23+ "strings"
24+
2325 "github.com/project-codeflare/multi-cluster-app-dispatcher/cmd/kar-controllers/app/options"
2426 arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
2527 listersv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/client/listers/controller/v1"
@@ -30,11 +32,11 @@ import (
3032 qmbackendutils "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/quota-forest/quota-manager/quota/utils"
3133 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/util"
3234 "k8s.io/client-go/rest"
33- "strings"
3435
35- "k8s.io/klog/v2"
3636 "math"
3737 "reflect"
38+
39+ "k8s.io/klog/v2"
3840)
3941
4042const (
@@ -48,7 +50,6 @@ const (
4850 DefaultQuotaNodeName = "UNKNOWNTREENODENAME"
4951
5052 MaxInt = int (^ uint (0 ) >> 1 )
51-
5253)
5354
5455// QuotaManager implements a QuotaManagerInterface.
@@ -62,8 +63,8 @@ type QuotaManager struct {
6263}
6364
6465type QuotaGroup struct {
65- GroupContext string `json:"groupcontext"`
66- GroupId string `json:"groupid"`
66+ GroupContext string `json:"groupcontext"`
67+ GroupId string `json:"groupid"`
6768}
6869
6970type Request struct {
@@ -85,12 +86,12 @@ type QuotaResponse struct {
8586}
8687
8788type 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"`
9495}
9596
9697// Making sure that QuotaManager implements QuotaManager.
@@ -106,7 +107,7 @@ func getDispatchedAppWrapper(dispatchedAWs map[string]*arbv1.AppWrapper, awId st
106107}
107108
108109func 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 ) {
110111
111112 if serverOptions .QuotaEnabled == false {
112113 klog .
@@ -139,7 +140,7 @@ func NewQuotaManager(dispatchedAWDemands map[string]*clusterstateapi.Resource, d
139140 err2 := qm .loadDispatchedAWs (dispatchedAWDemands , dispatchedAWs )
140141 if err2 != nil {
141142 klog .Errorf ("[dispatchedAWDemands] Failure during Quota Manager Backend Cache refresh, err=%#v" ,
142- err2 )
143+ err2 )
143144 // Combine errors for function return
144145 if err != nil {
145146 err = fmt .Errorf ("%w; Next error %s" , err , err2 .Error ())
@@ -161,29 +162,28 @@ func NewQuotaManager(dispatchedAWDemands map[string]*clusterstateapi.Resource, d
161162}
162163
163164func (qm * QuotaManager ) loadDispatchedAWs (dispatchedAWDemands map [string ]* clusterstateapi.Resource ,
164- dispatchedAWs map [string ]* arbv1.AppWrapper ) error {
165+ dispatchedAWs map [string ]* arbv1.AppWrapper ) error {
165166
166167 // Nothing to do
167168 if dispatchedAWDemands == nil || len (dispatchedAWDemands ) <= 0 {
168169 klog .V (4 ).Infof ("[loadDispatchedAWs] No dispatched AppWrappers found to preload." )
169170 return nil
170171 }
171172
172-
173173 // Process list of AppWrappers that are already dispatched
174174 var err error
175175 err = nil
176176
177- for k , v := range dispatchedAWDemands {
177+ for k , v := range dispatchedAWDemands {
178178 aw := getDispatchedAppWrapper (dispatchedAWs , k )
179179 if aw != nil {
180180
181- doesFit , preemptionIds , err2 := qm .Fits (aw , v , nil )
181+ doesFit , preemptionIds , err2 := qm .Fits (aw , v , nil )
182182 if doesFit == false {
183183 klog .Errorf ("[loadDispatchedAWs] Loading of AppWrapper %s/%s failed." ,
184- aw .Namespace , aw .Name )
184+ aw .Namespace , aw .Name )
185185 err = fmt .Errorf ("Loading of AppWrapper %s/%s failed, %#v \n " ,
186- aw .Namespace , aw .Name , err2 )
186+ aw .Namespace , aw .Name , err2 )
187187 }
188188
189189 if preemptionIds != nil && len (preemptionIds ) > 0 {
@@ -232,7 +232,7 @@ func (qm *QuotaManager) updateForestFromCache() error {
232232}
233233
234234// 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 {
236236 if len (parentNode .Children ) > 0 {
237237 for _ , childNode := range parentNode .Children {
238238 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,
265265 }
266266
267267 labels := aw .GetLabels ()
268- if ( labels != nil ) {
268+ if labels != nil {
269269 keys := reflect .ValueOf (labels ).MapKeys ()
270- for _ , key := range keys {
270+ for _ , key := range keys {
271271 strkey := key .String ()
272272 quotaGroup := QuotaGroup {
273273 GroupContext : strkey ,
274- GroupId : labels [strkey ],
274+ GroupId : labels [strkey ],
275275 }
276276 if isValidQuota (quotaGroup , qmTreeIDs ) {
277277 // Save the quota designation ain return var
@@ -288,7 +288,7 @@ func (qm *QuotaManager) getQuotaDesignation(aw *arbv1.AppWrapper) ([]QuotaGroup,
288288 }
289289 } else {
290290 klog .V (4 ).Infof ("[getQuotaDesignation] AppWrapper: %s/%s does not any context quota labels." ,
291- aw .Namespace , aw .Name )
291+ aw .Namespace , aw .Name )
292292 }
293293
294294 // Figure out which quota tree allocation is missing and produce an error
@@ -342,7 +342,7 @@ func (qm *QuotaManager) getQuotaDesignation(aw *arbv1.AppWrapper) ([]QuotaGroup,
342342 return groups , treeNameToResourceTypes , nil
343343}
344344
345- func (qm * QuotaManager ) convertInt64Demand (int64Demand int64 ) (int , error ) {
345+ func (qm * QuotaManager ) convertInt64Demand (int64Demand int64 ) (int , error ) {
346346 var err error
347347 err = nil
348348 if int64Demand > int64 (MaxInt ) {
@@ -354,7 +354,7 @@ func (qm *QuotaManager) convertInt64Demand (int64Demand int64) (int, error) {
354354 }
355355}
356356
357- func (qm * QuotaManager ) convertFloat64Demand (floatDemand float64 ) (int , error ) {
357+ func (qm * QuotaManager ) convertFloat64Demand (floatDemand float64 ) (int , error ) {
358358 var err error
359359 err = nil
360360 if floatDemand > float64 (MaxInt ) {
@@ -366,7 +366,7 @@ func (qm *QuotaManager) convertFloat64Demand (floatDemand float64) (int, error)
366366 }
367367}
368368
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 ) {
370370 demands := map [string ]int {}
371371 var err error
372372 err = nil
@@ -394,7 +394,7 @@ func (qm *QuotaManager) getQuotaTreeResourceTypesDemands(awResDemands *clusterst
394394 // Memory Demands
395395 if strings .Contains (strings .ToLower (treeResourceType ), "memory" ) {
396396 // Handle type conversions
397- demand , converErr := qm .convertFloat64Demand (awResDemands .Memory / 1000000 )
397+ demand , converErr := qm .convertFloat64Demand (awResDemands .Memory )
398398 if converErr != nil {
399399 if err == nil {
400400 err = fmt .Errorf ("resource type: %s %s" ,
@@ -443,7 +443,7 @@ func (qm *QuotaManager) getQuotaTreeResourceTypesDemands(awResDemands *clusterst
443443}
444444
445445func (qm * QuotaManager ) buildRequest (aw * arbv1.AppWrapper ,
446- awResDemands * clusterstateapi.Resource ) (* qmbackend.ConsumerInfo , error ) {
446+ awResDemands * clusterstateapi.Resource ) (* qmbackend.ConsumerInfo , error ) {
447447 awId := util .CreateId (aw .Namespace , aw .Name )
448448 if len (awId ) <= 0 {
449449 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,
471471
472472 priority := int (aw .Spec .Priority )
473473
474- consumerTreeSpec := & qmbackendutils.JConsumerTreeSpec {
474+ consumerTreeSpec := & qmbackendutils.JConsumerTreeSpec {
475475 ID : awId ,
476476 TreeName : quotaTreeDesignation .GroupContext ,
477477 GroupID : quotaTreeDesignation .GroupId ,
@@ -486,13 +486,13 @@ func (qm *QuotaManager) buildRequest(aw *arbv1.AppWrapper,
486486 }
487487
488488 // 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 ,
492492 }
493493
494494 // JConsumer : JSON consumer
495- consumer := & qmbackendutils.JConsumer {
495+ consumer := & qmbackendutils.JConsumer {
496496 Kind : qmbackendutils .DefaultConsumerKind ,
497497 Spec : * consumerSpec ,
498498 }
@@ -510,14 +510,14 @@ func (qm *QuotaManager) refreshQuotaDefiniions() error {
510510}
511511
512512func (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 ) {
514514
515515 doesFit := false
516516
517517 // If a Quota Manager Backend instance does not exists then assume quota failed
518518 if qm .quotaManagerBackend == nil {
519519 klog .V (4 ).Infof ("[Fits] No quota manager backend exists, %#v fails quota by default." ,
520- awResDemands )
520+ awResDemands )
521521 return doesFit , nil , "No quota manager backend exists"
522522 }
523523
@@ -559,11 +559,11 @@ func (qm *QuotaManager) Fits(aw *arbv1.AppWrapper, awResDemands *clusterstateapi
559559 if allocResponse != nil && len (allocResponse .GetMessage ()) > 0 {
560560 klog .Errorf ("[Fits] Error allocating consumer: %s/%s, msg=%s, err=%#v." ,
561561 aw .Namespace , aw .Name , allocResponse .GetMessage (), err )
562- return doesFit , nil , allocResponse .GetMessage ()
562+ return doesFit , nil , allocResponse .GetMessage ()
563563 } else {
564564 klog .Errorf ("[Fits] Error allocating consumer: %s/%s, err=%#v." ,
565565 aw .Namespace , aw .Name , err )
566- return doesFit , nil , err .Error ()
566+ return doesFit , nil , err .Error ()
567567
568568 }
569569 }
@@ -578,8 +578,7 @@ func (qm *QuotaManager) Fits(aw *arbv1.AppWrapper, awResDemands *clusterstateapi
578578 return doesFit , preemptIds , allocResponse .GetMessage ()
579579}
580580
581-
582- func (qm * QuotaManager ) getAppWrappers (preemptIds []string ) []* arbv1.AppWrapper {
581+ func (qm * QuotaManager ) getAppWrappers (preemptIds []string ) []* arbv1.AppWrapper {
583582 var aws []* arbv1.AppWrapper
584583 if len (preemptIds ) <= 0 {
585584 return nil
@@ -613,7 +612,7 @@ func (qm *QuotaManager) Release(aw *arbv1.AppWrapper) bool {
613612 // Handle uninitialized quota manager
614613 if qm .quotaManagerBackend == nil {
615614 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 )
617616 return released
618617 }
619618
0 commit comments