Skip to content

Commit

Permalink
Store scale downs by node group id
Browse files Browse the repository at this point in the history
  • Loading branch information
bskiba committed Jul 24, 2024
1 parent dee3865 commit bd40d70
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
24 changes: 13 additions & 11 deletions cluster-autoscaler/clusterstate/clusterstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ type ClusterStateRegistry struct {
sync.Mutex
config ClusterStateRegistryConfig
scaleUpRequests map[string]*ScaleUpRequest // nodeGroupName -> ScaleUpRequest
scaleDownRequests []*ScaleDownRequest
scaleDownRequests map[string][]*ScaleDownRequest
nodes []*apiv1.Node
nodeInfosForGroups map[string]*schedulerframework.NodeInfo
cloudProvider cloudprovider.CloudProvider
Expand Down Expand Up @@ -158,7 +158,7 @@ type NodeGroupScalingSafety struct {
func NewClusterStateRegistry(cloudProvider cloudprovider.CloudProvider, config ClusterStateRegistryConfig, logRecorder *utils.LogEventRecorder, backoff backoff.Backoff, nodeGroupConfigProcessor nodegroupconfig.NodeGroupConfigProcessor) *ClusterStateRegistry {
return &ClusterStateRegistry{
scaleUpRequests: make(map[string]*ScaleUpRequest),
scaleDownRequests: make([]*ScaleDownRequest, 0),
scaleDownRequests: make(map[string][]*ScaleDownRequest),
nodes: make([]*apiv1.Node, 0),
cloudProvider: cloudProvider,
config: config,
Expand Down Expand Up @@ -251,7 +251,7 @@ func (csr *ClusterStateRegistry) RegisterScaleDown(nodeGroup cloudprovider.NodeG
}
csr.Lock()
defer csr.Unlock()
csr.scaleDownRequests = append(csr.scaleDownRequests, request)
csr.scaleDownRequests[nodeGroup.Id()] = append(csr.scaleDownRequests[nodeGroup.Id()], request)
}

// To be executed under a lock.
Expand Down Expand Up @@ -291,10 +291,12 @@ func (csr *ClusterStateRegistry) updateScaleRequests(currentTime time.Time) {
}
}

newScaleDownRequests := make([]*ScaleDownRequest, 0)
for _, scaleDownRequest := range csr.scaleDownRequests {
if scaleDownRequest.ExpectedDeleteTime.After(currentTime) {
newScaleDownRequests = append(newScaleDownRequests, scaleDownRequest)
newScaleDownRequests := make(map[string][]*ScaleDownRequest)
for nodeGroupId, scaleDownRequests := range csr.scaleDownRequests {
for _, scaleDownRequest := range scaleDownRequests {
if scaleDownRequest.ExpectedDeleteTime.After(currentTime) {
newScaleDownRequests[nodeGroupId] = append(newScaleDownRequests[nodeGroupId], scaleDownRequest)
}
}
}
csr.scaleDownRequests = newScaleDownRequests
Expand Down Expand Up @@ -566,10 +568,10 @@ func (csr *ClusterStateRegistry) updateAcceptableRanges(targetSize map[string]in
acceptableRange.MinNodes -= scaleUpRequest.Increase
result[nodeGroupName] = acceptableRange
}
for _, scaleDownRequest := range csr.scaleDownRequests {
acceptableRange := result[scaleDownRequest.NodeGroup.Id()]
acceptableRange.MaxNodes++
result[scaleDownRequest.NodeGroup.Id()] = acceptableRange
for nodeGroupName, scaleDownRequests := range csr.scaleDownRequests {
acceptableRange := result[nodeGroupName]
acceptableRange.MaxNodes += len(scaleDownRequests)
result[nodeGroupName] = acceptableRange
}
csr.acceptableRanges = result
}
Expand Down
6 changes: 3 additions & 3 deletions cluster-autoscaler/clusterstate/clusterstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func TestRegisterScaleDown(t *testing.T) {
}, fakeLogRecorder, newBackoff(), nodegroupconfig.NewDefaultNodeGroupConfigProcessor(config.NodeGroupAutoscalingOptions{MaxNodeProvisionTime: 15 * time.Minute}))
now := time.Now()
clusterstate.RegisterScaleDown(provider.GetNodeGroup("ng1"), "ng1-1", now.Add(time.Minute), now)
assert.Equal(t, 1, len(clusterstate.scaleDownRequests))
assert.Equal(t, 1, len(clusterstate.scaleDownRequests["ng1"]))
clusterstate.updateScaleRequests(now.Add(5 * time.Minute))
assert.Equal(t, 0, len(clusterstate.scaleDownRequests))
assert.Empty(t, clusterstate.GetScaleUpFailures())
Expand Down Expand Up @@ -1143,9 +1143,9 @@ func TestUpdateAcceptableRanges(t *testing.T) {
for nodeGroupName, targetSize := range tc.targetSizes {
provider.AddNodeGroup(nodeGroupName, 0, 1000, targetSize)
}
var scaleDownRequests []*ScaleDownRequest
scaleDownRequests := make(map[string][]*ScaleDownRequest)
for _, nodeGroupName := range tc.scaledDownGroups {
scaleDownRequests = append(scaleDownRequests, &ScaleDownRequest{
scaleDownRequests[nodeGroupName] = append(scaleDownRequests[nodeGroupName], &ScaleDownRequest{
NodeGroup: provider.GetNodeGroup(nodeGroupName),
})
}
Expand Down

0 comments on commit bd40d70

Please sign in to comment.