forked from kubernetes/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes#5917 from ystryuchkov/feature/runtime-l…
…imits Implement configurable thresholds for threshold based estimator
- Loading branch information
Showing
15 changed files
with
600 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
cluster-autoscaler/estimator/cluster_capacity_threshold.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Copyright 2023 The Kubernetes 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. | ||
*/ | ||
|
||
package estimator | ||
|
||
import ( | ||
"time" | ||
|
||
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider" | ||
) | ||
|
||
type clusterCapacityThreshold struct { | ||
} | ||
|
||
// NodeLimit returns maximum number of new nodes that can be added to the cluster | ||
// based on its capacity. Possible return values are: | ||
// - -1 when cluster has no available capacity | ||
// - 0 when context or cluster-wide node limit is not set. Return value of 0 means that there is no limit. | ||
// - Any positive number representing maximum possible number of new nodes | ||
func (l *clusterCapacityThreshold) NodeLimit(_ cloudprovider.NodeGroup, context EstimationContext) int { | ||
if context == nil || context.ClusterMaxNodeLimit() == 0 { | ||
return 0 | ||
} | ||
if (context.ClusterMaxNodeLimit() < 0) || (context.ClusterMaxNodeLimit() <= context.CurrentNodeCount()) { | ||
return -1 | ||
} | ||
return context.ClusterMaxNodeLimit() - context.CurrentNodeCount() | ||
} | ||
|
||
// DurationLimit always returns 0 for this threshold, meaning that no limit is set. | ||
func (l *clusterCapacityThreshold) DurationLimit(cloudprovider.NodeGroup, EstimationContext) time.Duration { | ||
return 0 | ||
} | ||
|
||
// NewClusterCapacityThreshold returns a Threshold that can be used to limit binpacking | ||
// by available cluster capacity | ||
func NewClusterCapacityThreshold() Threshold { | ||
return &clusterCapacityThreshold{} | ||
} |
68 changes: 68 additions & 0 deletions
68
cluster-autoscaler/estimator/cluster_capacity_threshold_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2023 The Kubernetes 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. | ||
*/ | ||
|
||
package estimator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewClusterCapacityThreshold(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
wantThreshold int | ||
contextMaxNodes int | ||
contextCurrentNodes int | ||
}{ | ||
{ | ||
name: "returns available capacity", | ||
contextMaxNodes: 10, | ||
contextCurrentNodes: 5, | ||
wantThreshold: 5, | ||
}, | ||
{ | ||
name: "no threshold is set if cluster capacity is unlimited", | ||
contextMaxNodes: 0, | ||
contextCurrentNodes: 10, | ||
wantThreshold: 0, | ||
}, | ||
{ | ||
name: "threshold is negative if cluster has no capacity", | ||
contextMaxNodes: 5, | ||
contextCurrentNodes: 10, | ||
wantThreshold: -1, | ||
}, | ||
{ | ||
name: "threshold is negative if cluster node limit is negative", | ||
contextMaxNodes: -5, | ||
contextCurrentNodes: 0, | ||
wantThreshold: -1, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
context := &estimationContext{ | ||
similarNodeGroups: nil, | ||
currentNodeCount: tt.contextCurrentNodes, | ||
clusterMaxNodeLimit: tt.contextMaxNodes, | ||
} | ||
assert.Equal(t, tt.wantThreshold, NewClusterCapacityThreshold().NodeLimit(nil, context)) | ||
assert.True(t, NewClusterCapacityThreshold().DurationLimit(nil, nil) == 0) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2023 The Kubernetes 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. | ||
*/ | ||
|
||
package estimator | ||
|
||
import ( | ||
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider" | ||
) | ||
|
||
// EstimationContext stores static and runtime state of autoscaling, used by Estimator | ||
type EstimationContext interface { | ||
SimilarNodeGroups() []cloudprovider.NodeGroup | ||
ClusterMaxNodeLimit() int | ||
CurrentNodeCount() int | ||
} | ||
|
||
type estimationContext struct { | ||
similarNodeGroups []cloudprovider.NodeGroup | ||
currentNodeCount int | ||
clusterMaxNodeLimit int | ||
} | ||
|
||
// NewEstimationContext creates a patch for estimation context with runtime properties. | ||
// This patch is used to update existing context. | ||
func NewEstimationContext(clusterMaxNodeLimit int, similarNodeGroups []cloudprovider.NodeGroup, currentNodeCount int) EstimationContext { | ||
return &estimationContext{ | ||
similarNodeGroups: similarNodeGroups, | ||
currentNodeCount: currentNodeCount, | ||
clusterMaxNodeLimit: clusterMaxNodeLimit, | ||
} | ||
} | ||
|
||
// SimilarNodeGroups returns array of similar node groups | ||
func (c *estimationContext) SimilarNodeGroups() []cloudprovider.NodeGroup { | ||
return c.similarNodeGroups | ||
} | ||
|
||
// ClusterMaxNodeLimit returns maximum node number allowed for the cluster | ||
func (c *estimationContext) ClusterMaxNodeLimit() int { | ||
return c.clusterMaxNodeLimit | ||
} | ||
|
||
// CurrentNodeCount returns current number of nodes in the cluster | ||
func (c *estimationContext) CurrentNodeCount() int { | ||
return c.currentNodeCount | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.