Skip to content

Commit

Permalink
Change the string passing for rootdisk from disk number to diskname +…
Browse files Browse the repository at this point in the history
… partition

Signed-off-by: Bella Khizgiyaev <[email protected]>
  • Loading branch information
bkhizgiy committed Jun 17, 2024
1 parent 7c9c2ff commit e8d658f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 41 deletions.
1 change: 1 addition & 0 deletions pkg/controller/plan/adapter/vsphere/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ go_library(
"//vendor/github.com/vmware/govmomi/vim25/types",
"//vendor/k8s.io/api/core/v1:core",
"//vendor/k8s.io/apimachinery/pkg/api/resource",
"//vendor/k8s.io/utils/ptr",
"//vendor/kubevirt.io/api/core/v1:core",
"//vendor/kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client",
Expand Down
30 changes: 20 additions & 10 deletions pkg/controller/plan/adapter/vsphere/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"regexp"
"sort"
"strings"
"strconv"

"unicode"

api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
"github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/plan"
Expand All @@ -32,6 +31,7 @@ import (
"github.com/vmware/govmomi/vim25/types"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/ptr"
cnv "kubevirt.io/api/core/v1"
cdi "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -660,15 +660,10 @@ func (r *Builder) mapDisks(vm *model.VM, vmRef ref.Ref, persistentVolumeClaims [
var bootDisk int
for _, vmConf := range r.Migration.Status.VMs {
if vmConf.ID == vmRef.ID {
var err error
bootDisk, err = strconv.Atoi(vmConf.RootDisk)
if err != nil {
bootDisk = 1
}
bootDisk = getDeviceNumber(vmConf.RootDisk)
break

Check warning on line 664 in pkg/controller/plan/adapter/vsphere/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/adapter/vsphere/builder.go#L660-L664

Added lines #L660 - L664 were not covered by tests
}
}
bootOrder := func(order uint) *uint { return &order }

for i, disk := range disks {
pvc := pvcMap[r.baseVolume(disk.File)]
Expand All @@ -692,8 +687,8 @@ func (r *Builder) mapDisks(vm *model.VM, vmRef ref.Ref, persistentVolumeClaims [
},
}
if bootDisk == i+1 {
kubevirtDisk.BootOrder = bootOrder(1)
}
kubevirtDisk.BootOrder = ptr.To(uint(1))

Check warning on line 690 in pkg/controller/plan/adapter/vsphere/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/adapter/vsphere/builder.go#L689-L690

Added lines #L689 - L690 were not covered by tests
}
kVolumes = append(kVolumes, volume)
kDisks = append(kDisks, kubevirtDisk)
}
Expand Down Expand Up @@ -945,3 +940,18 @@ func (r *Builder) GetPopulatorTaskName(pvc *core.PersistentVolumeClaim) (taskNam
err = planbase.VolumePopulatorNotSupportedError
return
}

func getDeviceNumber(deviceString string) int {
if !strings.HasPrefix(deviceString, "/dev/sd") || len(deviceString) < 8 {
// In case we encounter an issue detecting the root disk order,
// we will return zero to avoid failing the migration due to boot orde
return 0

Check warning on line 948 in pkg/controller/plan/adapter/vsphere/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/adapter/vsphere/builder.go#L948

Added line #L948 was not covered by tests
}

for i := 7; i < len(deviceString); i++ {
if unicode.IsLetter(rune(deviceString[i])) {
return int(deviceString[i] - 'a' + 1)
}
}
return 0

Check warning on line 956 in pkg/controller/plan/adapter/vsphere/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/adapter/vsphere/builder.go#L956

Added line #L956 was not covered by tests
}
23 changes: 23 additions & 0 deletions pkg/controller/plan/adapter/vsphere/vsphere_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,26 @@ func TestVsphere(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "vSphere Suite")
}

func TestGetDeviceNumber(t *testing.T) {
tests := []struct {
input string
expected int
}{
{"/dev/sda", 1},
{"/dev/sdb", 2},
{"/dev/sdz", 26},
{"/dev/sda1", 1},
{"/dev/sda5", 1},
{"/dev/sdb2", 2},
{"/dev/sdza", 26},
{"/dev/sdzb", 26},
}

for _, test := range tests {
result := getDeviceNumber(test.input)
if result != test.expected {
t.Errorf("For input '%s', expected %d, but got %d", test.input, test.expected, result)
}
}
}
18 changes: 5 additions & 13 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1597,19 +1597,11 @@ func (r *KubeVirt) guestConversionPod(vm *plan.VMStatus, vmVolumes []cnv.Volume,
}

