@@ -66,7 +66,7 @@ func fastDeploy(
6666 }
6767 logger .Info ("Got destination file paths" , "dstFilePaths" , dstFilePaths )
6868
69- // Collect the disks and remove the storage profile from them .
69+ // Collect the disks.
7070 var (
7171 disks []* vimtypes.VirtualDisk
7272 diskSpecs []* vimtypes.VirtualDeviceConfigSpec
@@ -248,7 +248,8 @@ func fastDeploy(
248248 diskSpecs ,
249249 dstDiskFormat ,
250250 dstDiskPaths ,
251- srcDiskPaths )
251+ srcDiskPaths ,
252+ createArgs .IsEncryptedStorageProfile )
252253}
253254
254255func fastDeployLinked (
@@ -330,45 +331,65 @@ func fastDeployDirect(
330331 diskSpecs []* vimtypes.VirtualDeviceConfigSpec ,
331332 diskFormat vimtypes.DatastoreSectorFormat ,
332333 dstDiskPaths ,
333- srcDiskPaths []string ) (* vimtypes.ManagedObjectReference , error ) {
334+ srcDiskPaths []string ,
335+ isEncryptedStorageProfile bool ) (* vimtypes.ManagedObjectReference , error ) {
334336
335337 logger := pkglog .FromContextOrDefault (ctx ).WithName ("fastDeployDirect" )
336338
337- // Copy each disk into the VM directory.
338- if err := fastDeployDirectCopyDisks (
339- ctx ,
340- logger ,
341- datacenter ,
342- configSpec ,
343- srcDiskPaths ,
344- dstDiskPaths ,
345- diskFormat ); err != nil {
346-
347- return nil , err
348- }
349-
350- _ , isVMEncrypted := configSpec .Crypto .(* vimtypes.CryptoSpecEncrypt )
351-
339+ diskCopySpecs := make ([]vimtypes.FileBackedVirtualDiskSpec , len (dstDiskPaths ))
352340 for i := range diskSpecs {
353341 ds := diskSpecs [i ]
354342
355343 // Set the file operation to an empty string since the disk already
356344 // exists.
357345 ds .FileOperation = ""
358346
359- if isVMEncrypted {
360- // If the VM is to be encrypted, then the disks need to be updated
361- // so they are not marked as encrypted upon VM creation. This is
362- // because it is not possible to change the encryption state of VM
363- // disks when they are being attached. Instead the disks must be
364- // encrypted after they are attached to the VM.
365- ds .Profile = nil
366- if ds .Backing != nil {
367- ds .Backing .Crypto = nil
347+ profile := ds .Profile
348+ if len (profile ) == 0 {
349+ profile = configSpec .VmProfile
350+ }
351+
352+ diskCopySpecs [i ] = vimtypes.FileBackedVirtualDiskSpec {
353+ VirtualDiskSpec : vimtypes.VirtualDiskSpec {
354+ AdapterType : string (vimtypes .VirtualDiskAdapterTypeLsiLogic ),
355+ DiskType : string (vimtypes .VirtualDiskTypeThin ),
356+ },
357+ SectorFormat : string (diskFormat ),
358+ Profile : profile ,
359+ }
360+
361+ // Copy the disks using the same crypto key as the VM if the storage
362+ // class is encrypted.
363+ if isEncryptedStorageProfile && configSpec .Crypto != nil {
364+ if ds .Backing == nil {
365+ ds .Backing = & vimtypes.VirtualDeviceConfigSpecBackingSpec {}
366+ }
367+ crypto := ds .Backing .Crypto
368+ if crypto == nil {
369+ crypto = configSpec .Crypto
368370 }
371+ diskCopySpecs [i ].Crypto = crypto
372+
373+ // Please note, the disk is added to the VM with its crypto spec
374+ // set to nil since it is an existing disk. The disk is already
375+ // encrypted as part of the copy operation.
376+ ds .Profile = profile
377+ ds .Backing .Crypto = nil
369378 }
370379 }
371380
381+ // Copy each disk into the VM directory.
382+ if err := fastDeployDirectCopyDisks (
383+ ctx ,
384+ logger ,
385+ datacenter ,
386+ srcDiskPaths ,
387+ dstDiskPaths ,
388+ diskCopySpecs ); err != nil {
389+
390+ return nil , err
391+ }
392+
372393 return fastDeployCreateVM (ctx , logger , folder , pool , host , configSpec )
373394}
374395
@@ -410,43 +431,35 @@ func fastDeployDirectCopyDisks(
410431 ctx context.Context ,
411432 logger logr.Logger ,
412433 datacenter * object.Datacenter ,
413- configSpec vimtypes.VirtualMachineConfigSpec ,
414434 srcDiskPaths ,
415435 dstDiskPaths []string ,
416- diskFormat vimtypes.DatastoreSectorFormat ) error {
436+ dstDiskSpecs [] vimtypes.FileBackedVirtualDiskSpec ) error {
417437
418438 var (
419439 wg sync.WaitGroup
420440 copyDiskTasks = make ([]* object.Task , len (srcDiskPaths ))
421441 copyDiskErrs = make (chan error , len (srcDiskPaths ))
422- copyDiskSpec = vimtypes.FileBackedVirtualDiskSpec {
423- VirtualDiskSpec : vimtypes.VirtualDiskSpec {
424- AdapterType : string (vimtypes .VirtualDiskAdapterTypeLsiLogic ),
425- DiskType : string (vimtypes .VirtualDiskTypeThin ),
426- },
427- SectorFormat : string (diskFormat ),
428- Profile : configSpec .VmProfile ,
429- }
430- diskManager = object .NewVirtualDiskManager (datacenter .Client ())
442+ diskManager = object .NewVirtualDiskManager (datacenter .Client ())
431443 )
432444
433445 for i := range srcDiskPaths {
434446 s := srcDiskPaths [i ]
435447 d := dstDiskPaths [i ]
448+ c := dstDiskSpecs [i ]
436449
437450 logger .Info (
438451 "Copying disk" ,
439452 "dstDiskPath" , d ,
440453 "srcDiskPath" , s ,
441- "copyDiskSpec " , copyDiskSpec )
454+ "dstDiskSpec " , c )
442455
443456 t , err := diskManager .CopyVirtualDisk (
444457 ctx ,
445458 s ,
446459 datacenter ,
447460 d ,
448461 datacenter ,
449- & copyDiskSpec ,
462+ & c ,
450463 false )
451464 if err != nil {
452465 logger .Error (err , "failed to copy disk, cancelling other tasks" )
0 commit comments