@@ -77,6 +77,16 @@ func (w Weights) Sub(r Weights) {
7777 }
7878}
7979
80+ // Subtract use from quota
81+ func (w Weights ) QuotaSub (r Weights ) {
82+ for k , v := range r {
83+ if w [k ] == nil {
84+ continue // ignore undefined quota
85+ }
86+ w [k ].Sub (w [k ], v )
87+ }
88+ }
89+
8090// Add coefficient * weights to receiver
8191func (w Weights ) AddProd (coefficient int32 , r Weights ) {
8292 for k , v := range r {
@@ -123,6 +133,28 @@ func (w Weights) Fits(r Weights) (bool, []v1.ResourceName) {
123133 }
124134}
125135
136+ // Compare receiver to argument
137+ // True if receiver is less than or equal to argument in every dimension where argument is defined
138+ func (w Weights ) QuotaFits (r Weights ) (bool , []v1.ResourceName ) {
139+ insufficient := []v1.ResourceName {}
140+ zero := & inf.Dec {} // shared zero, never mutated
141+ for k , v := range w { // range over receiver not argument
142+ // ignore 0 requests or no quota
143+ if v .Cmp (zero ) <= 0 || r [k ] == nil {
144+ continue
145+ }
146+ // v > 0 so r[k] must be no less than v
147+ if v .Cmp (r [k ]) == 1 {
148+ insufficient = append (insufficient , k )
149+ }
150+ }
151+ if len (insufficient ) == 0 {
152+ return true , nil
153+ } else {
154+ return false , insufficient
155+ }
156+ }
157+
126158// Converts Weights to a ResourceList
127159func (w Weights ) AsResources () v1.ResourceList {
128160 resources := v1.ResourceList {}
@@ -161,9 +193,9 @@ func (w *WeightsPair) Add(r *WeightsPair) {
161193}
162194
163195// Subtract pair of weights from receiver
164- func (w * WeightsPair ) Sub (r * WeightsPair ) {
165- w .requests .Sub (r .requests )
166- w .limits .Sub (r .limits )
196+ func (w * WeightsPair ) QuotaSub (r * WeightsPair ) {
197+ w .requests .QuotaSub (r .requests )
198+ w .limits .QuotaSub (r .limits )
167199}
168200
169201// Max of two pairs of weights
@@ -184,8 +216,8 @@ func (w *WeightsPair) Clone() *WeightsPair {
184216// If False, return list of insufficient resource names
185217func (w * WeightsPair ) Fits (r * WeightsPair ) (bool , []v1.ResourceName ) {
186218 insufficient := []v1.ResourceName {}
187- requestsFits , requestsInsufficient := w .requests .Fits (r .requests )
188- limitsFits , limitsInsufficient := w .limits .Fits (r .limits )
219+ requestsFits , requestsInsufficient := w .requests .QuotaFits (r .requests )
220+ limitsFits , limitsInsufficient := w .limits .QuotaFits (r .limits )
189221 if requestsFits && limitsFits {
190222 return true , insufficient
191223 }
0 commit comments