Skip to content

Commit 74528cf

Browse files
committed
Add option to GetVirtualDiskInfoByUUID for choosing exclude snapshot disks when calculating virtualdisk size
Signed-off-by: Lubron Zhan <[email protected]>
1 parent 341c9d8 commit 74528cf

File tree

2 files changed

+167
-15
lines changed

2 files changed

+167
-15
lines changed

vmdk/disk_info.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ type VirtualDiskInfo struct {
3939
//
4040
// These are the only backing types that have a UUID property for comparing the
4141
// provided value.
42+
//
43+
// excludeSnapshots is used to determine if the delta disks for snapshot should
44+
// be excluded when calculating the disk size. If true, only the file keys from
45+
// the last chain, which is last delta disk, will be used. If false, all the
46+
// file keys from all the chains will be included.
4247
func GetVirtualDiskInfoByUUID(
4348
ctx context.Context,
4449
client *vim25.Client,
4550
mo mo.VirtualMachine,
4651
fetchProperties bool,
47-
diskUUID string) (VirtualDiskInfo, error) {
52+
excludeSnapshots bool,
53+
diskUUID string,
54+
) (VirtualDiskInfo, error) {
4855

4956
if diskUUID == "" {
5057
return VirtualDiskInfo{}, fmt.Errorf("diskUUID is empty")
@@ -129,11 +136,21 @@ func GetVirtualDiskInfoByUUID(
129136
// Build a lookup table for determining if file key belongs to this disk
130137
// chain.
131138
diskFileKeys := map[int32]struct{}{}
132-
for i := range mo.LayoutEx.Disk {
133-
if d := mo.LayoutEx.Disk[i]; d.Key == disk.Key {
134-
for j := range d.Chain {
135-
for k := range d.Chain[j].FileKey {
136-
diskFileKeys[d.Chain[j].FileKey[k]] = struct{}{}
139+
for _, d := range mo.LayoutEx.Disk {
140+
if d.Key != disk.Key || len(d.Chain) == 0 {
141+
continue
142+
}
143+
144+
// Take the file keys from the child most delta disk chain.
145+
for _, fileKey := range d.Chain[len(d.Chain)-1].FileKey {
146+
diskFileKeys[fileKey] = struct{}{}
147+
}
148+
if !excludeSnapshots {
149+
// Take all the file keys from previous delta disk chains.
150+
// These are for the snapshots.
151+
for _, chain := range d.Chain[:len(d.Chain)-1] {
152+
for _, fileKey := range chain.FileKey {
153+
diskFileKeys[fileKey] = struct{}{}
137154
}
138155
}
139156
}

vmdk/disk_info_test.go

Lines changed: 144 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ import (
2828
func TestGetVirtualDiskInfoByUUID(t *testing.T) {
2929

3030
type testCase struct {
31-
name string
32-
ctx context.Context
33-
client *vim25.Client
34-
mo mo.VirtualMachine
35-
fetchProperties bool
36-
diskUUID string
37-
diskInfo vmdk.VirtualDiskInfo
38-
err string
31+
name string
32+
ctx context.Context
33+
client *vim25.Client
34+
mo mo.VirtualMachine
35+
fetchProperties bool
36+
diskUUID string
37+
diskInfo vmdk.VirtualDiskInfo
38+
excludeSnapshots bool
39+
err string
3940
}
4041

4142
t.Run("w cached properties", func(t *testing.T) {
@@ -386,18 +387,146 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
386387
UniqueSize: (5 * 1024 * 1024) + 100 + 100 + 200 + 300,
387388
},
388389
},
390+
{
391+
name: "one disk w/o chain entries, it should return 0 for size and unique size",
392+
mo: mo.VirtualMachine{
393+
Config: &types.VirtualMachineConfigInfo{
394+
Hardware: types.VirtualHardware{
395+
Device: []types.BaseVirtualDevice{
396+
getDisk(&types.VirtualDiskFlatVer2BackingInfo{
397+
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
398+
FileName: fileName,
399+
},
400+
Uuid: diskUUID,
401+
}),
402+
},
403+
},
404+
},
405+
LayoutEx: &types.VirtualMachineFileLayoutEx{
406+
Disk: []types.VirtualMachineFileLayoutExDiskLayout{
407+
{
408+
Key: deviceKey,
409+
Chain: []types.VirtualMachineFileLayoutExDiskUnit{},
410+
},
411+
},
412+
File: []types.VirtualMachineFileLayoutExFileInfo{
413+
{
414+
Key: 1,
415+
Size: 100,
416+
UniqueSize: 100,
417+
},
418+
},
419+
},
420+
},
421+
diskUUID: diskUUID,
422+
diskInfo: vmdk.VirtualDiskInfo{
423+
CapacityInBytes: tenGiBInBytes,
424+
DeviceKey: deviceKey,
425+
FileName: fileName,
426+
Size: 0,
427+
UniqueSize: 0,
428+
},
429+
},
430+
{
431+
name: "one disk w multiple chain entries and includeSnapshots is false, it should exclude the snapshot delta disks, only the size offile keys (8, 9) should be included",
432+
mo: mo.VirtualMachine{
433+
Config: &types.VirtualMachineConfigInfo{
434+
Hardware: types.VirtualHardware{
435+
Device: []types.BaseVirtualDevice{
436+
getDisk(&types.VirtualDiskFlatVer2BackingInfo{
437+
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
438+
FileName: fileName,
439+
},
440+
Uuid: diskUUID,
441+
}),
442+
},
443+
},
444+
},
445+
LayoutEx: &types.VirtualMachineFileLayoutEx{
446+
Disk: []types.VirtualMachineFileLayoutExDiskLayout{
447+
{
448+
Key: deviceKey,
449+
Chain: []types.VirtualMachineFileLayoutExDiskUnit{
450+
{
451+
FileKey: []int32{
452+
4,
453+
5,
454+
},
455+
},
456+
{
457+
FileKey: []int32{
458+
6,
459+
7,
460+
},
461+
},
462+
{
463+
FileKey: []int32{
464+
8,
465+
9,
466+
},
467+
},
468+
},
469+
},
470+
},
471+
File: []types.VirtualMachineFileLayoutExFileInfo{
472+
{
473+
Key: 4,
474+
Size: 1 * 1024 * 1024 * 1024, // 1 GiB
475+
UniqueSize: 5 * 1024 * 1024, // 500 MiB
476+
},
477+
{
478+
Key: 5,
479+
Size: 950,
480+
UniqueSize: 100,
481+
},
482+
{
483+
Key: 6,
484+
Size: 500,
485+
UniqueSize: 100,
486+
},
487+
{
488+
Key: 7,
489+
Size: 500,
490+
UniqueSize: 200,
491+
},
492+
{
493+
Key: 8,
494+
Size: 1000,
495+
UniqueSize: 300,
496+
},
497+
{
498+
Key: 9,
499+
Size: 1000,
500+
UniqueSize: 300,
501+
},
502+
},
503+
},
504+
},
505+
excludeSnapshots: true,
506+
diskUUID: diskUUID,
507+
diskInfo: vmdk.VirtualDiskInfo{
508+
CapacityInBytes: tenGiBInBytes,
509+
DeviceKey: deviceKey,
510+
FileName: fileName,
511+
Size: 1000 + 1000,
512+
UniqueSize: 300 + 300,
513+
},
514+
},
389515
}
390516

391517
for i := range testCases {
392518
tc := testCases[i]
393519
t.Run(tc.name, func(t *testing.T) {
394520
var ctx context.Context
395521
dii, err := vmdk.GetVirtualDiskInfoByUUID(
396-
ctx, nil, tc.mo, false, tc.diskUUID)
522+
ctx, nil, tc.mo, false, tc.excludeSnapshots, tc.diskUUID)
397523

398524
if tc.err != "" {
399525
assert.EqualError(t, err, tc.err)
526+
} else {
527+
assert.NoError(t, err)
400528
}
529+
401530
assert.Equal(t, tc.diskInfo, dii)
402531
})
403532
}
@@ -416,6 +545,9 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
416545
}
417546
finder.SetDatacenter(datacenter)
418547
vmList, err := finder.VirtualMachineList(ctx, "*")
548+
if err != nil {
549+
t.Fatalf("failed to get vm list: %s", err)
550+
}
419551
if len(vmList) == 0 {
420552
t.Fatal("vmList == 0")
421553
}
@@ -574,10 +706,13 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
574706
tc.client,
575707
tc.mo,
576708
tc.fetchProperties,
709+
tc.excludeSnapshots,
577710
tc.diskUUID)
578711

579712
if tc.err != "" {
580713
assert.EqualError(t, err, tc.err)
714+
} else {
715+
assert.NoError(t, err)
581716
}
582717
assert.Equal(t, tc.diskInfo, dii)
583718
})

0 commit comments

Comments
 (0)