Skip to content

Commit

Permalink
Merge pull request #20572 from baude/ocirefactor
Browse files Browse the repository at this point in the history
Refactor key machine objects
  • Loading branch information
openshift-merge-bot[bot] authored Nov 7, 2023
2 parents f47a85f + a45ba06 commit 57dbd61
Show file tree
Hide file tree
Showing 33 changed files with 783 additions and 657 deletions.
8 changes: 5 additions & 3 deletions pkg/machine/applehv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"

"github.com/containers/podman/v4/pkg/machine"
"github.com/containers/podman/v4/pkg/machine/compression"
"github.com/containers/podman/v4/pkg/machine/define"
vfConfig "github.com/crc-org/vfkit/pkg/config"
"github.com/docker/go-units"
"golang.org/x/sys/unix"
Expand All @@ -34,7 +36,7 @@ type MMHardwareConfig struct {

func VirtualizationProvider() machine.VirtProvider {
return &AppleHVVirtualization{
machine.NewVirtualization(machine.AppleHV, machine.Xz, machine.Raw, vmtype),
machine.NewVirtualization(define.AppleHV, compression.Xz, define.Raw, vmtype),
}
}

Expand Down Expand Up @@ -116,7 +118,7 @@ func (v AppleHVVirtualization) NewMachine(opts machine.InitOptions) (machine.VM,
return nil, err
}

configPath, err := machine.NewMachineFile(getVMConfigPath(configDir, opts.Name), nil)
configPath, err := define.NewMachineFile(getVMConfigPath(configDir, opts.Name), nil)
if err != nil {
return nil, err
}
Expand All @@ -127,7 +129,7 @@ func (v AppleHVVirtualization) NewMachine(opts machine.InitOptions) (machine.VM,
return nil, err
}

ignitionPath, err := machine.NewMachineFile(filepath.Join(configDir, m.Name)+".ign", nil)
ignitionPath, err := define.NewMachineFile(filepath.Join(configDir, m.Name)+".ign", nil)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/applehv/ignition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"net"
"net/http"

"github.com/containers/podman/v4/pkg/machine"
"github.com/containers/podman/v4/pkg/machine/define"
"github.com/sirupsen/logrus"
)

