Skip to content

Commit c030a10

Browse files
committed
fix deleting logic
1 parent 76031bc commit c030a10

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

azure/scope/strategies/machinepool_deployments/machinepool_deployment_strategy.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ func (rollingUpdateStrategy rollingUpdateStrategy) SelectMachinesToDelete(ctx co
144144
}()
145145
)
146146

147+
// Order AzureMachinePoolMachines with the clusterv1.DeleteMachineAnnotation to the front so that they have delete priority.
148+
// This allows MachinePool Machines to work with the autoscaler.
149+
failedMachines = orderByDeleteMachineAnnotation(failedMachines)
150+
deletingMachines = orderByDeleteMachineAnnotation(deletingMachines)
151+
147152
log.Info("selecting machines to delete",
148153
"readyMachines", len(readyMachines),
149154
"desiredReplicaCount", desiredReplicaCount,
@@ -155,18 +160,18 @@ func (rollingUpdateStrategy rollingUpdateStrategy) SelectMachinesToDelete(ctx co
155160
"deletingMachines", len(deletingMachines),
156161
)
157162

158-
// if we have machines annotated with delete machine, remove them
159-
if len(deleteAnnotatedMachines) > 0 {
160-
log.Info("delete annotated machines", "desiredReplicaCount", desiredReplicaCount, "maxUnavailable", maxUnavailable, "deleteAnnotatedMachines", getProviderIDs(deleteAnnotatedMachines))
161-
return deleteAnnotatedMachines, nil
162-
}
163-
164163
// if we have failed or deleting machines, remove them
165164
if len(failedMachines) > 0 || len(deletingMachines) > 0 {
166165
log.Info("failed or deleting machines", "desiredReplicaCount", desiredReplicaCount, "maxUnavailable", maxUnavailable, "failedMachines", getProviderIDs(failedMachines), "deletingMachines", getProviderIDs(deletingMachines))
167166
return append(failedMachines, deletingMachines...), nil
168167
}
169168

169+
// if we have machines annotated with delete machine, remove them
170+
if len(deleteAnnotatedMachines) > 0 {
171+
log.Info("delete annotated machines", "desiredReplicaCount", desiredReplicaCount, "maxUnavailable", maxUnavailable, "deleteAnnotatedMachines", getProviderIDs(deleteAnnotatedMachines))
172+
return deleteAnnotatedMachines, nil
173+
}
174+
170175
// if we have not yet reached our desired count, don't try to delete anything
171176
if len(readyMachines) < int(desiredReplicaCount) {
172177
log.Info("not enough ready machines", "desiredReplicaCount", desiredReplicaCount, "readyMachinesCount", len(readyMachines), "machinesByProviderID", len(machinesByProviderID))
@@ -317,6 +322,18 @@ func orderRandom(machines []infrav1exp.AzureMachinePoolMachine) []infrav1exp.Azu
317322
return machines
318323
}
319324

325+
// orderByDeleteMachineAnnotation will sort AzureMachinePoolMachines with the clusterv1.DeleteMachineAnnotation to the front of the list.
326+
// It will preserve the existing order of the list otherwise so that it respects the existing delete priority otherwise.
327+
func orderByDeleteMachineAnnotation(machines []infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
328+
sort.SliceStable(machines, func(i, j int) bool {
329+
_, iHasAnnotation := machines[i].Annotations[clusterv1.DeleteMachineAnnotation]
330+
331+
return iHasAnnotation
332+
})
333+
334+
return machines
335+
}
336+
320337
func getProviderIDs(machines []infrav1exp.AzureMachinePoolMachine) []string {
321338
ids := make([]string, len(machines))
322339
for i, machine := range machines {

0 commit comments

Comments
 (0)