Skip to content

Commit 397dce6

Browse files
Pramodh Pallapothueriknordmark
authored andcommitted
Refactor code in hypervisor.go
Signed-off-by: Pramodh Pallapothu <[email protected]>
1 parent 4613f7f commit 397dce6

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

pkg/pillar/hypervisor/hypervisor.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/lf-edge/eve/pkg/pillar/base"
1313
"github.com/lf-edge/eve/pkg/pillar/types"
14+
fileutils "github.com/lf-edge/eve/pkg/pillar/utils/file"
1415
uuid "github.com/satori/go.uuid"
1516
"github.com/shirou/gopsutil/cpu"
1617
"github.com/shirou/gopsutil/mem"
@@ -38,17 +39,17 @@ type Hypervisor interface {
3839

3940
type hypervisorDesc struct {
4041
constructor func() Hypervisor
41-
dom0handle string
42+
enabled func() bool
4243
hvTypeFileContent string
4344
}
4445

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

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

0 commit comments

Comments
 (0)