Skip to content

Commit acddbc6

Browse files
committed
Provider obfuscation in command line
For Podman 6, we still have providers and will continue to have a default provider for each platform. But where a platform has multiple providers, we want users to be able to cross provider boudnaries imposed in Podman 4/5. The key change is to look up virtual machines by name, as before, but to then also iterate all possible providers. As of this PR, init will still only create with the default provider, but a subsequent PR will introdouce an provider override. I also removed the "--all-providers" command line option on `podman machine ls` because it no longer makes sense. And I marked the all provider list test to be skipped. Signed-off-by: Brent Baude <[email protected]>
1 parent 571031f commit acddbc6

File tree

19 files changed

+140
-219
lines changed

19 files changed

+140
-219
lines changed

cmd/podman/machine/cp.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/containers/podman/v5/pkg/copy"
1515
"github.com/containers/podman/v5/pkg/machine"
1616
"github.com/containers/podman/v5/pkg/machine/define"
17-
"github.com/containers/podman/v5/pkg/machine/env"
17+
"github.com/containers/podman/v5/pkg/machine/shim"
1818
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
1919
"github.com/containers/podman/v5/pkg/specgen"
2020
"github.com/spf13/cobra"
@@ -78,12 +78,16 @@ func cp(_ *cobra.Command, args []string) error {
7878
destPath = args[1]
7979
}
8080

81-
mc, err := resolveMachine(srcMachine, destMachine)
81+
vmName, err := resolveMachineName(srcMachine, destMachine)
82+
if err != nil {
83+
return err
84+
}
85+
mc, vmProvider, err := shim.VMExists(vmName)
8286
if err != nil {
8387
return err
8488
}
8589

86-
state, err := provider.State(mc, false)
90+
state, err := vmProvider.State(mc, false)
8791
if err != nil {
8892
return err
8993
}
@@ -135,25 +139,18 @@ func localhostSSHCopy(opts *cpOptions) error {
135139
return cmd.Run()
136140
}
137141

