Skip to content

Commit e6f1522

Browse files
committed
propagate deleteMachine annotation
1 parent 44e0cd5 commit e6f1522

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

azure/scope/machinepool.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,27 @@ func (m *MachinePoolScope) applyAzureMachinePoolMachines(ctx context.Context) er
405405
}
406406

407407
existingMachinesByProviderID := make(map[string]infrav1exp.AzureMachinePoolMachine, len(ampms))
408-
for _, machine := range ampms {
409-
existingMachinesByProviderID[machine.Spec.ProviderID] = machine
408+
for _, ampm := range ampms {
409+
// propagate Machine delete annotation from owner machine to AzureMachinePoolMachine
410+
// this ensures setting a deleteMachine annotation on the Machine has an effect on the AzureMachinePoolMachine
411+
// and the deployment strategy.
412+
machine, err := util.GetOwnerMachine(ctx, m.client, ampm.ObjectMeta)
413+
if err != nil {
414+
// TODO(mw): which verbosity? info or error?
415+
log.V(4).Info("failed to get owner machine", "machine", ampm.Spec.ProviderID)
416+
continue
417+
}
418+
if machine != nil && machine.Annotations != nil {
419+
if _, hasDeleteAnnotation := machine.Annotations[clusterv1.DeleteMachineAnnotation]; hasDeleteAnnotation {
420+
log.V(4).Info("propagating DeleteMachineAnnotation", "machine", ampm.Spec.ProviderID)
421+
if ampm.Annotations == nil {
422+
ampm.Annotations = make(map[string]string)
423+
}
424+
ampm.Annotations[clusterv1.DeleteMachineAnnotation] = machine.Annotations[clusterv1.DeleteMachineAnnotation]
425+
}
426+
}
427+
428+
existingMachinesByProviderID[ampm.Spec.ProviderID] = ampm
410429
}
411430

412431
// determine which machines need to be created to reflect the current state in Azure

azure/scope/machinepool_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"reflect"
2323
"testing"
24+
"time"
2425

2526
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
2627
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2"
@@ -1414,6 +1415,46 @@ func TestMachinePoolScope_applyAzureMachinePoolMachines(t *testing.T) {
14141415
g.Expect(list.Items).Should(HaveLen(1))
14151416
},
14161417
},
1418+
{
1419+
Name: "if MachinePool is not externally managed, and Machines have delete machine annotation, and overProvisionCount > 0, delete machines with deleteMachine annotation first",
1420+
Setup: func(mp *expv1.MachinePool, amp *infrav1exp.AzureMachinePool, vmssState *azure.VMSS, cb *fake.ClientBuilder) {
1421+
mp.Spec.Replicas = ptr.To[int32](2)
1422+
1423+
mpm1, ampm1 := getAzureMachinePoolMachineWithOwnerMachine(1)
1424+
1425+
mpm2, ampm2 := getAzureMachinePoolMachineWithOwnerMachine(2)
1426+
mpm2.Annotations = map[string]string{
1427+
clusterv1.DeleteMachineAnnotation: time.Now().String(),
1428+
}
1429+
1430+
mpm3, ampm3 := getAzureMachinePoolMachineWithOwnerMachine(3)
1431+
objects := []client.Object{&mpm1, &ampm1, &mpm2, &ampm2, &mpm3, &ampm3}
1432+
cb.WithObjects(objects...)
1433+
1434+
vmssState.Instances = []azure.VMSSVM{
1435+
{
1436+
ID: "/subscriptions/123/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachineScaleSets/my-vmss/virtualMachines/1",
1437+
Name: "ampm1",
1438+
},
1439+
{
1440+
ID: "/subscriptions/123/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachineScaleSets/my-vmss/virtualMachines/2",
1441+
Name: "ampm2",
1442+
},
1443+
{
1444+
ID: "/subscriptions/123/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachineScaleSets/my-vmss/virtualMachines/3",
1445+
Name: "ampm3",
1446+
},
1447+
}
1448+
},
1449+
Verify: func(g *WithT, amp *infrav1exp.AzureMachinePool, c client.Client, err error) {
1450+
g.Expect(err).NotTo(HaveOccurred())
1451+
list := clusterv1.MachineList{}
1452+
g.Expect(c.List(ctx, &list)).NotTo(HaveOccurred())
1453+
g.Expect(list.Items).Should(HaveLen(2))
1454+
g.Expect(list.Items[0].Name).Should(Equal("mpm1"))
1455+
g.Expect(list.Items[1].Name).Should(Equal("mpm3"))
1456+
},
1457+
},
14171458
{
14181459
Name: "if existing MachinePool is not present, reduce replicas",
14191460
Setup: func(mp *expv1.MachinePool, amp *infrav1exp.AzureMachinePool, vmssState *azure.VMSS, cb *fake.ClientBuilder) {

0 commit comments

Comments
 (0)