Skip to content

Commit 95770f8

Browse files
committed
Add DefaultDiskBusType field to VirtualMachineImportSpec
This allows the user to customize the disk bus type in case the auto-detection fails. Note, the OpenStack source client does not support auto-detection, thus it will always make use of the `DefaultDiskBusType` field if specified. The `DefaultDiskBusType` defaults to `virtio`. Signed-off-by: Volker Theile <[email protected]>
1 parent 9167828 commit 95770f8

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

pkg/apis/migration.harvesterhci.io/v1beta1/virtualmachines.go

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/rancher/wrangler/pkg/condition"
55
corev1 "k8s.io/api/core/v1"
66
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/utils/ptr"
78
kubevirtv1 "kubevirt.io/api/core/v1"
89

910
"github.com/harvester/vm-import-controller/pkg/apis/common"
@@ -33,6 +34,12 @@ type VirtualMachineImportSpec struct {
3334
Folder string `json:"folder,omitempty"`
3435
Mapping []NetworkMapping `json:"networkMapping,omitempty"` //If empty new VirtualMachineImport will be mapped to Management Network
3536
StorageClass string `json:"storageClass,omitempty"`
37+
38+
// The bus type that is used for imported disks if auto-detection fails.
39+
// Note, the OpenStack source client does not support auto-detection,
40+
// therefore it always makes use of this field if specified.
41+
// Defaults to "virtio".
42+
DefaultDiskBusType *kubevirtv1.DiskBus `json:"defaultDiskBusType,omitempty"`
3643
}
3744

3845
// VirtualMachineImportStatus tracks the status of the VirtualMachineImport export from migration and import into the Harvester cluster
@@ -93,3 +100,7 @@ const (
93100
VirtualMachineExportFailed condition.Cond = "VMExportFailed"
94101
VirtualMachineMigrationFailed ImportStatus = "VMMigrationFailed"
95102
)
103+
104+
func (in *VirtualMachineImport) GetDefaultDiskBusType() kubevirtv1.DiskBus {
105+
return ptr.Deref[kubevirtv1.DiskBus](in.Spec.DefaultDiskBusType, kubevirtv1.DiskBusVirtio)
106+
}

pkg/source/openstack/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func (c *Client) ExportVirtualMachine(vm *migration.VirtualMachineImport) error
373373
Name: rawImageFileName,
374374
DiskSize: int64(volObj.Size),
375375
DiskLocalPath: server.TempDir(),
376-
BusType: kubevirt.DiskBusVirtio,
376+
BusType: vm.GetDefaultDiskBusType(),
377377
})
378378
}
379379

pkg/source/vmware/client.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (c *Client) ExportVirtualMachine(vm *migration.VirtualMachineImport) (err e
187187
i.Path = vm.Name + "-" + vm.Namespace + "-" + i.Path
188188
}
189189

190-
busType := detectBusType(i.DeviceId)
190+
busType := detectDiskBusType(i.DeviceId, vm.GetDefaultDiskBusType())
191191

