diff --git a/cluster-autoscaler/clusterstate/clusterstate.go b/cluster-autoscaler/clusterstate/clusterstate.go index 894fe1008aa2..3703c13e0258 100644 --- a/cluster-autoscaler/clusterstate/clusterstate.go +++ b/cluster-autoscaler/clusterstate/clusterstate.go @@ -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 @@ -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, @@ -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. @@ -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 @@ -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 } diff --git a/cluster-autoscaler/clusterstate/clusterstate_test.go b/cluster-autoscaler/clusterstate/clusterstate_test.go index 7b4fb3d6c736..c9290e2dcb0f 100644 --- a/cluster-autoscaler/clusterstate/clusterstate_test.go +++ b/cluster-autoscaler/clusterstate/clusterstate_test.go @@ -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()) @@ -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), }) }