forked from vmware-tanzu/vm-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_fastdeploy.go
More file actions
588 lines (510 loc) · 16.8 KB
/
create_fastdeploy.go
File metadata and controls
588 lines (510 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// © Broadcom. All Rights Reserved.
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: Apache-2.0
package vmlifecycle
import (
"context"
"errors"
"fmt"
"path"
"strings"
"sync"
"time"
"github.com/go-logr/logr"
"github.com/vmware/govmomi/fault"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25"
vimtypes "github.com/vmware/govmomi/vim25/types"
pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config"
pkgconst "github.com/vmware-tanzu/vm-operator/pkg/constants"
pkgctx "github.com/vmware-tanzu/vm-operator/pkg/context"
pkglog "github.com/vmware-tanzu/vm-operator/pkg/log"
pkgutil "github.com/vmware-tanzu/vm-operator/pkg/util"
"github.com/vmware-tanzu/vm-operator/pkg/util/ptr"
)
//nolint:gocyclo
func fastDeploy(
vmCtx pkgctx.VirtualMachineContext,
vimClient *vim25.Client,
createArgs *CreateArgs) (vmRef *vimtypes.ManagedObjectReference, retErr error) {
logger := vmCtx.Logger.WithName("fastDeploy")
if len(createArgs.Datastores) == 0 {
return nil, errors.New("no compatible datastores")
}
vmDir := path.Dir(createArgs.ConfigSpec.Files.VmPathName)
logger.Info("Got vm dir", "vmDir", vmDir)
var dp object.DatastorePath
if !dp.FromString(vmDir) {
return nil, fmt.Errorf("invalid datastore path: %s", vmDir)
}
vmDirName := dp.Path
logger.Info("Got vm dir name", "vmDirName", vmDirName)
srcFilePaths := createArgs.FilePaths
logger.Info("Got source file paths", "srcFilePaths", srcFilePaths)
srcDiskPaths := createArgs.DiskPaths
logger.Info("Got source disk paths", "srcDiskPaths", srcDiskPaths)
dstDiskFormat := pkgutil.GetPreferredDiskFormat(
createArgs.Datastores[0].DiskFormats...)
logger.Info("Got destination disk format", "dstDiskFormat", dstDiskFormat)
datacenter := object.NewDatacenter(
vimClient,
vimtypes.ManagedObjectReference{
Type: string(vimtypes.ManagedObjectTypeDatacenter),
Value: createArgs.DatacenterMoID,
})
logger.Info("Got datacenter", "datacenter", datacenter.Reference())
// Create the directory where the VM will be created.
var (
vmDirPath string
vmDirUUIDPath string
nm *object.DatastoreNamespaceManager
fm = object.NewFileManager(vimClient)
)
if createArgs.Datastores[0].TopLevelDirectoryCreateSupported {
//
// The datastore supports TLD creation, so just use file manager.
//
if err := fm.MakeDirectory(vmCtx, vmDir, datacenter, true); err != nil {
return nil, fmt.Errorf("failed to create vm dir %q: %w", vmDir, err)
}
vmDirPath = vmDir
} else {
//
// The datastore does NOT support TLD creation, so use the datastore
// namespace manager to create the new directory.
//
nm = object.NewDatastoreNamespaceManager(vimClient)
vdp, err := nm.CreateDirectory(
vmCtx,
object.NewDatastore(vimClient, createArgs.Datastores[0].MoRef),
vmDirName,
"")
if err != nil {
return nil, fmt.Errorf("failed to create vm dir: %w", err)
}
// The vmDirUUIDPath value will look something like the following:
//
// /vmfs/volumes/vsan:a5982d36bd6d11f0-9fd50050569d3d0d/dfc05687-5880-431c-9924-a3a34837443e
vmDirUUIDPath = vdp
logger.Info("Got vm dir UUID path", "vmDirUUIDPath", vmDirUUIDPath)
// Get the name of the newly created directory. From the above example
// this would be dfc05687-5880-431c-9924-a3a34837443e.
vmDirUUIDName := path.Base(vmDirUUIDPath)
// Update the original VM directory path to use the newly created dir
// name. If the original path was:
//
// [vsanDatastore-1] bdefaf58-234d-49c0-a152-55a592f37e34
//
// Then using the above example, the new path would be:
//
// [vsanDatastore-1] dfc05687-5880-431c-9924-a3a34837443e
//
// The original path also exists, but as a symlink to the real one.
vmDirPath = strings.ReplaceAll(vmDir, vmDirName, vmDirUUIDName)
// Update the path to the VMX file with the newly created directory
// name. If the original path was:
//
// [vsanDatastore-1] bdefaf58-234d-49c0-a152-55a592f37e34/my-vm-1.vmx
//
// Then using the above example, the new path would be:
//
// [vsanDatastore-1] dfc05687-5880-431c-9924-a3a34837443e/my-vm-1.vmx
//
// The original path also exists, but as a symlink to the real one.
createArgs.ConfigSpec.Files.VmPathName = strings.ReplaceAll(
createArgs.ConfigSpec.Files.VmPathName, vmDirName, vmDirUUIDName)
}
logger.Info("Updated vm dir paths",
"vmDirPath", vmDirPath,
"vmPathName", createArgs.ConfigSpec.Files.VmPathName)
// Register cleanup defer immediately after directory creation.
defer func() {
if retErr == nil {
// Do not delete the VM directory if this function succeeded.
return
}
// Use a context that is not cancelled when the parent is, so cleanup
// runs even if the request is cancelled. Preserves the VC opID so
// cleanup is correlated with the failed create in VC logs.
ctx := context.WithoutCancel(vmCtx.Context)
// Delete the VM directory and its contents.
// Always use FileManager.DeleteDatastoreFile() first as it can delete
// non-empty directories recursively. Note that DeleteDirectory() on a
// non-empty directory will return an error, which is why we delete the
// directory contents first using DeleteDatastoreFile().
t, err := fm.DeleteDatastoreFile(ctx, vmDirPath, datacenter)
if err != nil {
retErr = fmt.Errorf(
"failed to call delete api for vm dir %q: %w,%w",
vmDirPath, err, retErr)
return
}
// Wait for the delete call to return.
if err := t.Wait(ctx); err != nil &&
!fault.Is(err, &vimtypes.FileNotFound{}) {
retErr = fmt.Errorf(
"failed to delete vm dir %q: %w,%w",
vmDirPath, err, retErr)
}
// For non-TLD datastores, also clean up the namespace mapping.
if !createArgs.Datastores[0].TopLevelDirectoryCreateSupported {
if err := nm.DeleteDirectory(
ctx,
datacenter,
vmDirUUIDPath); err != nil &&
!fault.Is(err, &vimtypes.FileNotFound{}) {
retErr = fmt.Errorf(
"failed to delete vm dir namespace mapping %q: %w,%w",
vmDirUUIDPath, err, retErr)
}
}
}()
dstDiskPaths := make([]string, len(srcDiskPaths))
for i := 0; i < len(dstDiskPaths); i++ {
dstDiskPaths[i] = fmt.Sprintf("%s/disk-%d.vmdk", vmDirPath, i)
}
logger.Info("Got destination disk paths", "dstDiskPaths", dstDiskPaths)
dstFilePaths := make([]string, len(srcFilePaths))
for i := 0; i < len(dstFilePaths); i++ {
// Rename file using vm name + same extension
ext := path.Ext(srcFilePaths[i])
name := createArgs.ConfigSpec.Name + ext
dstFilePaths[i] = path.Join(vmDirPath, name)
}
logger.Info("Got destination file paths", "dstFilePaths", dstFilePaths)
var (
disks []*vimtypes.VirtualDisk
diskSpecs []*vimtypes.VirtualDeviceConfigSpec
)
for i := range createArgs.ConfigSpec.DeviceChange {
dc := createArgs.ConfigSpec.DeviceChange[i].GetVirtualDeviceConfigSpec()
if d, ok := dc.Device.(*vimtypes.VirtualDisk); ok {
d.VirtualDiskFormat = string(dstDiskFormat)
disks = append(disks, d)
diskSpecs = append(diskSpecs, dc)
}
}
if a, b := len(srcDiskPaths), len(disks); a != b {
logger.Info("Got disks", "disks", disks)
return nil, fmt.Errorf(
"invalid disk count: len(srcDiskPaths)=%d, len(disks)=%d", a, b)
}
// Update the disks with their expected file names.
for i := range disks {
d := disks[i]
if bfb, ok := d.Backing.(vimtypes.BaseVirtualDeviceFileBackingInfo); ok {
fb := bfb.GetVirtualDeviceFileBackingInfo()
fb.Datastore = &createArgs.Datastores[0].MoRef
fb.FileName = dstDiskPaths[i]
}
}
logger.Info("Got disks", "disks", disks)
// Copy any non-disk files to the target directory.
for i := range dstFilePaths {
task, err := fm.CopyDatastoreFile(
vmCtx,
srcFilePaths[i],
datacenter,
dstFilePaths[i],
datacenter, false)
if err != nil {
return nil, err
}
if err = task.Wait(vmCtx); err != nil {
return nil, err
}
}
// Update the config spec to know about a potential NVRAM file.
for i := range createArgs.ConfigSpec.ExtraConfig {
ov := createArgs.ConfigSpec.ExtraConfig[i].GetOptionValue()
if ov != nil && ov.Key == "nvram" {
if v, ok := ov.Value.(string); ok {
oldNvramPath := v
for j := range dstFilePaths {
if strings.EqualFold(".nvram", path.Ext(dstFilePaths[j])) {
newNvramPath := path.Base(dstFilePaths[j])
logger.Info("Updated NVRAM in ExtraConfig",
"oldValue", oldNvramPath,
"newValue", newNvramPath)
createArgs.ConfigSpec.ExtraConfig[i] = &vimtypes.OptionValue{
Key: ov.Key,
Value: newNvramPath,
}
}
}
}
}
}
folder := object.NewFolder(vimClient, vimtypes.ManagedObjectReference{
Type: "Folder",
Value: createArgs.FolderMoID,
})
logger.Info("Got folder", "folder", folder.Reference())
pool := object.NewResourcePool(vimClient, vimtypes.ManagedObjectReference{
Type: "ResourcePool",
Value: createArgs.ResourcePoolMoID,
})
logger.Info("Got pool", "pool", pool.Reference())
var host *object.HostSystem
if createArgs.HostMoID != "" {
host = object.NewHostSystem(vimClient, vimtypes.ManagedObjectReference{
Type: "HostSystem",
Value: createArgs.HostMoID,
})
logger.Info("Got host", "host", host.Reference())
}
// Determine the type of fast deploy operation.
var (
fastDeployMode string
fastDeployModeReason string
)
switch {
case createArgs.IsEncryptedStorageProfile:
fastDeployMode = pkgconst.FastDeployModeDirect
fastDeployModeReason = "encrypted storage profile"
case !pkgutil.IsOnlinePromoteDisksSupported(createArgs.ConfigSpec):
fastDeployMode = pkgconst.FastDeployModeDirect
fastDeployModeReason = "online promote disks not supported"
default:
fastDeployMode = vmCtx.VM.Annotations[pkgconst.FastDeployAnnotationKey]
if fastDeployMode == "" {
fastDeployMode = pkgcfg.FromContext(vmCtx).FastDeployMode
fastDeployModeReason = "global default"
} else {
fastDeployModeReason = "from annotation"
}
}
logger.Info(
"Deploying VM with Fast Deploy",
"mode", fastDeployMode,
"reason", fastDeployModeReason,
"k8sVMCreateTimestamp", vmCtx.VM.CreationTimestamp.Format(time.RFC3339))
if strings.EqualFold(fastDeployMode, pkgconst.FastDeployModeLinked) {
return fastDeployLinked(
vmCtx,
folder,
pool,
host,
createArgs.ConfigSpec,
createArgs.Datastores[0].MoRef,
disks,
diskSpecs,
srcDiskPaths)
}
return fastDeployDirect(
vmCtx,
datacenter,
folder,
pool,
host,
createArgs.ConfigSpec,
diskSpecs,
dstDiskFormat,
dstDiskPaths,
srcDiskPaths)
}
func fastDeployLinked(
ctx context.Context,
folder *object.Folder,
pool *object.ResourcePool,
host *object.HostSystem,
configSpec vimtypes.VirtualMachineConfigSpec,
datastoreRef vimtypes.ManagedObjectReference,
disks []*vimtypes.VirtualDisk,
diskSpecs []*vimtypes.VirtualDeviceConfigSpec,
srcDiskPaths []string) (*vimtypes.ManagedObjectReference, error) {
logger := pkglog.FromContextOrDefault(ctx).WithName("fastDeployLinked")
// Linked clones do not fully support encryption, so remove the possible
// crypto information from the VM's disks.
for i := range diskSpecs {
ds := diskSpecs[i]
if ds.Backing != nil {
ds.Backing.Crypto = nil
}
}
// Ensure all the parent disks are added to the ExtraConfig as the special
// vmprov.keepDisks value to prevent the parent disk from being deleted with
// the VM when it is deleted and/or promoted.
srcDiskNames := make([]string, len(srcDiskPaths))
for i := range srcDiskPaths {
srcDiskNames[i] = path.Base(srcDiskPaths[i])
}
exConfigKeyVal := &vimtypes.OptionValue{
Key: pkgconst.VMProvKeepDisksExtraConfigKey,
Value: strings.Join(srcDiskNames, ","),
}
configSpec.ExtraConfig = append(configSpec.ExtraConfig, exConfigKeyVal)
logger.Info("Preserving linked parents on delete/promote via extraConfig",
"extraConfigKey", exConfigKeyVal.Key,
"extraConfigValue", exConfigKeyVal.Value)
for i := range disks {
fileBackingInfo := vimtypes.VirtualDeviceFileBackingInfo{
Datastore: &datastoreRef,
FileName: srcDiskPaths[i],
}
switch tBack := disks[i].Backing.(type) {
case *vimtypes.VirtualDiskFlatVer2BackingInfo:
// Point the disk to its parent.
tBack.Parent = &vimtypes.VirtualDiskFlatVer2BackingInfo{
VirtualDeviceFileBackingInfo: fileBackingInfo,
DiskMode: string(vimtypes.VirtualDiskModePersistent),
ThinProvisioned: ptr.To(true),
}
case *vimtypes.VirtualDiskSeSparseBackingInfo:
// Point the disk to its parent.
tBack.Parent = &vimtypes.VirtualDiskSeSparseBackingInfo{
VirtualDeviceFileBackingInfo: fileBackingInfo,
DiskMode: string(vimtypes.VirtualDiskModePersistent),
}
case *vimtypes.VirtualDiskSparseVer2BackingInfo:
// Point the disk to its parent.
tBack.Parent = &vimtypes.VirtualDiskSparseVer2BackingInfo{
VirtualDeviceFileBackingInfo: fileBackingInfo,
DiskMode: string(vimtypes.VirtualDiskModePersistent),
}
}
}
return fastDeployCreateVM(ctx, logger, folder, pool, host, configSpec)
}
func fastDeployDirect(
ctx context.Context,
datacenter *object.Datacenter,
folder *object.Folder,
pool *object.ResourcePool,
host *object.HostSystem,
configSpec vimtypes.VirtualMachineConfigSpec,
diskSpecs []*vimtypes.VirtualDeviceConfigSpec,
diskFormat vimtypes.DatastoreSectorFormat,
dstDiskPaths,
srcDiskPaths []string) (*vimtypes.ManagedObjectReference, error) {
logger := pkglog.FromContextOrDefault(ctx).WithName("fastDeployDirect")
// Copy each disk into the VM directory.
if err := fastDeployDirectCopyDisks(
ctx,
logger,
datacenter,
configSpec,
srcDiskPaths,
dstDiskPaths,
diskFormat); err != nil {
return nil, err
}
for i := range diskSpecs {
ds := diskSpecs[i]
// Set the file operation to an empty string since the disk already
// exists.
ds.FileOperation = ""
}
return fastDeployCreateVM(ctx, logger, folder, pool, host, configSpec)
}
func fastDeployCreateVM(
ctx context.Context,
logger logr.Logger,
folder *object.Folder,
pool *object.ResourcePool,
host *object.HostSystem,
configSpec vimtypes.VirtualMachineConfigSpec) (*vimtypes.ManagedObjectReference, error) {
logger.Info("Creating VM", "configSpec", vimtypes.ToString(configSpec))
createTask, err := folder.CreateVM(
ctx,
configSpec,
pool,
host)
if err != nil {
return nil, fmt.Errorf("failed to call create task: %w", err)
}
createTaskInfo, err := createTask.WaitForResult(ctx)
if err != nil {
return nil, fmt.Errorf("failed to wait for create task: %w", err)
}
vmRefVal, ok := createTaskInfo.Result.(vimtypes.ManagedObjectReference)
if !ok {
return nil, fmt.Errorf(
"failed to assert create task result is ref: %[1]T %+[1]v",
createTaskInfo.Result)
}
return &vmRefVal, nil
}
func fastDeployDirectCopyDisks(
ctx context.Context,
logger logr.Logger,
datacenter *object.Datacenter,
configSpec vimtypes.VirtualMachineConfigSpec,
srcDiskPaths,
dstDiskPaths []string,
diskFormat vimtypes.DatastoreSectorFormat) error {
var (
wg sync.WaitGroup
copyDiskTasks = make([]*object.Task, len(srcDiskPaths))
copyDiskErrs = make(chan error, len(srcDiskPaths))
copyDiskSpec = vimtypes.FileBackedVirtualDiskSpec{
VirtualDiskSpec: vimtypes.VirtualDiskSpec{
AdapterType: string(vimtypes.VirtualDiskAdapterTypeLsiLogic),
DiskType: string(vimtypes.VirtualDiskTypeThin),
},
SectorFormat: string(diskFormat),
Profile: configSpec.VmProfile,
Crypto: configSpec.Crypto,
}
diskManager = object.NewVirtualDiskManager(datacenter.Client())
)
for i := range srcDiskPaths {
s := srcDiskPaths[i]
d := dstDiskPaths[i]
logger.Info(
"Copying disk",
"dstDiskPath", d,
"srcDiskPath", s,
"copyDiskSpec", copyDiskSpec)
t, err := diskManager.CopyVirtualDisk(
ctx,
s,
datacenter,
d,
datacenter,
©DiskSpec,
false)
if err != nil {
logger.Error(err, "failed to copy disk, cancelling other tasks")
// Cancel any other outstanding disk copies.
for _, t := range copyDiskTasks {
if t != nil {
// Cancel the task using a background context to ensure it
// goes through.
_ = t.Cancel(context.Background())
}
}
logger.Info("waiting on other copy tasks to complete")
// Wait on any other outstanding disk copies to complete before
// returning to ensure the parent folder can be cleaned up.
wg.Wait()
logger.Info("waited on other copy tasks to complete")
return fmt.Errorf("failed to call copy disk %q to %q: %w", s, d, err)
}
wg.Add(1)
copyDiskTasks[i] = t
go func() {
defer wg.Done()
if err := t.Wait(context.Background()); err != nil {
copyDiskErrs <- fmt.Errorf(
"failed to copy disk %q to %q: %w", s, d, err)
}
}()
}
// Wait on all the disk copies to complete before proceeding.
go func() {
wg.Wait()
close(copyDiskErrs)
}()
var copyDiskErr error
for err := range copyDiskErrs {
if err != nil {
if copyDiskErr == nil {
copyDiskErr = err
} else {
copyDiskErr = fmt.Errorf("%w,%w", copyDiskErr, err)
}
}
}
return copyDiskErr
}