diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_managedDiskController.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_managedDiskController.go index 78fd2468ee0..e93401eb504 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_managedDiskController.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_managedDiskController.go @@ -297,6 +297,10 @@ func (c *ManagedDiskController) ResizeDisk(diskURI string, oldSize resource.Quan return newSizeQuant, nil } + if result.DiskProperties.DiskState != compute.Unattached { + return oldSize, fmt.Errorf("azureDisk - disk resize is only supported on Unattached disk, current disk state: %s, already attached to %s", result.DiskProperties.DiskState, to.String(result.ManagedBy)) + } + diskParameter := compute.DiskUpdate{ DiskUpdateProperties: &compute.DiskUpdateProperties{ DiskSizeGB: &requestGiB, diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_managedDiskController_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_managedDiskController_test.go index 1e40eeb4933..7ead4f5cf27 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_managedDiskController_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_managedDiskController_test.go @@ -311,7 +311,7 @@ func TestResizeDisk(t *testing.T) { diskName: diskName, oldSize: *resource.NewQuantity(2*(1024*1024*1024), resource.BinarySI), newSize: *resource.NewQuantity(3*(1024*1024*1024), resource.BinarySI), - existedDisk: compute.Disk{Name: to.StringPtr("disk1"), DiskProperties: &compute.DiskProperties{DiskSizeGB: &diskSizeGB}}, + existedDisk: compute.Disk{Name: to.StringPtr("disk1"), DiskProperties: &compute.DiskProperties{DiskSizeGB: &diskSizeGB, DiskState: compute.Unattached}}, expectedQuantity: *resource.NewQuantity(3*(1024*1024*1024), resource.BinarySI), expectedErr: false, }, @@ -330,7 +330,7 @@ func TestResizeDisk(t *testing.T) { diskName: diskName, oldSize: *resource.NewQuantity(1*(1024*1024*1024), resource.BinarySI), newSize: *resource.NewQuantity(2*(1024*1024*1024), resource.BinarySI), - existedDisk: compute.Disk{Name: to.StringPtr("disk1"), DiskProperties: &compute.DiskProperties{DiskSizeGB: &diskSizeGB}}, + existedDisk: compute.Disk{Name: to.StringPtr("disk1"), DiskProperties: &compute.DiskProperties{DiskSizeGB: &diskSizeGB, DiskState: compute.Unattached}}, expectedQuantity: *resource.NewQuantity(2*(1024*1024*1024), resource.BinarySI), expectedErr: false, }, @@ -339,7 +339,7 @@ func TestResizeDisk(t *testing.T) { diskName: fakeGetDiskFailed, oldSize: *resource.NewQuantity(2*(1024*1024*1024), resource.BinarySI), newSize: *resource.NewQuantity(3*(1024*1024*1024), resource.BinarySI), - existedDisk: compute.Disk{Name: to.StringPtr(fakeGetDiskFailed), DiskProperties: &compute.DiskProperties{DiskSizeGB: &diskSizeGB}}, + existedDisk: compute.Disk{Name: to.StringPtr(fakeGetDiskFailed), DiskProperties: &compute.DiskProperties{DiskSizeGB: &diskSizeGB, DiskState: compute.Unattached}}, expectedQuantity: *resource.NewQuantity(2*(1024*1024*1024), resource.BinarySI), expectedErr: true, expectedErrMsg: fmt.Errorf("Retriable: false, RetryAfter: 0s, HTTPStatusCode: 0, RawError: Get Disk failed"), @@ -349,11 +349,21 @@ func TestResizeDisk(t *testing.T) { diskName: fakeCreateDiskFailed, oldSize: *resource.NewQuantity(2*(1024*1024*1024), resource.BinarySI), newSize: *resource.NewQuantity(3*(1024*1024*1024), resource.BinarySI), - existedDisk: compute.Disk{Name: to.StringPtr(fakeCreateDiskFailed), DiskProperties: &compute.DiskProperties{DiskSizeGB: &diskSizeGB}}, + existedDisk: compute.Disk{Name: to.StringPtr(fakeCreateDiskFailed), DiskProperties: &compute.DiskProperties{DiskSizeGB: &diskSizeGB, DiskState: compute.Unattached}}, expectedQuantity: *resource.NewQuantity(2*(1024*1024*1024), resource.BinarySI), expectedErr: true, expectedErrMsg: fmt.Errorf("Retriable: false, RetryAfter: 0s, HTTPStatusCode: 0, RawError: Create Disk failed"), }, + { + desc: "an error shall be returned if disk is not in Unattached state", + diskName: fakeCreateDiskFailed, + oldSize: *resource.NewQuantity(2*(1024*1024*1024), resource.BinarySI), + newSize: *resource.NewQuantity(3*(1024*1024*1024), resource.BinarySI), + existedDisk: compute.Disk{Name: to.StringPtr(fakeCreateDiskFailed), DiskProperties: &compute.DiskProperties{DiskSizeGB: &diskSizeGB, DiskState: compute.Attached}}, + expectedQuantity: *resource.NewQuantity(2*(1024*1024*1024), resource.BinarySI), + expectedErr: true, + expectedErrMsg: fmt.Errorf("azureDisk - disk resize is only supported on Unattached disk, current disk state: Attached, already attached to "), + }, } for i, test := range testCases {