mirror of
https://github.com/containers/podman.git
synced 2025-09-28 17:25:31 +08:00
Merge pull request #5978 from rhatdan/ports
Make podman container list == podman ps
This commit is contained in:
@ -1,28 +1,11 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/cri-o/ocicni/pkg/ocicni"
|
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExposedPorts parses user and image ports and returns binding information
|
func verifyExpose(expose []string) error {
|
||||||
func ExposedPorts(expose []string, publish []ocicni.PortMapping, publishAll bool, imageExposedPorts map[string]struct{}) ([]ocicni.PortMapping, error) {
|
|
||||||
containerPorts := make(map[string]string)
|
|
||||||
|
|
||||||
// TODO this needs to be added into a something that
|
|
||||||
// has access to an imageengine
|
|
||||||
// add expose ports from the image itself
|
|
||||||
//for expose := range imageExposedPorts {
|
|
||||||
// _, port := nat.SplitProtoPort(expose)
|
|
||||||
// containerPorts[port] = ""
|
|
||||||
//}
|
|
||||||
|
|
||||||
// add the expose ports from the user (--expose)
|
// add the expose ports from the user (--expose)
|
||||||
// can be single or a range
|
// can be single or a range
|
||||||
for _, expose := range expose {
|
for _, expose := range expose {
|
||||||
@ -30,97 +13,10 @@ func ExposedPorts(expose []string, publish []ocicni.PortMapping, publishAll bool
|
|||||||
_, port := nat.SplitProtoPort(expose)
|
_, port := nat.SplitProtoPort(expose)
|
||||||
//parse the start and end port and create a sequence of ports to expose
|
//parse the start and end port and create a sequence of ports to expose
|
||||||
//if expose a port, the start and end port are the same
|
//if expose a port, the start and end port are the same
|
||||||
start, end, err := nat.ParsePortRange(port)
|
_, _, err := nat.ParsePortRange(port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid range format for --expose: %s, error: %s", expose, err)
|
return errors.Wrapf(err, "invalid range format for --expose: %s", expose)
|
||||||
}
|
|
||||||
for i := start; i <= end; i++ {
|
|
||||||
containerPorts[strconv.Itoa(int(i))] = ""
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
// TODO/FIXME this is hell reencarnated
|
|
||||||
// parse user inputted port bindings
|
|
||||||
pbPorts, portBindings, err := nat.ParsePortSpecs([]string{})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete exposed container ports if being used by -p
|
|
||||||
for i := range pbPorts {
|
|
||||||
delete(containerPorts, i.Port())
|
|
||||||
}
|
|
||||||
|
|
||||||
// iterate container ports and make port bindings from them
|
|
||||||
if publishAll {
|
|
||||||
for e := range containerPorts {
|
|
||||||
//support two formats for expose, original format <portnum>/[<proto>] or <startport-endport>/[<proto>]
|
|
||||||
//proto, port := nat.SplitProtoPort(e)
|
|
||||||
p, err := nat.NewPort("tcp", e)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rp, err := getRandomPort()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
logrus.Debug(fmt.Sprintf("Using random host port %d with container port %d", rp, p.Int()))
|
|
||||||
portBindings[p] = CreatePortBinding(rp, "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We need to see if any host ports are not populated and if so, we need to assign a
|
|
||||||
// random port to them.
|
|
||||||
for k, pb := range portBindings {
|
|
||||||
if pb[0].HostPort == "" {
|
|
||||||
hostPort, err := getRandomPort()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
logrus.Debug(fmt.Sprintf("Using random host port %d with container port %s", hostPort, k.Port()))
|
|
||||||
pb[0].HostPort = strconv.Itoa(hostPort)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var pms []ocicni.PortMapping
|
|
||||||
for k, v := range portBindings {
|
|
||||||
for _, pb := range v {
|
|
||||||
hp, err := strconv.Atoi(pb.HostPort)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
pms = append(pms, ocicni.PortMapping{
|
|
||||||
HostPort: int32(hp),
|
|
||||||
ContainerPort: int32(k.Int()),
|
|
||||||
//Protocol: "",
|
|
||||||
HostIP: pb.HostIP,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return pms, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getRandomPort() (int, error) {
|
|
||||||
l, err := net.Listen("tcp", ":0")
|
|
||||||
if err != nil {
|
|
||||||
return 0, errors.Wrapf(err, "unable to get free port")
|
|
||||||
}
|
|
||||||
defer l.Close()
|
|
||||||
_, randomPort, err := net.SplitHostPort(l.Addr().String())
|
|
||||||
if err != nil {
|
|
||||||
return 0, errors.Wrapf(err, "unable to determine free port")
|
|
||||||
}
|
|
||||||
rp, err := strconv.Atoi(randomPort)
|
|
||||||
if err != nil {
|
|
||||||
return 0, errors.Wrapf(err, "unable to convert random port to int")
|
|
||||||
}
|
|
||||||
return rp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//CreatePortBinding takes port (int) and IP (string) and creates an array of portbinding structs
|
|
||||||
func CreatePortBinding(hostPort int, hostIP string) []nat.PortBinding {
|
|
||||||
pb := nat.PortBinding{
|
|
||||||
HostPort: strconv.Itoa(hostPort),
|
|
||||||
}
|
|
||||||
pb.HostIP = hostIP
|
|
||||||
return []nat.PortBinding{pb}
|
|
||||||
}
|
}
|
||||||
|
@ -227,11 +227,14 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.Terminal = c.TTY
|
s.Terminal = c.TTY
|
||||||
ep, err := ExposedPorts(c.Expose, c.Net.PublishPorts, c.PublishAll, nil)
|
|
||||||
if err != nil {
|
if err := verifyExpose(c.Expose); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.PortMappings = ep
|
// We are not handling the Expose flag yet.
|
||||||
|
// s.PortsExpose = c.Expose
|
||||||
|
s.PortMappings = c.Net.PublishPorts
|
||||||
|
s.PublishImagePorts = c.PublishAll
|
||||||
s.Pod = c.Pod
|
s.Pod = c.Pod
|
||||||
|
|
||||||
for k, v := range map[string]*specgen.Namespace{
|
for k, v := range map[string]*specgen.Namespace{
|
||||||
|
@ -14,7 +14,7 @@ var (
|
|||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
Short: "List containers",
|
Short: "List containers",
|
||||||
Long: "Prints out information about the containers",
|
Long: "Prints out information about the containers",
|
||||||
RunE: containers,
|
RunE: ps,
|
||||||
Example: `podman container list -a
|
Example: `podman container list -a
|
||||||
podman container list -a --format "{{.ID}} {{.Image}} {{.Labels}} {{.Mounts}}"
|
podman container list -a --format "{{.ID}} {{.Image}} {{.Labels}} {{.Mounts}}"
|
||||||
podman container list --size --sort names`,
|
podman container list --size --sort names`,
|
||||||
@ -27,8 +27,5 @@ func init() {
|
|||||||
Command: listCmd,
|
Command: listCmd,
|
||||||
Parent: containerCmd,
|
Parent: containerCmd,
|
||||||
})
|
})
|
||||||
}
|
listFlagSet(listCmd.Flags())
|
||||||
|
|
||||||
func containers(cmd *cobra.Command, args []string) error {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/docker/go-units"
|
"github.com/docker/go-units"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -47,7 +48,10 @@ func init() {
|
|||||||
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
|
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
|
||||||
Command: psCommand,
|
Command: psCommand,
|
||||||
})
|
})
|
||||||
flags := psCommand.Flags()
|
listFlagSet(psCommand.Flags())
|
||||||
|
}
|
||||||
|
|
||||||
|
func listFlagSet(flags *pflag.FlagSet) {
|
||||||
flags.BoolVarP(&listOpts.All, "all", "a", false, "Show all the containers, default is only running containers")
|
flags.BoolVarP(&listOpts.All, "all", "a", false, "Show all the containers, default is only running containers")
|
||||||
flags.StringSliceVarP(&filters, "filter", "f", []string{}, "Filter output based on conditions given")
|
flags.StringSliceVarP(&filters, "filter", "f", []string{}, "Filter output based on conditions given")
|
||||||
flags.StringVar(&listOpts.Format, "format", "", "Pretty-print containers to JSON or using a Go template")
|
flags.StringVar(&listOpts.Format, "format", "", "Pretty-print containers to JSON or using a Go template")
|
||||||
@ -165,14 +169,14 @@ func ps(cmd *cobra.Command, args []string) error {
|
|||||||
responses = append(responses, psReporter{r})
|
responses = append(responses, psReporter{r})
|
||||||
}
|
}
|
||||||
|
|
||||||
headers, row := createPsOut()
|
headers, format := createPsOut()
|
||||||
if cmd.Flag("format").Changed {
|
if cmd.Flag("format").Changed {
|
||||||
row = listOpts.Format
|
format = listOpts.Format
|
||||||
if !strings.HasPrefix(row, "\n") {
|
if !strings.HasPrefix(format, "\n") {
|
||||||
row += "\n"
|
format += "\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
format := "{{range . }}" + row + "{{end}}"
|
format = "{{range . }}" + format + "{{end}}"
|
||||||
if !listOpts.Quiet && !cmd.Flag("format").Changed {
|
if !listOpts.Quiet && !cmd.Flag("format").Changed {
|
||||||
format = headers + format
|
format = headers + format
|
||||||
}
|
}
|
||||||
@ -247,6 +251,14 @@ type psReporter struct {
|
|||||||
entities.ListContainer
|
entities.ListContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ImageID returns the ID of the container
|
||||||
|
func (l psReporter) ImageID() string {
|
||||||
|
if !noTrunc {
|
||||||
|
return l.ListContainer.ImageID[0:12]
|
||||||
|
}
|
||||||
|
return l.ListContainer.ImageID
|
||||||
|
}
|
||||||
|
|
||||||
// ID returns the ID of the container
|
// ID returns the ID of the container
|
||||||
func (l psReporter) ID() string {
|
func (l psReporter) ID() string {
|
||||||
if !noTrunc {
|
if !noTrunc {
|
||||||
|
@ -25,6 +25,8 @@ type ListContainer struct {
|
|||||||
ID string `json:"Id"`
|
ID string `json:"Id"`
|
||||||
// Container image
|
// Container image
|
||||||
Image string
|
Image string
|
||||||
|
// Container image ID
|
||||||
|
ImageID string
|
||||||
// If this container is a Pod infra container
|
// If this container is a Pod infra container
|
||||||
IsInfra bool
|
IsInfra bool
|
||||||
// Labels for container
|
// Labels for container
|
||||||
|
@ -158,6 +158,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
|
|||||||
ExitedAt: exitedTime.Unix(),
|
ExitedAt: exitedTime.Unix(),
|
||||||
ID: conConfig.ID,
|
ID: conConfig.ID,
|
||||||
Image: conConfig.RootfsImageName,
|
Image: conConfig.RootfsImageName,
|
||||||
|
ImageID: conConfig.RootfsImageID,
|
||||||
IsInfra: conConfig.IsInfra,
|
IsInfra: conConfig.IsInfra,
|
||||||
Labels: conConfig.Labels,
|
Labels: conConfig.Labels,
|
||||||
Mounts: ctr.UserVolumes(),
|
Mounts: ctr.UserVolumes(),
|
||||||
|
@ -21,7 +21,6 @@ var _ = Describe("Podman ps", func() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
Skip(v2fail)
|
|
||||||
tempdir, err = CreateTempDirInTempDir()
|
tempdir, err = CreateTempDirInTempDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -168,6 +167,7 @@ var _ = Describe("Podman ps", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("podman ps namespace flag with go template format", func() {
|
It("podman ps namespace flag with go template format", func() {
|
||||||
|
Skip(v2fail)
|
||||||
_, ec, _ := podmanTest.RunLsContainer("test1")
|
_, ec, _ := podmanTest.RunLsContainer("test1")
|
||||||
Expect(ec).To(Equal(0))
|
Expect(ec).To(Equal(0))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user