192192
logrus.WithFields(logrus.Fields{
193193
"name": vm.Name,
@@ -504,8 +504,9 @@ func mapNetworkCards(networkCards []networkInfo, mapping []migration.NetworkMapp
504504
return retNetwork
505505
}
506506

507-
// detectBusType tries to identify the disk bus type from VMware to attempt and
508-
// set correct bus types in KubeVirt.
507+
// detectDiskBusType tries to identify the disk bus type from VMware to attempt and
508+
// set correct bus types in KubeVirt. Defaults to the specified bus type in `def`
509+
// if auto-detection fails.
509510
// Examples:
510511
// .--------------------------------------------------.
511512
// | Bus | Device ID |
@@ -524,7 +525,7 @@ func mapNetworkCards(networkCards []networkInfo, mapping []migration.NetworkMapp
524525
// - https://vdc-download.vmware.com/vmwb-repository/dcr-public/d1902b0e-d479-46bf-8ac9-cee0e31e8ec0/07ce8dbd-db48-4261-9b8f-c6d3ad8ba472/vim.vm.device.VirtualSCSIController.html
525526
// - https://libvirt.org/formatdomain.html#controllers
526527
// - https://kubevirt.io/api-reference/v1.1.0/definitions.html#_v1_disktarget
527-
func detectBusType(deviceID string) kubevirt.DiskBus {
528+
func detectDiskBusType(deviceID string, def kubevirt.DiskBus) kubevirt.DiskBus {
528529
deviceID = strings.ToLower(deviceID)
529530
switch {
530531
case strings.Contains(deviceID, "paravirtualscsi"):
@@ -546,7 +547,7 @@ func detectBusType(deviceID string) kubevirt.DiskBus {
546547
case strings.Contains(deviceID, "usb"):
547548
return kubevirt.DiskBusUSB
548549
default:
549-
return kubevirt.DiskBusVirtio
550+
return def
550551
}
551552
}
552553

pkg/source/vmware/client_test.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -306,62 +306,78 @@ func Test_identifyNetworkCards(t *testing.T) {
306306
assert.Len(noMappedInfo, 0, "expected to find no item in the mapped networkinfo")
307307
}
308308

309-
func Test_adapterType(t *testing.T) {
309+
func Test_detectDiskBusType(t *testing.T) {
310310
assert := require.New(t)
311311
testCases := []struct {
312312
desc string
313313
deviceID string
314+
def kubevirt.DiskBus
314315
expected kubevirt.DiskBus
315316
}{
316317
{
317318
desc: "SCSI disk - VMware Paravirtual",
318319
deviceID: "/vm-13010/ParaVirtualSCSIController0:0",
320+
def: kubevirt.DiskBusVirtio,
319321
expected: kubevirt.DiskBusSATA,
320322
},
321323
{
322324
desc: "SCSI disk - BusLogic Parallel",
323325
deviceID: "/vm-13011/VirtualBusLogicController0:0",
326+
def: kubevirt.DiskBusVirtio,
324327
expected: kubevirt.DiskBusSCSI,
325328
},
326329
{
327330
desc: "SCSI disk - LSI Logic Parallel",
328331
deviceID: "/vm-13012/VirtualLsiLogicController0:0",
332+
def: kubevirt.DiskBusVirtio,
329333
expected: kubevirt.DiskBusSCSI,
330334
},
331335
{
332336
desc: "SCSI disk - LSI Logic SAS",
333337
deviceID: "/vm-13013/VirtualLsiLogicSASController0:0",
338+
def: kubevirt.DiskBusVirtio,
334339
expected: kubevirt.DiskBusSCSI,
335340
},
336341
{
337342
desc: "NVMe disk",
338343
deviceID: "/vm-2468/VirtualNVMEController0:0",
344+
def: kubevirt.DiskBusVirtio,
339345
expected: kubevirt.DiskBusVirtio,
340346
},
341347
{
342348
desc: "USB disk",
343349
deviceID: "/vm-54321/VirtualUSBController0:0",
350+
def: kubevirt.DiskBusVirtio,
344351
expected: kubevirt.DiskBusUSB,
345352
},
346353
{
347354
desc: "SATA disk",
348355
deviceID: "/vm-13767/VirtualAHCIController0:1",
356+
def: kubevirt.DiskBusVirtio,
349357
expected: kubevirt.DiskBusSATA,
350358
},
351359
{
352360
desc: "IDE disk",
353361
deviceID: "/vm-5678/VirtualIDEController1:0",
362+
def: kubevirt.DiskBusVirtio,
354363
expected: kubevirt.DiskBusSATA,
355364
},
356365
{
357-
desc: "Unknown disk",
366+
desc: "Unknown disk 1",
358367
deviceID: "foo",
368+
def: kubevirt.DiskBusVirtio,
359369
expected: kubevirt.DiskBusVirtio,
360370
},
371+
{
372+
desc: "Unknown disk 2",
373+
deviceID: "bar",
374+
def: kubevirt.DiskBusSATA,
375+
expected: kubevirt.DiskBusSATA,
376+
},
361377
}
362378

363379
for _, tc := range testCases {
364-
busType := detectBusType(tc.deviceID)
380+
busType := detectDiskBusType(tc.deviceID, tc.def)
365381
assert.Equal(tc.expected, busType)
366382
}
367383
}

0 commit comments

Comments
 (0)