Skip to content

Commit

Permalink
Add GetVmFromConfigYaml
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Necas <[email protected]>
  • Loading branch information
mnecas committed Sep 19, 2024
1 parent 30c573b commit 11ebad9
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 176 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
kubevirt.io/containerized-data-importer-api v1.59.0
libvirt.org/libvirt-go-xml v7.4.0+incompatible
sigs.k8s.io/controller-runtime v0.16.3
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -115,7 +116,6 @@ require (
kubevirt.io/controller-lifecycle-operator-sdk/api v0.0.0-20220329064328-f3cc58c6ed90 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/gophercloud/gophercloud => github.com/kubev2v/gophercloud v0.0.0-20230629135522-9d701a75c760
1 change: 0 additions & 1 deletion pkg/controller/plan/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ go_library(
"//pkg/controller/base",
"//pkg/controller/plan/adapter",
"//pkg/controller/plan/adapter/base",
"//pkg/controller/plan/adapter/ova",
"//pkg/controller/plan/adapter/vsphere",
"//pkg/controller/plan/context",
"//pkg/controller/plan/handler",
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/plan/adapter/ova/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"builder.go",
"client.go",
"destinationclient.go",
"ovfparser.go",
"validator.go",
],
importpath = "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/ova",
Expand All @@ -26,6 +27,7 @@ go_library(
"//pkg/lib/error",
"//pkg/lib/inventory/web",
"//pkg/lib/itinerary",
"//pkg/lib/logging",
"//vendor/github.com/go-logr/logr",
"//vendor/k8s.io/api/core/v1:core",
"//vendor/k8s.io/apimachinery/pkg/api/resource",
Expand Down
13 changes: 6 additions & 7 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,18 +982,18 @@ func (r *KubeVirt) UpdateVmByConvertedConfig(vm *plan.VMStatus, pod *core.Pod, s
case api.Ova:
vmConf, err := io.ReadAll(resp.Body)
if err != nil {
return err
return liberr.Wrap(err)
}
if vm.Firmware, err = util.GetFirmwareFromYaml(vmConf); err != nil {
return err
return liberr.Wrap(err)
}
case api.VSphere:
inspectionXML, err := r.getInspectionXml(pod)
if err != nil {
return err
return liberr.Wrap(err)
}
if vm.OperatingSystem, err = inspectionparser.GetOperationSystemFromConfig(inspectionXML); err != nil {
return err
return liberr.Wrap(err)
}
r.Log.Info("Setting the vm OS ", vm.OperatingSystem, "vmId", vm.ID)
}
Expand Down Expand Up @@ -1800,9 +1800,8 @@ func (r *KubeVirt) guestConversionPod(vm *plan.VMStatus, vmVolumes []cnv.Volume,
InitContainers: initContainers,
Containers: []core.Container{
{
ImagePullPolicy: core.PullAlways,
Name: "virt-v2v",
Env: environment,
Name: "virt-v2v",
Env: environment,
EnvFrom: []core.EnvFromSource{
{
Prefix: "V2V_",
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/plan/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ go_library(
"//pkg/controller/provider/web/ovirt",
"//pkg/lib/logging",
"//pkg/settings",
"//vendor/gopkg.in/yaml.v2:yaml_v2",
"//vendor/k8s.io/api/core/v1:core",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta",
"//vendor/kubevirt.io/api/core/v1:core",
"//vendor/sigs.k8s.io/yaml",
],
)

go_test(
name = "util_test",
srcs = [
"kubevirtvmparser_test.go",
"util_suite_test.go",
"utils_test.go",
],
data = glob(["testdata/**"]),
embed = [":util"],
deps = [
"//vendor/github.com/onsi/ginkgo/v2:ginkgo",
Expand Down
91 changes: 17 additions & 74 deletions pkg/controller/plan/util/kubevirtvmparser.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package util

import (
"fmt"
"strings"
cnv "kubevirt.io/api/core/v1"
"sigs.k8s.io/yaml"

"github.com/konveyor/forklift-controller/pkg/lib/logging"
"gopkg.in/yaml.v2"
)

const (
Expand All @@ -16,42 +15,6 @@ const (
// Package logger.
var log = logging.WithName(Name)

// Map of osinfo ids to vmware guest ids.
var osV2VMap = map[string]string{
"centos6": "centos6_64Guest",
"centos7": "centos7_64Guest",
"centos8": "centos8_64Guest",
"centos9": "centos9_64Guest",
"rhel7": "rhel7_64Guest",
"rhel8": "rhel8_64Guest",
"rhel9": "rhel9_64Guest",
"rocky": "rockylinux_64Guest",
"sles10": "sles10_64Guest",
"sles11": "sles11_64Guest",
"sles12": "sles12_64Guest",
"sles15": "sles15_64Guest",
"sles16": "sles16_64Guest",
"opensuse": "opensuse64Guest",
"debian4": "debian4_64Guest",
"debian5": "debian5_64Guest",
"debian6": "debian6_64Guest",
"debian7": "debian7_64Guest",
"debian8": "debian8_64Guest",
"debian9": "debian9_64Guest",
"debian10": "debian10_64Guest",
"debian11": "debian11_64Guest",
"debian12": "debian12_64Guest",
"ubuntu": "ubuntu64Guest",
"fedora": "fedora64Guest",
"win7": "windows7Server64Guest",
"win8": "windows8Server64Guest",
"win10": "windows9Server64Guest",
"win11": "windows11_64Guest",
"win12": "windows12_64Guest",
"win2k19": "windows2019srv_64Guest",
"win2k22": "windows2022srvNext_64Guest",
}

type OS struct {
Firmware string `yaml:"firmware"`
}
Expand Down Expand Up @@ -91,45 +54,25 @@ func GetFirmwareFromYaml(yamlData []byte) (string, error) {
}

firmware := vm.Spec.Template.Spec.Domain.OS.Firmware
if firmware == "" {
log.Info("Firmware type was not detected")
if firmware != "" {
return firmware, nil
}

return firmware, nil
}

func GetOperationSystemFromYaml(yamlData []byte) (os string, err error) {
var vm VirtualMachine
if err = yaml.Unmarshal(yamlData, &vm); err != nil {
return
// FIXME: In newer version of virt-v2v the output will change to support CNV VM format.
// With this we will support both so the migrations should not fail during the update to newer virt-v2v.
// But we still need to remove the custom templating.
// https://issues.redhat.com/browse/RHEL-58065
var cnvVm *cnv.VirtualMachine
if err := yaml.Unmarshal(yamlData, &cnvVm); err != nil {
return "", err
}

labels := vm.Metadata.Labels
if osinfo, ok := labels["libguestfs.org/osinfo"]; ok {
return mapOs(osinfo), nil

if cnvVm.Spec.Template.Spec.Domain.Firmware.Bootloader.BIOS != nil {
return "bios", nil
}
return
}

func mapOs(labelOS string) (os string) {
distro := strings.SplitN(labelOS, ".", 2)[0]

switch {
case strings.HasPrefix(distro, "rocky"):
distro = "rocky"
case strings.HasPrefix(distro, "opensuse"):
distro = "opensuse"
case strings.HasPrefix(distro, "ubuntu"):
distro = "ubuntu"
case strings.HasPrefix(distro, "fedora"):
distro = "fedora"
if cnvVm.Spec.Template.Spec.Domain.Firmware.Bootloader.EFI != nil {
return "uefi", nil
}

os, ok := osV2VMap[distro]
if !ok {
log.Info(fmt.Sprintf("Received %s, mapped to: %s", labelOS, os))
os = "otherGuest64"
}
return
log.Info("Firmware type was not detected")
return "", nil
}
31 changes: 31 additions & 0 deletions pkg/controller/plan/util/kubevirtvmparser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package util

import (
"fmt"
"os"
"path/filepath"
"testing"
)

func TestKubevirtVmParser(t *testing.T) {
fmt.Println("test")
testFile(t, "new_format_bios.yml", "bios")
testFile(t, "new_format_efi.yml", "uefi")
testFile(t, "old_format_bios.yml", "bios")
testFile(t, "old_format_efi.yml", "uefi")
}

func testFile(t *testing.T, filename, expectedFormat string) {
data, err := os.ReadFile(filepath.Join("testdata", filename))
if err != nil {
fmt.Println(err)
}
firmware, err := GetFirmwareFromYaml(data)
if err != nil {
fmt.Println(err)
}
fmt.Println(firmware)
if firmware != expectedFormat {
t.Fatalf("Failed to parse '%s' from file '%s'", expectedFormat, filename)
}
}
38 changes: 38 additions & 0 deletions pkg/controller/plan/util/testdata/new_format_bios.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by virt-v2v 2.5.8fedora=40,release=1.fc40
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: mnecas-win-2022
labels:
libguestfs.org/virt-v2v-version: "2.5.8"
libguestfs.org/genid: f2fc75e8-4961-1425-4740-fcf2bc09685a
libguestfs.org/osinfo: win2k22
libguestfs.org/source: vmware
spec:
template:
spec:
domain:
firmware:
bootloader:
bios: {}
resources:
clock:
timer:
hpet:
present: false
hyperv: {}
pit:
tickPolicy: delay
rtc:
tickPolicy: catchup
utc: {}
requests:
memory: 8192Mi
features:
cpu:
sockets: 2
cores: 2
thread: 1
terminationGracePeriodSeconds: 0

39 changes: 39 additions & 0 deletions pkg/controller/plan/util/testdata/new_format_efi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
# generated by virt-v2v 2.5.8fedora=40,release=1.fc40
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: mnecas-win-2022
labels:
libguestfs.org/virt-v2v-version: "2.5.8"
libguestfs.org/genid: f2fc75e8-4961-1425-4740-fcf2bc09685a
libguestfs.org/osinfo: win2k22
libguestfs.org/source: vmware
spec:
template:
spec:
domain:
firmware:
bootloader:
efi:
persistent: true
resources:
clock:
timer:
hpet:
present: false
hyperv: {}
pit:
tickPolicy: delay
rtc:
tickPolicy: catchup
utc: {}
requests:
memory: 8192Mi
features:
cpu:
sockets: 2
cores: 2
thread: 1
terminationGracePeriodSeconds: 0

38 changes: 38 additions & 0 deletions pkg/controller/plan/util/testdata/old_format_bios.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by virt-v2v 2.5.8fedora=40,release=1.fc40
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: mnecas-win-2022
labels:
libguestfs.org/virt-v2v-version: "2.5.8"
libguestfs.org/genid: 4f3c8a7c-3464-c9b0-cf79-08325a6bf1e8
libguestfs.org/osinfo: win2k22
libguestfs.org/source: kvm
spec:
template:
spec:
domain:
os:
firmware: bios
resources:
clock:
timer:
hpet:
present: false
hyperv: {}
pit:
tickPolicy: delay
rtc:
tickPolicy: catchup
utc: {}
requests:
memory: 8192Mi
features:
acpi: {}
apic: {}
cpu:
sockets: 2
cores: 2
thread: 1
terminationGracePeriodSeconds: 0
Loading

0 comments on commit 11ebad9

Please sign in to comment.