// serveIgnitionOverSock allows podman to open a small httpd instance on the vsock between the host
// and guest to inject the ignitionfile into fcos
func (m *MacMachine) serveIgnitionOverSock(ignitionSocket *machine.VMFile) error {
func (m *MacMachine) serveIgnitionOverSock(ignitionSocket *define.VMFile) error {
logrus.Debugf("reading ignition file: %s", m.IgnitionFile.GetPath())
ignFile, err := m.IgnitionFile.Read()
if err != nil {
Expand Down
33 changes: 17 additions & 16 deletions pkg/machine/applehv/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/containers/common/pkg/config"
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/containers/podman/v4/pkg/machine"
"github.com/containers/podman/v4/pkg/machine/define"
"github.com/containers/podman/v4/pkg/strongunits"
"github.com/containers/podman/v4/pkg/util"
"github.com/containers/podman/v4/utils"
Expand All @@ -45,13 +46,13 @@ const (
type VfkitHelper struct {
LogLevel logrus.Level
Endpoint string
VfkitBinaryPath *machine.VMFile
VfkitBinaryPath *define.VMFile
VirtualMachine *vfConfig.VirtualMachine
}

type MacMachine struct {
// ConfigPath is the fully qualified path to the configuration file
ConfigPath machine.VMFile
ConfigPath define.VMFile
// HostUser contains info about host user
machine.HostUser
// ImageConfig describes the bootable image
Expand All @@ -61,7 +62,7 @@ type MacMachine struct {
// Name of VM
Name string
// ReadySocket tells host when vm is booted
ReadySocket machine.VMFile
ReadySocket define.VMFile
// ResourceConfig is physical attrs of the VM
machine.ResourceConfig
// SSHConfig for accessing the remote vm
Expand All @@ -74,17 +75,17 @@ type MacMachine struct {
LastUp time.Time
// The VFKit endpoint where we can interact with the VM
Vfkit VfkitHelper
LogPath machine.VMFile
GvProxyPid machine.VMFile
GvProxySock machine.VMFile
LogPath define.VMFile
GvProxyPid define.VMFile
GvProxySock define.VMFile

// Used at runtime for serializing write operations
lock *lockfile.LockFile
}

// setGVProxyInfo sets the VM's gvproxy pid and socket files
func (m *MacMachine) setGVProxyInfo(runtimeDir string) error {
gvProxyPid, err := machine.NewMachineFile(filepath.Join(runtimeDir, "gvproxy.pid"), nil)
gvProxyPid, err := define.NewMachineFile(filepath.Join(runtimeDir, "gvproxy.pid"), nil)
if err != nil {
return err
}
Expand All @@ -95,7 +96,7 @@ func (m *MacMachine) setGVProxyInfo(runtimeDir string) error {

// setVfkitInfo stores the default devices, sets the vfkit endpoint, and
// locates/stores the path to the binary
func (m *MacMachine) setVfkitInfo(cfg *config.Config, readySocket machine.VMFile) error {
func (m *MacMachine) setVfkitInfo(cfg *config.Config, readySocket define.VMFile) error {
defaultDevices, err := getDefaultDevices(m.ImagePath.GetPath(), m.LogPath.GetPath(), readySocket.GetPath())
if err != nil {
return err
Expand All @@ -105,7 +106,7 @@ func (m *MacMachine) setVfkitInfo(cfg *config.Config, readySocket machine.VMFile
if err != nil {
return err
}
vfkitBinaryPath, err := machine.NewMachineFile(vfkitPath, nil)
vfkitBinaryPath, err := define.NewMachineFile(vfkitPath, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -217,7 +218,7 @@ func (m *MacMachine) Init(opts machine.InitOptions) (bool, error) {
m.ImagePath = *imagePath
m.ImageStream = strm.String()

logPath, err := machine.NewMachineFile(filepath.Join(dataDir, fmt.Sprintf("%s.log", m.Name)), nil)
logPath, err := define.NewMachineFile(filepath.Join(dataDir, fmt.Sprintf("%s.log", m.Name)), nil)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -490,7 +491,7 @@ func (m *MacMachine) SSH(name string, opts machine.SSHOptions) error {

// deleteIgnitionSocket retrieves the ignition socket, deletes it, and returns a
// pointer to the `VMFile`
func (m *MacMachine) deleteIgnitionSocket() (*machine.VMFile, error) {
func (m *MacMachine) deleteIgnitionSocket() (*define.VMFile, error) {
ignitionSocket, err := m.getIgnitionSock()
if err != nil {
return nil, err
Expand Down Expand Up @@ -556,7 +557,7 @@ func (m *MacMachine) addVolumesToVfKit() error {
}

func (m *MacMachine) Start(name string, opts machine.StartOptions) error {
var ignitionSocket *machine.VMFile
var ignitionSocket *define.VMFile

m.lock.Lock()
defer m.lock.Unlock()
Expand Down Expand Up @@ -996,13 +997,13 @@ func (m *MacMachine) dockerSock() (string, error) {
return filepath.Join(dd, "podman.sock"), nil
}

func (m *MacMachine) forwardSocketPath() (*machine.VMFile, error) {
func (m *MacMachine) forwardSocketPath() (*define.VMFile, error) {
sockName := "podman.sock"
path, err := machine.GetDataDir(machine.AppleHvVirt)
if err != nil {
return nil, fmt.Errorf("Resolving data dir: %s", err.Error())
}
return machine.NewMachineFile(filepath.Join(path, sockName), &sockName)
return define.NewMachineFile(filepath.Join(path, sockName), &sockName)
}

// resizeDisk uses os truncate to resize (only larger) a raw disk. the input size
Expand Down Expand Up @@ -1034,7 +1035,7 @@ func (m *MacMachine) isFirstBoot() (bool, error) {
return m.LastUp == never, nil
}

func (m *MacMachine) getIgnitionSock() (*machine.VMFile, error) {
func (m *MacMachine) getIgnitionSock() (*define.VMFile, error) {
dataDir, err := machine.GetDataDir(machine.AppleHvVirt)
if err != nil {
return nil, err
Expand All @@ -1044,7 +1045,7 @@ func (m *MacMachine) getIgnitionSock() (*machine.VMFile, error) {
return nil, err
}
}
return machine.NewMachineFile(filepath.Join(dataDir, ignitionSocketName), nil)
return define.NewMachineFile(filepath.Join(dataDir, ignitionSocketName), nil)
}

func (m *MacMachine) getRuntimeDir() (string, error) {
Expand Down
91 changes: 91 additions & 0 deletions pkg/machine/compression/compression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package compression

import "testing"

func Test_compressionFromFile(t *testing.T) {
type args struct {
path string
}
var tests = []struct {
name string
args args
want ImageCompression
}{
{
name: "xz",
args: args{
path: "/tmp/foo.xz",
},
want: Xz,
},
{
name: "gzip",
args: args{
path: "/tmp/foo.gz",
},
want: Gz,
},
{
name: "bz2",
args: args{
path: "/tmp/foo.bz2",
},
want: Bz2,
},
{
name: "default is xz",
args: args{
path: "/tmp/foo",
},
want: Xz,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := KindFromFile(tt.args.path); got != tt.want {
t.Errorf("KindFromFile() = %v, want %v", got, tt.want)
}
})
}
}

func TestImageCompression_String(t *testing.T) {
tests := []struct {
name string
c ImageCompression
want string
}{
{
name: "xz",
c: Xz,
want: "xz",
},
{
name: "gz",
c: Gz,
want: "gz",
},
{
name: "bz2",
c: Bz2,
want: "bz2",
},
{
name: "zip",
c: Zip,
want: "zip",
},
{
name: "xz is default",
c: 99,
want: "xz",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.c.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
36 changes: 36 additions & 0 deletions pkg/machine/compression/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package compression

import "strings"

type ImageCompression int64

const (
Xz ImageCompression = iota
Zip
Gz
Bz2
)

func KindFromFile(path string) ImageCompression {
switch {
case strings.HasSuffix(path, Bz2.String()):
return Bz2
case strings.HasSuffix(path, Gz.String()):
return Gz
case strings.HasSuffix(path, Zip.String()):
return Zip
}
return Xz
}

func (c ImageCompression) String() string {
switch c {
case Gz:
return "gz"
case Zip:
return "zip"
case Bz2:
return "bz2"
}
return "xz"
}
Loading

1 comment on commit 57dbd61

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.