mirror of
https://github.com/containers/podman.git
synced 2025-06-24 03:08:13 +08:00
fix port list by container with port
code was erronously misinterpretting the port as a containername. Fixes: #1791832 Signed-off-by: baude <bbaude@redhat.com> Signed-off-by: Brent Baude <bbaude@redhat.com>
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/containers/libpod/cmd/podman/cliconfig"
|
"github.com/containers/libpod/cmd/podman/cliconfig"
|
||||||
"github.com/containers/libpod/pkg/adapter"
|
"github.com/containers/libpod/pkg/adapter"
|
||||||
|
"github.com/docker/go-connections/nat"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -48,8 +49,8 @@ func init() {
|
|||||||
|
|
||||||
func portCmd(c *cliconfig.PortValues) error {
|
func portCmd(c *cliconfig.PortValues) error {
|
||||||
var (
|
var (
|
||||||
userProto string
|
userPort nat.Port
|
||||||
userPort int
|
err error
|
||||||
)
|
)
|
||||||
args := c.InputArgs
|
args := c.InputArgs
|
||||||
|
|
||||||
@ -70,25 +71,19 @@ func portCmd(c *cliconfig.PortValues) error {
|
|||||||
if len(args) == 1 && c.Latest {
|
if len(args) == 1 && c.Latest {
|
||||||
port = args[0]
|
port = args[0]
|
||||||
}
|
}
|
||||||
if port != "" {
|
if len(port) > 0 {
|
||||||
fields := strings.Split(port, "/")
|
fields := strings.Split(port, "/")
|
||||||
// User supplied at least port
|
|
||||||
var err error
|
|
||||||
// User supplied port and protocol
|
|
||||||
if len(fields) == 2 {
|
|
||||||
userProto = fields[1]
|
|
||||||
}
|
|
||||||
if len(fields) >= 1 {
|
|
||||||
p := fields[0]
|
|
||||||
userPort, err = strconv.Atoi(p)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "unable to format port")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Format is incorrect
|
|
||||||
if len(fields) > 2 || len(fields) < 1 {
|
if len(fields) > 2 || len(fields) < 1 {
|
||||||
return errors.Errorf("port formats are port/protocol. '%s' is invalid", port)
|
return errors.Errorf("port formats are port/protocol. '%s' is invalid", port)
|
||||||
}
|
}
|
||||||
|
if len(fields) == 1 {
|
||||||
|
fields = append(fields, "tcp")
|
||||||
|
}
|
||||||
|
|
||||||
|
userPort, err = nat.NewPort(fields[1], fields[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
|
runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
|
||||||
@ -96,7 +91,6 @@ func portCmd(c *cliconfig.PortValues) error {
|
|||||||
return errors.Wrapf(err, "could not get runtime")
|
return errors.Wrapf(err, "could not get runtime")
|
||||||
}
|
}
|
||||||
defer runtime.DeferredShutdown(false)
|
defer runtime.DeferredShutdown(false)
|
||||||
|
|
||||||
containers, err := runtime.Port(c)
|
containers, err := runtime.Port(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -122,17 +116,18 @@ func portCmd(c *cliconfig.PortValues) error {
|
|||||||
fmt.Printf("%d/%s -> %s:%d\n", v.ContainerPort, v.Protocol, hostIP, v.HostPort)
|
fmt.Printf("%d/%s -> %s:%d\n", v.ContainerPort, v.Protocol, hostIP, v.HostPort)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// We have a match on ports
|
containerPort, err := nat.NewPort(v.Protocol, strconv.Itoa(int(v.ContainerPort)))
|
||||||
if v.ContainerPort == int32(userPort) {
|
if err != nil {
|
||||||
if userProto == "" || userProto == v.Protocol {
|
return err
|
||||||
fmt.Printf("%s:%d\n", hostIP, v.HostPort)
|
}
|
||||||
found = true
|
if containerPort == userPort {
|
||||||
break
|
fmt.Printf("%s:%d\n", hostIP, v.HostPort)
|
||||||
}
|
found = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found && port != "" {
|
if !found && port != "" {
|
||||||
return errors.Errorf("failed to find published port '%d'", userPort)
|
return errors.Errorf("failed to find published port %q", port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1116,7 +1116,11 @@ func (r *LocalRuntime) Port(c *cliconfig.PortValues) ([]*Container, error) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if !c.All {
|
if !c.All {
|
||||||
containers, err = shortcuts.GetContainersByContext(false, c.Latest, c.InputArgs, r.Runtime)
|
names := []string{}
|
||||||
|
if len(c.InputArgs) > 1 {
|
||||||
|
names = []string{c.InputArgs[0]}
|
||||||
|
}
|
||||||
|
containers, err = shortcuts.GetContainersByContext(false, c.Latest, names, r.Runtime)
|
||||||
} else {
|
} else {
|
||||||
containers, err = r.Runtime.GetRunningContainers()
|
containers, err = r.Runtime.GetRunningContainers()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user