Skip to content

Commit

Permalink
Merge pull request #18977 from jakecorrenti/move-qemu-functions-to-pr…
Browse files Browse the repository at this point in the history
…oper-files

Re-organize hypervisor implementations
  • Loading branch information
openshift-merge-robot authored Jun 27, 2023
2 parents 089dbed + 5160342 commit 4dc2e08
Show file tree
Hide file tree
Showing 13 changed files with 627 additions and 643 deletions.
2 changes: 1 addition & 1 deletion cmd/podman/machine/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func GetSystemProvider() (machine.VirtProvider, error) {
logrus.Debugf("Using Podman machine with `%s` virtualization provider", resolvedVMType.String())
switch resolvedVMType {
case machine.QemuVirt:
return qemu.GetVirtualizationProvider(), nil
return qemu.VirtualizationProvider(), nil
default:
return nil, fmt.Errorf("unsupported virtualization provider: `%s`", resolvedVMType.String())
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/machine/platform_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func GetSystemProvider() (machine.VirtProvider, error) {
logrus.Debugf("Using Podman machine with `%s` virtualization provider", resolvedVMType.String())
switch resolvedVMType {
case machine.QemuVirt:
return qemu.GetVirtualizationProvider(), nil
return qemu.VirtualizationProvider(), nil
case machine.AppleHvVirt:
return applehv.GetVirtualizationProvider(), nil
return applehv.VirtualizationProvider(), nil
default:
return nil, fmt.Errorf("unsupported virtualization provider: `%s`", resolvedVMType.String())
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/machine/platform_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func GetSystemProvider() (machine.VirtProvider, error) {
logrus.Debugf("Using Podman machine with `%s` virtualization provider", resolvedVMType.String())
switch resolvedVMType {
case machine.WSLVirt:
return wsl.GetWSLProvider(), nil
return wsl.VirtualizationProvider(), nil
case machine.HyperVVirt:
return hyperv.GetVirtualizationProvider(), nil
return hyperv.VirtualizationProvider(), nil
default:
return nil, fmt.Errorf("unsupported virtualization provider: `%s`", resolvedVMType.String())
}
Expand Down
36 changes: 14 additions & 22 deletions pkg/machine/applehv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ const (
defaultVFKitEndpoint = "http://localhost:8081"
)

type Virtualization struct {
artifact machine.Artifact
compression machine.ImageCompression
format machine.ImageFormat
type AppleHVVirtualization struct {
machine.Virtualization
}

type MMHardwareConfig struct {
Expand All @@ -30,11 +28,13 @@ type MMHardwareConfig struct {
Memory int32
}

func (v Virtualization) Artifact() machine.Artifact {
return machine.Metal
func VirtualizationProvider() machine.VirtProvider {
return &AppleHVVirtualization{
machine.NewVirtualization(machine.Metal, machine.Xz, machine.Raw),
}
}

func (v Virtualization) CheckExclusiveActiveVM() (bool, string, error) {
func (v AppleHVVirtualization) CheckExclusiveActiveVM() (bool, string, error) {
fsVms, err := getVMInfos()
if err != nil {
return false, "", err
Expand All @@ -48,15 +48,7 @@ func (v Virtualization) CheckExclusiveActiveVM() (bool, string, error) {
return false, "", nil
}

func (v Virtualization) Compression() machine.ImageCompression {
return v.compression
}

func (v Virtualization) Format() machine.ImageFormat {
return v.format
}

func (v Virtualization) IsValidVMName(name string) (bool, error) {
func (v AppleHVVirtualization) IsValidVMName(name string) (bool, error) {
mm := MacMachine{Name: name}
configDir, err := machine.GetConfDir(machine.AppleHvVirt)
if err != nil {
Expand All @@ -68,7 +60,7 @@ func (v Virtualization) IsValidVMName(name string) (bool, error) {
return true, nil
}

func (v Virtualization) List(opts machine.ListOptions) ([]*machine.ListResponse, error) {
func (v AppleHVVirtualization) List(opts machine.ListOptions) ([]*machine.ListResponse, error) {
var (
response []*machine.ListResponse
)
Expand Down Expand Up @@ -108,12 +100,12 @@ func (v Virtualization) List(opts machine.ListOptions) ([]*machine.ListResponse,
return response, nil
}

func (v Virtualization) LoadVMByName(name string) (machine.VM, error) {
func (v AppleHVVirtualization) LoadVMByName(name string) (machine.VM, error) {
m := MacMachine{Name: name}
return m.loadFromFile()
}

func (v Virtualization) NewMachine(opts machine.InitOptions) (machine.VM, error) {
func (v AppleHVVirtualization) NewMachine(opts machine.InitOptions) (machine.VM, error) {
m := MacMachine{Name: opts.Name}

configDir, err := machine.GetConfDir(machine.AppleHvVirt)
Expand Down Expand Up @@ -149,16 +141,16 @@ func (v Virtualization) NewMachine(opts machine.InitOptions) (machine.VM, error)
return m.loadFromFile()
}

func (v Virtualization) RemoveAndCleanMachines() error {
func (v AppleHVVirtualization) RemoveAndCleanMachines() error {
// This can be implemented when host networking is completed.
return machine.ErrNotImplemented
}

func (v Virtualization) VMType() machine.VMType {
func (v AppleHVVirtualization) VMType() machine.VMType {
return vmtype
}

func (v Virtualization) loadFromLocalJson() ([]*MacMachine, error) {
func (v AppleHVVirtualization) loadFromLocalJson() ([]*MacMachine, error) {
var (
jsonFiles []string
mms []*MacMachine
Expand Down
8 changes: 0 additions & 8 deletions pkg/machine/applehv/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ var (
vmtype = machine.AppleHvVirt
)

func GetVirtualizationProvider() machine.VirtProvider {
return &Virtualization{
artifact: machine.None,
compression: machine.Xz,
format: machine.Raw,
}
}

// VfkitHelper describes the use of vfkit: cmdline and endpoint
type VfkitHelper struct {
Bootloader string
Expand Down
52 changes: 39 additions & 13 deletions pkg/machine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,6 @@ const (
DefaultMachineName string = "podman-machine-default"
)

type VirtProvider interface {
Artifact() Artifact
CheckExclusiveActiveVM() (bool, string, error)
Compression() ImageCompression
Format() ImageFormat
IsValidVMName(name string) (bool, error)
List(opts ListOptions) ([]*ListResponse, error)
LoadVMByName(name string) (VM, error)
NewMachine(opts InitOptions) (VM, error)
RemoveAndCleanMachines() error
VMType() VMType
}

type RemoteConnectionType string

var (
Expand Down Expand Up @@ -428,3 +415,42 @@ func ParseVMType(input string, emptyFallback VMType) (VMType, error) {
return QemuVirt, fmt.Errorf("unknown VMType `%s`", input)
}
}

type VirtProvider interface {
Artifact() Artifact
CheckExclusiveActiveVM() (bool, string, error)
Compression() ImageCompression
Format() ImageFormat
IsValidVMName(name string) (bool, error)
List(opts ListOptions) ([]*ListResponse, error)
LoadVMByName(name string) (VM, error)
NewMachine(opts InitOptions) (VM, error)
RemoveAndCleanMachines() error
VMType() VMType
}

type Virtualization struct {
artifact Artifact
compression ImageCompression
format ImageFormat
}

func (p *Virtualization) Artifact() Artifact {
return p.artifact
}

func (p *Virtualization) Compression() ImageCompression {
return p.compression
}

func (p *Virtualization) Format() ImageFormat {
return p.format
}

func NewVirtualization(artifact Artifact, compression ImageCompression, format ImageFormat) Virtualization {
return Virtualization{
artifact,
compression,
format,
}
}
2 changes: 1 addition & 1 deletion pkg/machine/e2e/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestMachine(t *testing.T) {
}

var _ = BeforeSuite(func() {
qemuVP := qemu.GetVirtualizationProvider()
qemuVP := qemu.VirtualizationProvider()
fcd, err := machine.GetFCOSDownload(qemuVP, defaultStream)
if err != nil {
Fail("unable to get virtual machine image")
Expand Down
36 changes: 14 additions & 22 deletions pkg/machine/hyperv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ import (
"github.com/sirupsen/logrus"
)

type Virtualization struct {
artifact machine.Artifact
compression machine.ImageCompression
format machine.ImageFormat
type HyperVVirtualization struct {
machine.Virtualization
}

func (v Virtualization) Artifact() machine.Artifact {
return machine.None
func VirtualizationProvider() machine.VirtProvider {
return &HyperVVirtualization{
machine.NewVirtualization(machine.HyperV, machine.Zip, machine.Vhdx),
}
}

func (v Virtualization) CheckExclusiveActiveVM() (bool, string, error) {
func (v HyperVVirtualization) CheckExclusiveActiveVM() (bool, string, error) {
vmm := hypervctl.NewVirtualMachineManager()
// Use of GetAll is OK here because we do not want to use the same name
// as something already *actually* configured in hyperv
Expand All @@ -43,15 +43,7 @@ func (v Virtualization) CheckExclusiveActiveVM() (bool, string, error) {
return false, "", nil
}

func (v Virtualization) Compression() machine.ImageCompression {
return v.compression
}

func (v Virtualization) Format() machine.ImageFormat {
return v.format
}

func (v Virtualization) IsValidVMName(name string) (bool, error) {
func (v HyperVVirtualization) IsValidVMName(name string) (bool, error) {
// We check both the local filesystem and hyperv for the valid name
mm := HyperVMachine{Name: name}
configDir, err := machine.GetConfDir(v.VMType())
Expand All @@ -69,7 +61,7 @@ func (v Virtualization) IsValidVMName(name string) (bool, error) {
return true, nil
}

func (v Virtualization) List(opts machine.ListOptions) ([]*machine.ListResponse, error) {
func (v HyperVVirtualization) List(opts machine.ListOptions) ([]*machine.ListResponse, error) {
mms, err := v.loadFromLocalJson()
if err != nil {
return nil, err
Expand Down Expand Up @@ -103,12 +95,12 @@ func (v Virtualization) List(opts machine.ListOptions) ([]*machine.ListResponse,
return response, err
}

func (v Virtualization) LoadVMByName(name string) (machine.VM, error) {
func (v HyperVVirtualization) LoadVMByName(name string) (machine.VM, error) {
m := &HyperVMachine{Name: name}
return m.loadFromFile()
}

func (v Virtualization) NewMachine(opts machine.InitOptions) (machine.VM, error) {
func (v HyperVVirtualization) NewMachine(opts machine.InitOptions) (machine.VM, error) {
m := HyperVMachine{Name: opts.Name}
if len(opts.ImagePath) < 1 {
return nil, errors.New("must define --image-path for hyperv support")
Expand Down Expand Up @@ -180,7 +172,7 @@ func (v Virtualization) NewMachine(opts machine.InitOptions) (machine.VM, error)
return v.LoadVMByName(opts.Name)
}

func (v Virtualization) RemoveAndCleanMachines() error {
func (v HyperVVirtualization) RemoveAndCleanMachines() error {
// Error handling used here is following what qemu did
var (
prevErr error
Expand Down Expand Up @@ -238,11 +230,11 @@ func (v Virtualization) RemoveAndCleanMachines() error {
return prevErr
}

func (v Virtualization) VMType() machine.VMType {
func (v HyperVVirtualization) VMType() machine.VMType {
return vmtype
}

func (v Virtualization) loadFromLocalJson() ([]*HyperVMachine, error) {
func (v HyperVVirtualization) loadFromLocalJson() ([]*HyperVMachine, error) {
var (
jsonFiles []string
mms []*HyperVMachine
Expand Down
8 changes: 0 additions & 8 deletions pkg/machine/hyperv/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ var (
vmtype = machine.HyperVVirt
)

func GetVirtualizationProvider() machine.VirtProvider {
return &Virtualization{
artifact: machine.HyperV,
compression: machine.Zip,
format: machine.Vhdx,
}
}

const (
// Some of this will need to change when we are closer to having
// working code.
Expand Down
Loading

0 comments on commit 4dc2e08

Please sign in to comment.