Skip to content

Commit

Permalink
Refactor code in hypervisor.go
Browse files Browse the repository at this point in the history
Signed-off-by: Pramodh Pallapothu <[email protected]>
  • Loading branch information
Pramodh Pallapothu authored and eriknordmark committed Apr 29, 2024
1 parent 4613f7f commit 397dce6
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions pkg/pillar/hypervisor/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/lf-edge/eve/pkg/pillar/base"
"github.com/lf-edge/eve/pkg/pillar/types"
fileutils "github.com/lf-edge/eve/pkg/pillar/utils/file"
uuid "github.com/satori/go.uuid"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
Expand Down Expand Up @@ -38,17 +39,17 @@ type Hypervisor interface {

type hypervisorDesc struct {
constructor func() Hypervisor
dom0handle string
enabled func() bool
hvTypeFileContent string
}

var knownHypervisors = map[string]hypervisorDesc{
XenHypervisorName: {constructor: newXen, dom0handle: "/proc/xen", hvTypeFileContent: "xen"},
KVMHypervisorName: {constructor: newKvm, dom0handle: "/dev/kvm", hvTypeFileContent: "kvm"},
KubevirtHypervisorName: {constructor: newKubevirt, dom0handle: "/dev/kvm", hvTypeFileContent: "kubevirt"},
ACRNHypervisorName: {constructor: newAcrn, dom0handle: "/dev/acrn", hvTypeFileContent: "acrn"},
ContainerdHypervisorName: {constructor: newContainerd, dom0handle: "/run/containerd/containerd.sock"},
NullHypervisorName: {constructor: newNull, dom0handle: "/"},
XenHypervisorName: {constructor: newXen, enabled: func() bool { return fileutils.FileExists(nil, "/proc/xen") }, hvTypeFileContent: "xen"},
KVMHypervisorName: {constructor: newKvm, enabled: func() bool { return fileutils.FileExists(nil, "/dev/kvm") && !base.IsHVTypeKube() }, hvTypeFileContent: "kvm"},
KubevirtHypervisorName: {constructor: newKubevirt, enabled: func() bool { return fileutils.FileExists(nil, "/dev/kvm") && base.IsHVTypeKube() }, hvTypeFileContent: "kubevirt"},
ACRNHypervisorName: {constructor: newAcrn, enabled: func() bool { return fileutils.FileExists(nil, "/dev/acrn") }, hvTypeFileContent: "acrn"},
ContainerdHypervisorName: {constructor: newContainerd, enabled: func() bool { return fileutils.FileExists(nil, "/run/containerd/containerd.sock") }},
NullHypervisorName: {constructor: newNull, enabled: func() bool { return fileutils.DirExists(nil, "/") }},
}

// this is a priority order to pick a default hypervisor if multiple are available (more to less likely)
Expand Down Expand Up @@ -96,18 +97,9 @@ func BootTimeHypervisor() Hypervisor {
// the advice of this function and always ask for the enabled one.
func GetAvailableHypervisors() (all []string, enabled []string) {
all = hypervisorPriority
isHVTypeKube := base.IsHVTypeKube()
for _, v := range all {
if _, err := os.Stat(knownHypervisors[v].dom0handle); err == nil {
// Both Kubevirt and KVM use same dom0handle.
// Lets differentiate by eve_flavor
if isHVTypeKube && strings.Compare(v, KVMHypervisorName) == 0 {
continue // kubevirt image don't set kvm
} else if !isHVTypeKube && strings.Compare(v, KubevirtHypervisorName) == 0 {
continue // kvm image don't set kubevirt
} else {
enabled = append(enabled, v)
}
if knownHypervisors[v].enabled() {
enabled = append(enabled, v)
}
}
return
Expand Down

0 comments on commit 397dce6

Please sign in to comment.