Skip to content

Commit

Permalink
Deprecate MachineEnabled and replace with API
Browse files Browse the repository at this point in the history
machine.IsPodmanMachine replaces MachineEnabled
machine.MachineHostType informs the type unknown, qemu, wsl, etc
machine.IsGvProxyBased used to make specific determinations re gvproxy

Signed-off-by: Jason T. Greene <jason.greene@redhat.com>
  • Loading branch information
n1hility committed Apr 23, 2022
1 parent 333e4c0 commit 0db57c8
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 15 deletions.
6 changes: 0 additions & 6 deletions docs/containers.conf.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,6 @@ Change the default only if you are sure of what you are doing, in general
faster "shm" lock type. You may need to run "podman system renumber" after you
change the lock type.

**machine_enabled**=false

Indicates if Podman is running inside a VM via Podman Machine.
Podman uses this value to do extra setup around networking from the
container inside the VM to to host.

**multi_image_archive**=false

Allows for creating archives (e.g., tarballs) with more than one image. Some container engines, such as Podman, interpret additional arguments as tags for one image and hence do not store more than one image. The default behavior can be altered with this option.
Expand Down
5 changes: 3 additions & 2 deletions libnetwork/etchosts/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/libnetwork/util"
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/machine"
"github.com/containers/storage/pkg/unshare"
)

Expand All @@ -15,8 +16,8 @@ func GetHostContainersInternalIP(conf *config.Config, netStatus map[string]types
switch conf.Containers.HostContainersInternalIP {
case "":
// if empty (default) we will automatically choose one below
// if machine we let the gvproxy dns server handle the dns name so do not add it
if conf.Engine.MachineEnabled {
// if machine using gvproxy we let the gvproxy dns server handle the dns name so do not add it
if machine.IsGvProxyBased() {
return ""
}
case "none":
Expand Down
3 changes: 2 additions & 1 deletion libnetwork/network/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/containers/common/libnetwork/netavark"
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/machine"
"github.com/containers/storage"
"github.com/containers/storage/pkg/homedir"
"github.com/containers/storage/pkg/ioutils"
Expand Down Expand Up @@ -173,7 +174,7 @@ func getCniInterface(conf *config.Config) (types.ContainerNetwork, error) {
DefaultNetwork: conf.Network.DefaultNetwork,
DefaultSubnet: conf.Network.DefaultSubnet,
DefaultsubnetPools: conf.Network.DefaultSubnetPools,
IsMachine: conf.Engine.MachineEnabled,
IsMachine: machine.IsGvProxyBased(),
})
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ type EngineConfig struct {
LockType string `toml:"lock_type,omitempty"`

// MachineEnabled indicates if Podman is running in a podman-machine VM
//
// This method is soft deprecated, use machine.IsPodmanMachine instead
MachineEnabled bool `toml:"machine_enabled,omitempty"`

// MultiImageArchive - if true, the container engine allows for storing
Expand Down
6 changes: 0 additions & 6 deletions pkg/config/containers.conf
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,6 @@ default_sysctls = [
#
#lock_type** = "shm"

# Indicates if Podman is running inside a VM via Podman Machine.
# Podman uses this value to do extra setup around networking from the
# container inside the VM to to host.
#
#machine_enabled = false

# MultiImageArchive - if true, the container engine allows for storing archives
# (e.g., of the docker-archive transport) with multiple images. By default,
# Podman creates single-image archives.
Expand Down
70 changes: 70 additions & 0 deletions pkg/machine/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package machine

import (
"os"
"strings"
"sync"

"github.com/containers/common/pkg/config"
"github.com/sirupsen/logrus"
)

type MachineMarker struct {
Enabled bool
Type string
}

const (
markerFile = "/etc/containers/podman-machine"
Wsl = "wsl"
Qemu = "qemu"
)

var (
markerSync sync.Once
machineMarker *MachineMarker
)

func loadMachineMarker(file string) {
var kind string

// Support deprecated config value for compatibility
enabled := isLegacyConfigSet()

if content, err := os.ReadFile(file); err == nil {
enabled = true
kind = strings.TrimSpace(string(content))
}

machineMarker = &MachineMarker{enabled, kind}
}

func isLegacyConfigSet() bool {
config, err := config.Default()
if err != nil {
logrus.Warnf("could not obtain container configuration")
return false
}

//nolint:staticcheck //lint:ignore SA1019 deprecated call
return config.Engine.MachineEnabled
}

func IsPodmanMachine() bool {
return GetMachineMarker().Enabled
}

func MachineHostType() string {
return GetMachineMarker().Type
}

func IsGvProxyBased() bool {
return IsPodmanMachine() && MachineHostType() != Wsl
}

func GetMachineMarker() *MachineMarker {
markerSync.Do(func() {
loadMachineMarker(markerFile)
})
return machineMarker
}
13 changes: 13 additions & 0 deletions pkg/machine/machine_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package machine

import (
"testing"

. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
gomega.RegisterFailHandler(Fail)
RunSpecs(t, "Machine Suite")
}
54 changes: 54 additions & 0 deletions pkg/machine/machine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package machine

import (
"github.com/containers/common/pkg/config"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

var _ = Describe("Machine", func() {
BeforeEach(func() {
// disable normal init for testing
markerSync.Do(func() {})

// ensure legacy flag is off
config, _ := config.Default()
//nolint:staticcheck //lint:ignore SA1019 deprecated call
config.Engine.MachineEnabled = false
})

It("not a machine", func() {
loadMachineMarker("testdata/does-not-exist")

gomega.Expect(IsPodmanMachine()).To(gomega.BeFalse())
gomega.Expect(MachineHostType()).To(gomega.BeEmpty())
gomega.Expect(IsGvProxyBased()).To(gomega.BeFalse())
})

It("generic machine", func() {
loadMachineMarker("testdata/empty-machine")

gomega.Expect(IsPodmanMachine()).To(gomega.BeTrue())
gomega.Expect(MachineHostType()).To(gomega.BeEmpty())
gomega.Expect(IsGvProxyBased()).To(gomega.BeTrue())
})

It("wsl machine", func() {
loadMachineMarker("testdata/wsl-machine")

gomega.Expect(IsPodmanMachine()).To(gomega.BeTrue())
gomega.Expect(MachineHostType()).To(gomega.Equal(Wsl))
gomega.Expect(IsGvProxyBased()).To(gomega.BeFalse())
})

It("legacy config machine", func() {
config, _ := config.Default()
//nolint:staticcheck //lint:ignore SA1019 deprecated call
config.Engine.MachineEnabled = true
loadMachineMarker("testdata/does-not-exist")

gomega.Expect(IsPodmanMachine()).To(gomega.BeTrue())
gomega.Expect(MachineHostType()).To(gomega.BeEmpty())
gomega.Expect(IsGvProxyBased()).To(gomega.BeTrue())
})
})
Empty file.
1 change: 1 addition & 0 deletions pkg/machine/testdata/wsl-machine
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wsl

0 comments on commit 0db57c8

Please sign in to comment.