diff options
author | openshift-merge-bot[bot] <148852131+openshift-merge-bot[bot]@users.noreply.github.com> | 2024-08-30 16:52:31 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-30 16:52:31 +0000 |
commit | ef905ef8d0c2dd9468029c6575ce4ab74442b725 (patch) | |
tree | 36e65e6454ecc2afae3e4c576483538934d709fc /cmd | |
parent | 3a3f14db620d59f8ef98429bd449015a4128b4bb (diff) | |
parent | 41f945fc249b2a40220b312a2bbc43cc45f5c24b (diff) |
Merge pull request #23726 from ashley-cui/machlist
machine: Add -all-providers flag to machine list
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/podman/machine/list.go | 60 | ||||
-rw-r--r-- | cmd/podman/machine/reset.go | 11 |
2 files changed, 51 insertions, 20 deletions
diff --git a/cmd/podman/machine/list.go b/cmd/podman/machine/list.go index 04ec6eec2..bddcf49c2 100644 --- a/cmd/podman/machine/list.go +++ b/cmd/podman/machine/list.go @@ -7,15 +7,18 @@ import ( "os" "sort" "strconv" + "strings" "time" "github.com/containers/common/pkg/completion" + "github.com/containers/common/pkg/config" "github.com/containers/common/pkg/report" "github.com/containers/podman/v5/cmd/podman/common" "github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/cmd/podman/validate" "github.com/containers/podman/v5/pkg/domain/entities" "github.com/containers/podman/v5/pkg/machine" + provider2 "github.com/containers/podman/v5/pkg/machine/provider" "github.com/containers/podman/v5/pkg/machine/shim" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/docker/go-units" @@ -24,11 +27,12 @@ import ( var ( lsCmd = &cobra.Command{ - Use: "list [options]", - Aliases: []string{"ls"}, - Short: "List machines", - Long: "List managed virtual machines.", - PersistentPreRunE: machinePreRunE, + Use: "list [options]", + Aliases: []string{"ls"}, + Short: "List machines", + Long: "List managed virtual machines.", + // do not use machinePreRunE, as that pre-sets the provider + PersistentPreRunE: rootlessOnly, RunE: list, Args: validate.NoArgs, ValidArgsFunction: completion.AutocompleteNone, @@ -40,9 +44,10 @@ var ( ) type listFlagType struct { - format string - noHeading bool - quiet bool + format string + noHeading bool + quiet bool + allProviders bool } func init() { @@ -57,6 +62,7 @@ func init() { _ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.ListReporter{})) flags.BoolVarP(&listFlag.noHeading, "noheading", "n", false, "Do not print headers") flags.BoolVarP(&listFlag.quiet, "quiet", "q", false, "Show only machine names") + flags.BoolVar(&listFlag.allProviders, "all-providers", false, "Show machines from all providers") } func list(cmd *cobra.Command, args []string) error { @@ -64,8 +70,18 @@ func list(cmd *cobra.Command, args []string) error { opts machine.ListOptions err error ) + var providers []vmconfigs.VMProvider + if listFlag.allProviders { + providers = provider2.GetAll() + } else { + provider, err = provider2.Get() + if err != nil { + return err + } + providers = []vmconfigs.VMProvider{provider} + } - listResponse, err := shim.List([]vmconfigs.VMProvider{provider}, opts) + listResponse, err := shim.List(providers, opts) if err != nil { return err } @@ -79,12 +95,8 @@ func list(cmd *cobra.Command, args []string) error { return listResponse[i].Running }) - defaultCon := "" - con, err := registry.PodmanConfig().ContainersConfDefaultsRO.GetConnection("", true) - if err == nil { - // ignore the error here we only want to know if we have a default connection to show it in list - defaultCon = con.Name - } + // ignore the error here we only want to know if we have a default connection to show it in list + defaultCon, _ := registry.PodmanConfig().ContainersConfDefaultsRO.GetConnection("", true) if report.IsJSON(listFlag.format) { machineReporter := toMachineFormat(listResponse, defaultCon) @@ -152,11 +164,16 @@ func streamName(imageStream string) string { return imageStream } -func toMachineFormat(vms []*machine.ListResponse, defaultCon string) []*entities.ListReporter { +func toMachineFormat(vms []*machine.ListResponse, defaultCon *config.Connection) []*entities.ListReporter { machineResponses := make([]*entities.ListReporter, 0, len(vms)) for _, vm := range vms { + isDefault := false + // check port, in case we somehow have machines with the same name in different providers + if defaultCon != nil { + isDefault = vm.Name == defaultCon.Name && strings.Contains(defaultCon.URI, strconv.Itoa((vm.Port))) + } response := new(entities.ListReporter) - response.Default = vm.Name == defaultCon + response.Default = isDefault response.Name = vm.Name response.Running = vm.Running response.LastUp = strTime(vm.LastUp) @@ -177,11 +194,16 @@ func toMachineFormat(vms []*machine.ListResponse, defaultCon string) []*entities return machineResponses } -func toHumanFormat(vms []*machine.ListResponse, defaultCon string) []*entities.ListReporter { +func toHumanFormat(vms []*machine.ListResponse, defaultCon *config.Connection) []*entities.ListReporter { humanResponses := make([]*entities.ListReporter, 0, len(vms)) for _, vm := range vms { response := new(entities.ListReporter) - if vm.Name == defaultCon { + isDefault := false + // check port, in case we somehow have machines with the same name in different providers + if defaultCon != nil { + isDefault = vm.Name == defaultCon.Name && strings.Contains(defaultCon.URI, strconv.Itoa((vm.Port))) + } + if isDefault { response.Name = vm.Name + "*" response.Default = true } else { diff --git a/cmd/podman/machine/reset.go b/cmd/podman/machine/reset.go index 9b76f97a8..2d49d8f8c 100644 --- a/cmd/podman/machine/reset.go +++ b/cmd/podman/machine/reset.go @@ -15,6 +15,7 @@ import ( "github.com/containers/podman/v5/pkg/machine" provider2 "github.com/containers/podman/v5/pkg/machine/provider" "github.com/containers/podman/v5/pkg/machine/shim" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -50,10 +51,18 @@ func reset(_ *cobra.Command, _ []string) error { err error ) - providers, err := provider2.GetAll(resetOptions.Force) + providers := provider2.GetAll() if err != nil { return err } + for _, p := range providers { + hasPerms := provider2.HasPermsForProvider(p.VMType()) + isInstalled, err := provider2.IsInstalled(p.VMType()) + if !hasPerms && (isInstalled || err != nil) && !resetOptions.Force { + logrus.Warnf("Managing %s machines require admin authority.", p.VMType().String()) + logrus.Warnf("Continuing to reset may cause Podman to be unaware of remaining VMs in the VM manager.") + } + } if !resetOptions.Force { listResponse, err := shim.List(providers, machine.ListOptions{}) |