Skip to content

Commit 7fdf13e

Browse files
committed
azure: Fix disk deletion by blocking on parent VMSS instead of VM instance
Signed-off-by: Ciprian Hacman <ciprian@hakman.dev>
1 parent 0187158 commit 7fdf13e

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

pkg/resources/azure/azure.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,13 @@ func (g *resourceGetter) listDisks(ctx context.Context) ([]*resources.Resource,
579579
func (g *resourceGetter) toDiskResource(disk *compute.Disk) *resources.Resource {
580580
var blocked []string
581581
if disk.ManagedBy != nil {
582-
blocked = append(blocked, toKey(typeVMScaleSetVM, *disk.ManagedBy))
582+
// Block on the parent VMScaleSet, not the individual VM instance.
583+
// The raw ManagedBy path may not match the listed VM's resource ID,
584+
// but parsing it to extract the parent VMSS gives a reliable match.
585+
vmID, err := arm.ParseResourceID(*disk.ManagedBy)
586+
if err == nil && vmID.Parent != nil {
587+
blocked = append(blocked, toKey(typeVMScaleSet, vmID.Parent.String()))
588+
}
583589
}
584590

585591
return &resources.Resource{

pkg/resources/azure/azure_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func TestListResourcesAzure(t *testing.T) {
283283
rtype: typeDisk,
284284
name: diskName,
285285
blocks: []string{toKey(typeResourceGroup, rgID)},
286-
blocked: []string{toKey(typeVMScaleSetVM, vmID)},
286+
blocked: []string{toKey(typeVMScaleSet, vmssID)},
287287
},
288288
toKey(typeRoleAssignment, raID): {
289289
rtype: typeRoleAssignment,
@@ -307,6 +307,49 @@ func TestListResourcesAzure(t *testing.T) {
307307
}
308308
}
309309

310+
func TestToDiskResource_ManagedBy(t *testing.T) {
311+
g := resourceGetter{
312+
clusterInfo: resources.ClusterInfo{
313+
AzureSubscriptionID: "sub",
314+
AzureResourceGroupName: "rg",
315+
},
316+
}
317+
318+
diskID := "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/disks/disk"
319+
diskName := "disk"
320+
vmssID := "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/vmss"
321+
322+
tests := []struct {
323+
name string
324+
managedBy *string
325+
wantBlocked []string
326+
}{
327+
{
328+
name: "nil ManagedBy",
329+
managedBy: nil,
330+
},
331+
{
332+
name: "valid ManagedBy blocks on parent VMSS",
333+
managedBy: to.Ptr(vmssID + "/virtualMachines/0"),
334+
wantBlocked: []string{toKey(typeVMScaleSet, vmssID)},
335+
},
336+
}
337+
338+
for _, tc := range tests {
339+
t.Run(tc.name, func(t *testing.T) {
340+
disk := &compute.Disk{
341+
ID: to.Ptr(diskID),
342+
Name: to.Ptr(diskName),
343+
ManagedBy: tc.managedBy,
344+
}
345+
r := g.toDiskResource(disk)
346+
if !reflect.DeepEqual(r.Blocked, tc.wantBlocked) {
347+
t.Errorf("got Blocked=%v, want %v", r.Blocked, tc.wantBlocked)
348+
}
349+
})
350+
}
351+
}
352+
310353
func TestIsOwnedByCluster(t *testing.T) {
311354
clusterName := "test-cluster"
312355

0 commit comments

Comments
 (0)