Skip to content

Commit f34ff17

Browse files
committed
delete annotated machines first
1 parent 2186ba3 commit f34ff17

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

azure/scope/strategies/machinepool_deployments/machinepool_deployment_strategy.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ func (rollingUpdateStrategy rollingUpdateStrategy) SelectMachinesToDelete(ctx co
117117
return nil, err
118118
}
119119

120+
log := ctrl.LoggerFrom(ctx).V(4)
121+
122+
// first check if there are any machines with the delete annotations and in this case, only delete those. This allows CAPZ to work with autoscaler.
123+
deleteAnnotatedMachines := getDeleteAnnotatedMachines(machinesByProviderID)
124+
if len(deleteAnnotatedMachines) > 0 {
125+
log.Info("delete annotated machines", "desiredReplicaCount", desiredReplicaCount, "maxUnavailable", maxUnavailable, "deleteAnnotatedMachines", getProviderIDs(deleteAnnotatedMachines))
126+
return deleteAnnotatedMachines, nil
127+
}
128+
120129
var (
121130
order = func() func(machines []infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
122131
switch rollingUpdateStrategy.DeletePolicy {
@@ -128,7 +137,6 @@ func (rollingUpdateStrategy rollingUpdateStrategy) SelectMachinesToDelete(ctx co
128137
return orderRandom
129138
}
130139
}()
131-
log = ctrl.LoggerFrom(ctx).V(4)
132140
failedMachines = order(getFailedMachines(machinesByProviderID))
133141
deletingMachines = order(getDeletingMachines(machinesByProviderID))
134142
readyMachines = order(getReadyMachines(machinesByProviderID))
@@ -143,13 +151,6 @@ func (rollingUpdateStrategy rollingUpdateStrategy) SelectMachinesToDelete(ctx co
143151
}()
144152
)
145153

146-
// Order AzureMachinePoolMachines with the clutserv1.DeleteMachineAnnotation to the front so that they have delete priority.
147-
// This allows MachinePool Machines to work with the autoscaler.
148-
failedMachines = orderByDeleteMachineAnnotation(failedMachines)
149-
deletingMachines = orderByDeleteMachineAnnotation(deletingMachines)
150-
readyMachines = orderByDeleteMachineAnnotation(readyMachines)
151-
machinesWithoutLatestModel = orderByDeleteMachineAnnotation(machinesWithoutLatestModel)
152-
153154
log.Info("selecting machines to delete",
154155
"readyMachines", len(readyMachines),
155156
"desiredReplicaCount", desiredReplicaCount,
@@ -224,6 +225,18 @@ func (rollingUpdateStrategy rollingUpdateStrategy) SelectMachinesToDelete(ctx co
224225
return toDelete, nil
225226
}
226227

228+
func getDeleteAnnotatedMachines(machinesByProviderID map[string]infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
229+
var machines []infrav1exp.AzureMachinePoolMachine
230+
for _, v := range machinesByProviderID {
231+
if v.Annotations != nil {
232+
if _, hasDeleteAnnotation := v.Annotations[clusterv1.DeleteMachineAnnotation]; hasDeleteAnnotation {
233+
machines = append(machines, v)
234+
}
235+
}
236+
}
237+
return machines
238+
}
239+
227240
func getFailedMachines(machinesByProviderID map[string]infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
228241
var machines []infrav1exp.AzureMachinePoolMachine
229242
for _, v := range machinesByProviderID {
@@ -304,18 +317,6 @@ func orderRandom(machines []infrav1exp.AzureMachinePoolMachine) []infrav1exp.Azu
304317
return machines
305318
}
306319

307-
// orderByDeleteMachineAnnotation will sort AzureMachinePoolMachines with the clusterv1.DeleteMachineAnnotation to the front of the list.
308-
// It will preserve the existing order of the list otherwise so that it respects the existing delete priority otherwise.
309-
func orderByDeleteMachineAnnotation(machines []infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
310-
sort.SliceStable(machines, func(i, j int) bool {
311-
_, iHasAnnotation := machines[i].Annotations[clusterv1.DeleteMachineAnnotation]
312-
313-
return iHasAnnotation
314-
})
315-
316-
return machines
317-
}
318-
319320
func getProviderIDs(machines []infrav1exp.AzureMachinePoolMachine) []string {
320321
ids := make([]string, len(machines))
321322
for i, machine := range machines {

0 commit comments

Comments
 (0)