@@ -46,7 +46,6 @@ type NodeGroup struct {
46
46
nodePoolSizeTmp int
47
47
getNodePoolSizeTime time.Time
48
48
49
- deleteMutex * sync.Mutex
50
49
clusterUpdateMutex * sync.Mutex
51
50
}
52
51
@@ -134,20 +133,19 @@ func (ng *NodeGroup) IncreaseSize(delta int) error {
134
133
// DeleteNodes deletes nodes from this node group. This function
135
134
// should wait until node group size is updated.
136
135
func (ng * NodeGroup ) DeleteNodes (nodes []* apiv1.Node ) error {
137
- // use ng.deleteMutex to update ng.nodePoolSizeTmp, ng.getNodePoolSizeTime and ng.nodesToDelete
138
- ng .deleteMutex .Lock ()
136
+ // start the process of deleting nodes
137
+ ng .clusterUpdateMutex .Lock ()
138
+ defer ng .clusterUpdateMutex .Unlock ()
139
+
140
+ // check whether deleting the nodes will cause the size of the node pool below minimum size
141
+ // and update ng.nodesToDelete (as well as ng.nodePoolSizeTmp and ng.getNodePoolSizeTime if necessary)
139
142
currentSize , err := checkAndUpdate (ng , nodes )
140
- ng .deleteMutex .Unlock ()
141
143
if err != nil {
142
144
return err
143
145
}
144
146
145
- // start the process of deleting nodes
146
- ng .clusterUpdateMutex .Lock ()
147
- defer ng .clusterUpdateMutex .Unlock ()
148
-
149
147
// get the unique ids of the nodes to be deleted
150
- nodeIDs , finished , err := getNodeIDsToDelete (ng , nodes , currentSize )
148
+ nodeIDs , finished , err := getNodeIDsToDelete (ng )
151
149
if finished { // nodes have been deleted by other goroutine
152
150
return nil
153
151
}
@@ -215,26 +213,13 @@ func checkAndUpdate(ng *NodeGroup, nodes []*apiv1.Node) (int, error) {
215
213
// getNodeIDsToDelete checks whether there're still nodes waiting for being deleted. If there're no nodes
216
214
// to delete, it will return true, representing that the process of deleting the nodes has been finished;
217
215
// otherwise, it will return a slice of node ids to be deleted.
218
- func getNodeIDsToDelete (ng * NodeGroup , nodes [] * apiv1. Node , currentSize int ) ([]string , bool , error ) {
216
+ func getNodeIDsToDelete (ng * NodeGroup ) ([]string , bool , error ) {
219
217
// check whether the nodes has already been deleted by other goroutine
220
- ng .deleteMutex .Lock ()
221
218
// If current goroutine is not the first one to acquire the ng.clusterUpdateMutex,
222
219
// it's possible that the nodes have already been deleted, which makes ng.nodesToDelete to be empty.
223
220
if len (ng .nodesToDelete ) == 0 {
224
- ng .deleteMutex .Unlock ()
225
221
return nil , true , nil
226
222
}
227
- ng .deleteMutex .Unlock ()
228
-
229
- // wait for more nodes to be added to ng.nodesToDelete
230
- time .Sleep (ng .deleteWaitTime )
231
-
232
- // get a copy of the nodes to be deleted and release the lock
233
- ng .deleteMutex .Lock ()
234
- nodes = make ([]* apiv1.Node , len (ng .nodesToDelete ))
235
- copy (nodes , ng .nodesToDelete )
236
- ng .nodesToDelete = nil
237
- ng .deleteMutex .Unlock ()
238
223
239
224
// check whether the cluster is available for deleting nodes
240
225
canUpdate , status , err := ng .huaweiCloudManager .canUpdate ()
@@ -245,17 +230,15 @@ func getNodeIDsToDelete(ng *NodeGroup, nodes []*apiv1.Node, currentSize int) ([]
245
230
return nil , false , fmt .Errorf ("cluster is in %s status, cannot perform node deletion now" , status )
246
231
}
247
232
248
- // check again whether deleting the nodes is valid
249
- if currentSize - len (nodes ) < ng .MinSize () {
250
- return nil , false , fmt .Errorf ("the size of the node pool is not sufficient for deleting the nodes, target size:%d, minimum size:%d" , currentSize - len (nodes ), ng .MinSize ())
251
- }
252
-
253
233
nodeIDs := make ([]string , 0 )
254
- for _ , node := range nodes {
234
+ for _ , node := range ng . nodesToDelete {
255
235
// the node.Spec.ProviderID is the node's uid
256
236
klog .Infof ("Delete node with node id: %s" , node .Spec .ProviderID )
257
237
nodeIDs = append (nodeIDs , node .Spec .ProviderID )
258
238
}
239
+
240
+ ng .nodesToDelete = nil
241
+
259
242
return nodeIDs , false , nil
260
243
}
261
244
0 commit comments