From 33118b9f9c542219dbedc99cdc42f59c1961e662 Mon Sep 17 00:00:00 2001 From: Jonathon Jongsma Date: Mon, 14 Oct 2024 15:51:45 -0500 Subject: [PATCH] ova: improve handling of extra devices When encountering an ova file, we iterate throught the devices listed in the of the file and attempt to identify items that are useful to us (notably Network Adapters, memory definition, etc). The remaining items are stored as a list of generic devices with a `Kind` that is a free-form string. It appears that we tried to make the generic devices slightly nicer by stripping off number suffixes from the names of the devices and setting the device `Kind` to e.g. "SCSI Controller" rather than "SCSI Controller 1". But the code assumes that *all* `Items` in this file have the same numeric suffix format for `ElementName`. In reality, the `Items` vary quite a bit. This variation occurs both between different `Items` in the same file, and especially between ova files that are generated by different sources. For example, in an ova file that was generated with vsphere 7.0, the `ElementName` for my video device is simply "Video Card" with no suffix, whereas the Hard Disk has an `ElementName` of "Hard Disk 1". So the ova provider transforms the video card into a generic device with Kind set to the truncated string "Video Ca". In addition, I encountered ova files that were produced by VirtualBox where many of the `Items` in this list did not even contain `ElementName` elements, but did have `Description`s. So to make the ova provider more robust to files that don't follow our current assumptions: - only trim the suffix if the `ElementName` string ends with a space and a digit - if the `ElementName` does not exist at all, use `Description` - If neither of these elements exist, set the `Kind` to "Unknown" Signed-off-by: Jonathon Jongsma --- .../ova-provider-server.go | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/cmd/ova-provider-server/ova-provider-server.go b/cmd/ova-provider-server/ova-provider-server.go index 3118b9e13..177cdfde8 100644 --- a/cmd/ova-provider-server/ova-provider-server.go +++ b/cmd/ova-provider-server/ova-provider-server.go @@ -15,6 +15,7 @@ import ( "path/filepath" "strconv" "strings" + "unicode" "github.com/konveyor/forklift-controller/pkg/lib/gob" @@ -466,11 +467,24 @@ func convertToVmStruct(envelope []Envelope, ovaPath []string) ([]VM, error) { newVM.MemoryUnits = item.AllocationUnits } else { - if len(item.ElementName) > 2 { - newVM.Devices = append(newVM.Devices, Device{ - Kind: item.ElementName[:len(item.ElementName)-2], + var itemKind string + if len(item.ElementName) > 0 { + // if the `ElementName` element has a name such as "Hard Disk 1", strip off the + // number suffix to try to get a more generic name for the device type + itemKind = strings.TrimRightFunc(item.ElementName, func(r rune) bool { + return unicode.IsDigit(r) || unicode.IsSpace(r) }) + } else { + // Some .ova files do not include an `ElementName` element for each device. Fall + // back to using the `Description` element + itemKind = item.Description } + if len(itemKind) == 0 { + itemKind = "Unknown" + } + newVM.Devices = append(newVM.Devices, Device{ + Kind: itemKind, + }) } }