Skip to content

Commit c558e7f

Browse files
authored
Merge pull request kubernetes#84211 from misterikkit/create-idempotent
vsphere: check if volume exists before create
2 parents c6c7e8b + 131668a commit c558e7f

File tree

1 file changed

+12
-4
lines changed
  • staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/diskmanagers

1 file changed

+12
-4
lines changed

staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/diskmanagers/vdm.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,26 @@ func (diskManager virtualDiskManager) Create(ctx context.Context, datastore *vcl
3939
if diskManager.volumeOptions.SCSIControllerType == "" {
4040
diskManager.volumeOptions.SCSIControllerType = vclib.LSILogicControllerType
4141
}
42-
// Create virtual disk
43-
diskFormat := vclib.DiskFormatValidType[diskManager.volumeOptions.DiskFormat]
44-
// Create a virtual disk manager
45-
vdm := object.NewVirtualDiskManager(datastore.Client())
42+
43+
// Check for existing VMDK before attempting create. Because a name collision
44+
// is unlikely, "VMDK already exists" is likely from a previous attempt to
45+
// create this volume.
46+
if dsPath := vclib.GetPathFromVMDiskPath(diskManager.diskPath); datastore.Exists(ctx, dsPath) {
47+
klog.V(2).Infof("Create: VirtualDisk already exists, returning success. Name=%q", diskManager.diskPath)
48+
return diskManager.diskPath, nil
49+
}
50+
4651
// Create specification for new virtual disk
52+
diskFormat := vclib.DiskFormatValidType[diskManager.volumeOptions.DiskFormat]
4753
vmDiskSpec := &types.FileBackedVirtualDiskSpec{
4854
VirtualDiskSpec: types.VirtualDiskSpec{
4955
AdapterType: diskManager.volumeOptions.SCSIControllerType,
5056
DiskType: diskFormat,
5157
},
5258
CapacityKb: int64(diskManager.volumeOptions.CapacityKB),
5359
}
60+
61+
vdm := object.NewVirtualDiskManager(datastore.Client())
5462
requestTime := time.Now()
5563
// Create virtual disk
5664
task, err := vdm.CreateVirtualDisk(ctx, diskManager.diskPath, datastore.Datacenter.Datacenter, vmDiskSpec)

0 commit comments

Comments
 (0)