138-
func resolveMachine(srcMachine, destMachine string) (*vmconfigs.MachineConfig, error) {
142+
func resolveMachineName(srcMachine, destMachine string) (string, error) {
139143
if len(srcMachine) > 0 && len(destMachine) > 0 {
140-
return nil, errors.New("copying between two machines is unsupported")
144+
return "", errors.New("copying between two machines is unsupported")
141145
}
142146

143147
if len(srcMachine) == 0 && len(destMachine) == 0 {
144-
return nil, errors.New("a machine name must prefix either the source path or destination path")
145-
}
146-
147-
dirs, err := env.GetMachineDirs(provider.VMType())
148-
if err != nil {
149-
return nil, err
148+
return "", errors.New("a machine name must prefix either the source path or destination path")
150149
}
151-
152150
name := destMachine
153151
if len(srcMachine) > 0 {
154152
cpOpts.IsSrc = true
155153
name = srcMachine
156154
}
157-
158-
return vmconfigs.LoadMachineByName(name, dirs)
155+
return name, nil
159156
}

cmd/podman/machine/init.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/containers/podman/v5/libpod/events"
1313
"github.com/containers/podman/v5/pkg/machine/define"
1414
"github.com/containers/podman/v5/pkg/machine/shim"
15-
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
1615
"github.com/shirou/gopsutil/v4/mem"
1716
"github.com/sirupsen/logrus"
1817
"github.com/spf13/cobra"
@@ -183,17 +182,15 @@ func initMachine(cmd *cobra.Command, args []string) error {
183182
return fmt.Errorf("invalid username %q: %w", initOpts.Username, ldefine.RegexError)
184183
}
185184

185+
// TODO: When the providers comes back and we get a no such vm error, we need to check the hypervisor for
186+
// the same name and error
186187
// Check if machine already exists
187-
_, exists, err := shim.VMExists(initOpts.Name, []vmconfigs.VMProvider{provider})
188-
if err != nil {
188+
var errNotExists *define.ErrVMDoesNotExist
189+
_, _, err := shim.VMExists(initOpts.Name)
190+
if err != nil && !errors.As(err, &errNotExists) {
189191
return err
190192
}
191193

192-
// machine exists, return error
193-
if exists {
194-
return fmt.Errorf("%s: %w", initOpts.Name, define.ErrVMAlreadyExists)
195-
}
196-
197194
// check if a system connection already exists
198195
cons, err := registry.PodmanConfig().ContainersConfDefaultsRO.GetAllConnections()
199196
if err != nil {

cmd/podman/machine/inspect.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/containers/podman/v5/cmd/podman/utils"
1111
"github.com/containers/podman/v5/pkg/machine"
1212
"github.com/containers/podman/v5/pkg/machine/env"
13-
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
13+
"github.com/containers/podman/v5/pkg/machine/shim"
1414
"github.com/spf13/cobra"
1515
"go.podman.io/common/pkg/report"
1616
)
@@ -48,33 +48,34 @@ func inspect(cmd *cobra.Command, args []string) error {
4848
var (
4949
errs utils.OutputErrors
5050
)
51-
dirs, err := env.GetMachineDirs(provider.VMType())
52-
if err != nil {
53-
return err
54-
}
5551
if len(args) < 1 {
5652
args = append(args, defaultMachineName)
5753
}
5854

5955
vms := make([]machine.InspectInfo, 0, len(args))
6056
for _, name := range args {
61-
mc, err := vmconfigs.LoadMachineByName(name, dirs)
57+
mc, vmProvider, err := shim.VMExists(name)
6258
if err != nil {
6359
errs = append(errs, err)
6460
continue
6561
}
6662

67-
state, err := provider.State(mc, false)
63+
dirs, err := env.GetMachineDirs(provider.VMType())
64+
if err != nil {
65+
return err
66+
}
67+
68+
state, err := vmProvider.State(mc, false)
6869
if err != nil {
6970
return err
7071
}
7172

72-
podmanSocket, podmanPipe, err := mc.ConnectionInfo(provider.VMType())
73+
podmanSocket, podmanPipe, err := mc.ConnectionInfo(vmProvider.VMType())
7374
if err != nil {
7475
return err
7576
}
7677

77-
rosetta, err := provider.GetRosetta(mc)
78+
rosetta, err := vmProvider.GetRosetta(mc)
7879
if err != nil {
7980
return err
8081
}
@@ -91,7 +92,7 @@ func inspect(cmd *cobra.Command, args []string) error {
9192
Resources: mc.Resources,
9293
SSHConfig: mc.SSH,
9394
State: state,
94-
UserModeNetworking: provider.UserModeNetworkEnabled(mc),
95+
UserModeNetworking: vmProvider.UserModeNetworkEnabled(mc),
9596
Rootful: mc.HostUser.Rootful,
9697
Rosetta: rosetta,
9798
}

cmd/podman/machine/list.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/containers/podman/v5/pkg/machine"
1818
provider2 "github.com/containers/podman/v5/pkg/machine/provider"
1919
"github.com/containers/podman/v5/pkg/machine/shim"
20-
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
2120
"github.com/docker/go-units"
2221
"github.com/spf13/cobra"
2322
"go.podman.io/common/pkg/completion"
@@ -44,10 +43,9 @@ var (
4443
)
4544

4645
type listFlagType struct {
47-
format string
48-
noHeading bool
49-
quiet bool
50-
allProviders bool
46+
format string
47+
noHeading bool
48+
quiet bool
5149
}
5250

5351
func init() {
@@ -62,25 +60,14 @@ func init() {
6260
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.ListReporter{}))
6361
flags.BoolVarP(&listFlag.noHeading, "noheading", "n", false, "Do not print headers")
6462
flags.BoolVarP(&listFlag.quiet, "quiet", "q", false, "Show only machine names")
65-
flags.BoolVar(&listFlag.allProviders, "all-providers", false, "Show machines from all providers")
6663
}
6764

6865
func list(cmd *cobra.Command, _ []string) error {
6966
var (
7067
opts machine.ListOptions
7168
err error
7269
)
73-
var providers []vmconfigs.VMProvider
74-
if listFlag.allProviders {
75-
providers = provider2.GetAll()
76-
} else {
77-
provider, err = provider2.Get()
78-
if err != nil {
79-
return err
80-
}
81-
providers = []vmconfigs.VMProvider{provider}
82-
}
83-
70+
providers := provider2.GetAll()
8471
listResponse, err := shim.List(providers, opts)
8572
if err != nil {
8673
return err

cmd/podman/machine/machine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var (
4242
)
4343

4444
var (
45+
// TODO This needs to be deleted!
4546
provider vmconfigs.VMProvider
4647
)
4748

cmd/podman/machine/os/apply.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/containers/podman/v5/cmd/podman/registry"
99
"github.com/containers/podman/v5/cmd/podman/validate"
1010
"github.com/containers/podman/v5/pkg/machine/os"
11-
provider2 "github.com/containers/podman/v5/pkg/machine/provider"
1211
"github.com/spf13/cobra"
1312
)
1413

@@ -49,11 +48,7 @@ func apply(_ *cobra.Command, args []string) error {
4948
Restart: restart,
5049
}
5150

52-
provider, err := provider2.Get()
53-
if err != nil {
54-
return err
55-
}
56-
osManager, err := NewOSManager(managerOpts, provider)
51+
osManager, err := NewOSManager(managerOpts)
5752
if err != nil {
5853
return err
5954
}

cmd/podman/machine/os/manager.go

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import (
99
"strings"
1010

1111
"github.com/containers/podman/v5/pkg/machine/define"
12-
"github.com/containers/podman/v5/pkg/machine/env"
1312
pkgOS "github.com/containers/podman/v5/pkg/machine/os"
14-
"github.com/containers/podman/v5/pkg/machine/provider"
15-
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
13+
"github.com/containers/podman/v5/pkg/machine/shim"
1614
machineconfig "go.podman.io/common/pkg/machine"
1715
)
1816

@@ -23,53 +21,43 @@ type ManagerOpts struct {
2321
}
2422

2523
// NewOSManager creates a new OSManager depending on the mode of the call
26-
func NewOSManager(opts ManagerOpts, p vmconfigs.VMProvider) (pkgOS.Manager, error) {
24+
func NewOSManager(opts ManagerOpts) (pkgOS.Manager, error) {
2725
// If a VM name is specified, then we know that we are not inside a
2826
// Podman VM, but rather outside of it.
2927
if machineconfig.IsPodmanMachine() && opts.VMName == "" {
3028
return guestOSManager()
3129
}
32-
return machineOSManager(opts, p)
33-
}
34-
35-
// guestOSManager returns an OSmanager for inside-VM operations
36-
func guestOSManager() (pkgOS.Manager, error) {
37-
dist := GetDistribution()
38-
switch {
39-
case dist.Name == "fedora" && dist.Variant == "coreos":
40-
return &pkgOS.OSTree{}, nil
41-
default:
42-
return nil, errors.New("unsupported OS")
43-
}
44-
}
4530

46-
// machineOSManager returns an os manager that manages outside the VM.
47-
func machineOSManager(opts ManagerOpts, _ vmconfigs.VMProvider) (pkgOS.Manager, error) {
48-
vmName := opts.VMName
31+
// Set to the default name if no VM was provided
4932
if opts.VMName == "" {
50-
vmName = define.DefaultMachineName
33+
opts.VMName = define.DefaultMachineName
5134
}
52-
p, err := provider.Get()
53-
if err != nil {
54-
return nil, err
55-
}
56-
dirs, err := env.GetMachineDirs(p.VMType())
57-
if err != nil {
58-
return nil, err
59-
}
60-
mc, err := vmconfigs.LoadMachineByName(vmName, dirs)
35+
36+
mc, vmProvider, err := shim.VMExists(opts.VMName)
6137
if err != nil {
6238
return nil, err
6339
}
40+
6441
return &pkgOS.MachineOS{
6542
VM: mc,
66-
Provider: p,
43+
Provider: vmProvider,
6744
Args: opts.CLIArgs,
68-
VMName: vmName,
45+
VMName: opts.VMName,
6946
Restart: opts.Restart,
7047
}, nil
7148
}
7249

50+
// guestOSManager returns an OSmanager for inside-VM operations
51+
func guestOSManager() (pkgOS.Manager, error) {
52+
dist := GetDistribution()
53+
switch {
54+
case dist.Name == "fedora" && dist.Variant == "coreos":
55+
return &pkgOS.OSTree{}, nil
56+
default:
57+
return nil, errors.New("unsupported OS")
58+
}
59+
}
60+
7361
type Distribution struct {
7462
Name string
7563
Variant string

cmd/podman/machine/rm.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
"github.com/containers/podman/v5/cmd/podman/registry"
77
"github.com/containers/podman/v5/libpod/events"
88
"github.com/containers/podman/v5/pkg/machine"
9-
"github.com/containers/podman/v5/pkg/machine/env"
109
"github.com/containers/podman/v5/pkg/machine/shim"
11-
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
1210
"github.com/spf13/cobra"
1311
)
1412

@@ -55,17 +53,12 @@ func rm(_ *cobra.Command, args []string) error {
5553
vmName = args[0]
5654
}
5755

58-
dirs, err := env.GetMachineDirs(provider.VMType())
56+
mc, vmProvider, err := shim.VMExists(vmName)
5957
if err != nil {
6058
return err
6159
}
6260

63-
mc, err := vmconfigs.LoadMachineByName(vmName, dirs)
64-
if err != nil {
65-
return err
66-
}
67-
68-
if err := shim.Remove(mc, provider, dirs, destroyOptions); err != nil {
61+
if err := shim.Remove(mc, vmProvider, destroyOptions); err != nil {
6962
return err
7063
}
7164
newMachineEvent(events.Remove, events.Event{Name: vmName})

cmd/podman/machine/set.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ package machine
55
import (
66
"github.com/containers/podman/v5/cmd/podman/registry"
77
"github.com/containers/podman/v5/pkg/machine/define"
8-
"github.com/containers/podman/v5/pkg/machine/env"
98
"github.com/containers/podman/v5/pkg/machine/shim"
10-
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
119
"github.com/spf13/cobra"
1210
"go.podman.io/common/pkg/completion"
1311
"go.podman.io/common/pkg/strongunits"
@@ -93,12 +91,7 @@ func setMachine(cmd *cobra.Command, args []string) error {
9391
vmName = args[0]
9492
}
9593

96-
dirs, err := env.GetMachineDirs(provider.VMType())
97-
if err != nil {
98-
return err
99-
}
100-
101-
mc, err := vmconfigs.LoadMachineByName(vmName, dirs)
94+
mc, vmProvider, err := shim.VMExists(vmName)
10295
if err != nil {
10396
return err
10497
}
@@ -129,5 +122,5 @@ func setMachine(cmd *cobra.Command, args []string) error {
129122

130123
// At this point, we have the known changed information, etc
131124
// Walk through changes to the providers if they need them
132-
return shim.Set(mc, provider, setOpts)
125+
return shim.Set(mc, vmProvider, setOpts)
133126
}

0 commit comments

Comments
 (0)