@@ -20,6 +20,7 @@ import (
20
20
"fmt"
21
21
22
22
apiv1 "k8s.io/api/core/v1"
23
+ "k8s.io/apimachinery/pkg/util/sets"
23
24
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
24
25
huaweicloudsdkasmodel "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/services/as/v1/model"
25
26
"k8s.io/klog/v2"
@@ -98,13 +99,24 @@ func (asg *AutoScalingGroup) DeleteNodes(nodes []*apiv1.Node) error {
98
99
return err
99
100
}
100
101
101
- instanceIds := make ([] string , 0 , len ( instances ) )
102
+ instanceSet := sets . NewString ( )
102
103
for _ , instance := range instances {
103
- for _ , n := range nodes {
104
- if n .Spec .ProviderID == instance .Id {
105
- instanceIds = append (instanceIds , instance .Id )
106
- }
104
+ instanceSet .Insert (instance .Id )
105
+ }
106
+
107
+ instanceIds := make ([]string , 0 , len (instances ))
108
+ for _ , node := range nodes {
109
+ providerID := node .Spec .ProviderID
110
+
111
+ // If one of the nodes not belongs to this auto scaling group, means there is something wrong happened,
112
+ // so, we should reject the whole deleting request.
113
+ if ! instanceSet .Has (providerID ) {
114
+ klog .Errorf ("delete node not belongs this node group is not allowed. group: %s, node: %s" , asg .groupID , providerID )
115
+ return fmt .Errorf ("node does not belong to this node group" )
107
116
}
117
+
118
+ klog .V (1 ).Infof ("going to remove node from scaling group. group: %s, node: %s" , asg .groupID , providerID )
119
+ instanceIds = append (instanceIds , providerID )
108
120
}
109
121
110
122
err = asg .cloudServiceManager .DeleteScalingInstances (asg .groupID , instanceIds )
0 commit comments