-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate MachineEnabled and replace with API
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
Showing
10 changed files
with
145 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
wsl |