if vm.RootDisk != "" {
diskNum, errDisk := strconv.Atoi(vm.RootDisk)
if errDisk != nil {
return
}

if diskNum > 0 {

environment = append(environment,
core.EnvVar{
Name: "V2V_RootDisk",
Value: vm.RootDisk,
})
}
environment = append(environment,
core.EnvVar{
Name: "V2V_RootDisk",
Value: vm.RootDisk,
})

Check warning on line 1604 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1599-L1604

Added lines #L1599 - L1604 were not covered by tests
}
// pod annotations
annotations := map[string]string{}
Expand Down
9 changes: 0 additions & 9 deletions pkg/controller/provider/container/vsphere/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,6 @@ func (v *VmAdapter) Apply(u types.ObjectUpdate) {
// Update virtual disk devices.
func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
disks := []model.Disk{}
//var diskNum int32 = 1
for _, dev := range devArray.VirtualDevice {
switch dev.(type) {
case *types.VirtualDisk:
Expand All @@ -713,7 +712,6 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
File: backing.FileName,
Capacity: disk.CapacityInBytes,
Mode: backing.DiskMode,
//Number: diskNum,
}
if backing.Datastore != nil {
datastoreId, _ := sanitize(backing.Datastore.Value)
Expand All @@ -723,15 +721,13 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
}
}
disks = append(disks, md)
//diskNum++
case *types.VirtualDiskFlatVer2BackingInfo:
md := model.Disk{
Key: disk.Key,
File: backing.FileName,
Capacity: disk.CapacityInBytes,
Shared: backing.Sharing != "sharingNone",
Mode: backing.DiskMode,
//Number: diskNum,
}
if backing.Datastore != nil {
datastoreId, _ := sanitize(backing.Datastore.Value)
Expand All @@ -741,7 +737,6 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
}
}
disks = append(disks, md)
//diskNum++
case *types.VirtualDiskRawDiskMappingVer1BackingInfo:
md := model.Disk{
Key: disk.Key,
Expand All @@ -750,7 +745,6 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
Shared: backing.Sharing != "sharingNone",
Mode: backing.DiskMode,
RDM: true,
//Number: diskNum,
}
if backing.Datastore != nil {
datastoreId, _ := sanitize(backing.Datastore.Value)
Expand All @@ -760,18 +754,15 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
}
}
disks = append(disks, md)
//diskNum++
case *types.VirtualDiskRawDiskVer2BackingInfo:
md := model.Disk{
Key: disk.Key,
File: backing.DescriptorFileName,
Capacity: disk.CapacityInBytes,
Shared: backing.Sharing != "sharingNone",
RDM: true,
//Number: diskNum,
}
disks = append(disks, md)
//diskNum++
}
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/controller/provider/model/vsphere/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ type Disk struct {
Shared bool `json:"shared"`
RDM bool `json:"rdm"`
Mode string `json:"mode,omitempty"`
//Number int32 `json:"number,omitempty"`
}

// Virtual Device.
Expand Down
11 changes: 3 additions & 8 deletions virt-v2v/cold/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,11 @@ func buildCommand() []string {

fmt.Println("Preparing virt-v2v")

virtV2vArgs = append(virtV2vArgs, "--root")
if checkEnvVariablesSet("V2V_RootDisk") {
diskNum, err := strconv.Atoi(os.Getenv("V2V_RootDisk"))
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
rootDisk := fmt.Sprintf("/dev/sd%s", genName(diskNum))
virtV2vArgs = append(virtV2vArgs, "--root", rootDisk)
virtV2vArgs = append(virtV2vArgs, os.Getenv("V2V_RootDisk"))

Check warning on line 104 in virt-v2v/cold/entrypoint.go

View check run for this annotation

Codecov / codecov/patch

virt-v2v/cold/entrypoint.go#L104

Added line #L104 was not covered by tests
} else {
virtV2vArgs = append(virtV2vArgs, "--root", "first")
virtV2vArgs = append(virtV2vArgs, "first")
}

switch source {
Expand Down

0 comments on commit e8d658f

Please sign in to comment.