|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package binpacking |
| 18 | + |
| 19 | +import ( |
| 20 | + "k8s.io/autoscaler/cluster-autoscaler/cloudprovider" |
| 21 | + "k8s.io/autoscaler/cluster-autoscaler/context" |
| 22 | + "k8s.io/autoscaler/cluster-autoscaler/expander" |
| 23 | +) |
| 24 | + |
| 25 | +// CombinedLimiter combines the outcome of multiple limiters. It will limit |
| 26 | +// binpacking when at least one limiter meets the stop condition. |
| 27 | +type CombinedLimiter struct { |
| 28 | + limiters []BinpackingLimiter |
| 29 | +} |
| 30 | + |
| 31 | +// NewCombinedLimiter returns an instance of a new CombinedLimiter. |
| 32 | +func NewCombinedLimiter(limiters []BinpackingLimiter) *CombinedLimiter { |
| 33 | + return &CombinedLimiter{ |
| 34 | + limiters: limiters, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// InitBinpacking initialises all the underline limiters. |
| 39 | +func (l *CombinedLimiter) InitBinpacking(context *context.AutoscalingContext, nodeGroups []cloudprovider.NodeGroup) { |
| 40 | + for _, limiter := range l.limiters { |
| 41 | + limiter.InitBinpacking(context, nodeGroups) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// MarkProcessed marks the nodegroup as processed in all underline limiters. |
| 46 | +func (l *CombinedLimiter) MarkProcessed(context *context.AutoscalingContext, nodegroupId string) { |
| 47 | + for _, limiter := range l.limiters { |
| 48 | + limiter.MarkProcessed(context, nodegroupId) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// StopBinpacking returns true if at least one of the underline limiter met the stop condition. |
| 53 | +func (l *CombinedLimiter) StopBinpacking(context *context.AutoscalingContext, evaluatedOptions []expander.Option) bool { |
| 54 | + stopCondition := false |
| 55 | + for _, limiter := range l.limiters { |
| 56 | + stopCondition = limiter.StopBinpacking(context, evaluatedOptions) || stopCondition |
| 57 | + } |
| 58 | + return stopCondition |
| 59 | +} |
| 60 | + |
| 61 | +// FinalizeBinpacking will call FinalizeBinpacking for all the underline limiters. |
| 62 | +func (l *CombinedLimiter) FinalizeBinpacking(context *context.AutoscalingContext, finalOptions []expander.Option) { |
| 63 | + for _, limiter := range l.limiters { |
| 64 | + limiter.FinalizeBinpacking(context, finalOptions) |
| 65 | + } |
| 66 | +} |
0 commit comments