Skip to content

Commit 91a53b6

Browse files
authored
Merge pull request #84917 from andyzhangx/deletedisk-check
fix race condition when delete azure disk right after that attach azure disk
2 parents 9488fbe + b6afc86 commit 91a53b6

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_common.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"path"
2525
"strings"
26+
"sync"
2627
"time"
2728

2829
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
@@ -65,7 +66,9 @@ type controllerCommon struct {
6566
location string
6667
storageEndpointSuffix string
6768
resourceGroup string
68-
cloud *Cloud
69+
// store disk URI when disk is in attaching or detaching process
70+
diskAttachDetachMap sync.Map
71+
cloud *Cloud
6972
}
7073

7174
// getNodeVMSet gets the VMSet interface based on config.VMType and the real virtual machine type.
@@ -151,6 +154,8 @@ func (c *controllerCommon) AttachDisk(isManagedDisk bool, diskName, diskURI stri
151154
}
152155

153156
klog.V(2).Infof("Trying to attach volume %q lun %d to node %q.", diskURI, lun, nodeName)
157+
c.diskAttachDetachMap.Store(strings.ToLower(diskURI), "attaching")
158+
defer c.diskAttachDetachMap.Delete(strings.ToLower(diskURI))
154159
return lun, vmset.AttachDisk(isManagedDisk, diskName, diskURI, nodeName, lun, cachingMode, diskEncryptionSetID)
155160
}
156161

@@ -177,14 +182,18 @@ func (c *controllerCommon) DetachDisk(diskName, diskURI string, nodeName types.N
177182

178183
// make the lock here as small as possible
179184
diskOpMutex.LockKey(instanceid)
185+
c.diskAttachDetachMap.Store(strings.ToLower(diskURI), "detaching")
180186
resp, err := vmset.DetachDisk(diskName, diskURI, nodeName)
187+
c.diskAttachDetachMap.Delete(strings.ToLower(diskURI))
181188
diskOpMutex.UnlockKey(instanceid)
182189

183190
if c.cloud.CloudProviderBackoff && shouldRetryHTTPRequest(resp, err) {
184191
klog.V(2).Infof("azureDisk - update backing off: detach disk(%s, %s), err: %v", diskName, diskURI, err)
185192
retryErr := kwait.ExponentialBackoff(c.cloud.RequestBackoff(), func() (bool, error) {
186193
diskOpMutex.LockKey(instanceid)
194+
c.diskAttachDetachMap.Store(strings.ToLower(diskURI), "detaching")
187195
resp, err := vmset.DetachDisk(diskName, diskURI, nodeName)
196+
c.diskAttachDetachMap.Delete(strings.ToLower(diskURI))
188197
diskOpMutex.UnlockKey(instanceid)
189198
return c.cloud.processHTTPRetryResponse(nil, "", resp, err)
190199
})
@@ -226,9 +235,13 @@ func (c *controllerCommon) GetDiskLun(diskName, diskURI string, nodeName types.N
226235
if disk.Lun != nil && (disk.Name != nil && diskName != "" && strings.EqualFold(*disk.Name, diskName)) ||
227236
(disk.Vhd != nil && disk.Vhd.URI != nil && diskURI != "" && strings.EqualFold(*disk.Vhd.URI, diskURI)) ||
228237
(disk.ManagedDisk != nil && strings.EqualFold(*disk.ManagedDisk.ID, diskURI)) {
229-
// found the disk
230-
klog.V(2).Infof("azureDisk - find disk: lun %d name %q uri %q", *disk.Lun, diskName, diskURI)
231-
return *disk.Lun, nil
238+
if disk.ToBeDetached != nil && *disk.ToBeDetached {
239+
klog.Warningf("azureDisk - find disk(ToBeDetached): lun %d name %q uri %q", *disk.Lun, diskName, diskURI)
240+
} else {
241+
// found the disk
242+
klog.V(2).Infof("azureDisk - find disk: lun %d name %q uri %q", *disk.Lun, diskName, diskURI)
243+
return *disk.Lun, nil
244+
}
232245
}
233246
}
234247
return -1, fmt.Errorf("cannot find Lun for disk %s", diskName)

staging/src/k8s.io/legacy-cloud-providers/azure/azure_managedDiskController.go

+4
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ func (c *ManagedDiskController) DeleteManagedDisk(diskURI string) error {
201201
ctx, cancel := getContextWithCancel()
202202
defer cancel()
203203

204+
if _, ok := c.common.diskAttachDetachMap.Load(strings.ToLower(diskURI)); ok {
205+
return fmt.Errorf("failed to delete disk(%s) since it's in attaching or detaching state", diskURI)
206+
}
207+
204208
_, err = c.common.cloud.DisksClient.Delete(ctx, resourceGroup, diskName)
205209
if err != nil {
206210
return err

0 commit comments

Comments
 (0)