Add Networks format placeholder to podman ps and pod ps

`podman ps --format {{.Networks}}` will show all connected networks for
this container. For `pod ps` it will show the infra container networks.

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
This commit is contained in:
Paul Holzinger 2021-01-09 16:54:41 +01:00
parent 1242e7b7a6
commit 38baf3d5e2
10 changed files with 115 additions and 2 deletions

View File

@ -392,6 +392,11 @@ func (l psReporter) Names() string {
return l.ListContainer.Names[0]
}
// Networks returns the container network names in string format
func (l psReporter) Networks() string {
return strings.Join(l.ListContainer.Networks, ",")
}
// Ports converts from Portmappings to the string form
// required by ps
func (l psReporter) Ports() string {

View File

@ -191,6 +191,11 @@ func (l ListPodReporter) Labels() map[string]string {
return l.ListPodsReport.Labels
}
// Networks returns the infra container network names in string format
func (l ListPodReporter) Networks() string {
return strings.Join(l.ListPodsReport.Networks, ",")
}
// NumberOfContainers returns an int representation for
// the number of containers belonging to the pod
func (l ListPodReporter) NumberOfContainers() int {

View File

@ -72,6 +72,8 @@ Valid placeholders for the Go template are listed below:
| .Cgroup | Cgroup path of pod |
| .Created | Creation time of pod |
| .InfraID | Pod infra container ID |
| .Networks | Show all networks connected to the infra container |
#### **--sort**
Sort by created, ID, name, status, or number of containers

View File

@ -80,6 +80,7 @@ Valid placeholders for the Go template are listed below:
| .Ports | Exposed ports |
| .Size | Size of container |
| .Names | Name of container |
| .Networks | Show all networks connected to the container |
| .Labels | All the labels assigned to the container |
| .Mounts | Volumes mounted in the container |

View File

@ -43,6 +43,8 @@ type ListContainer struct {
// Namespaces the container belongs to. Requires the
// namespace boolean to be true
Namespaces ListContainerNamespaces
// The network names assigned to the container
Networks []string
// The process id of the container
Pid int
// If the container is part of Pod, the Pod ID. Requires the pod

View File

@ -28,8 +28,10 @@ type ListPodsReport struct {
InfraId string //nolint
Name string
Namespace string
Status string
Labels map[string]string
// Network names connected to infra container
Networks []string
Status string
Labels map[string]string
}
type ListPodContainer struct {

View File

@ -333,6 +333,17 @@ func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOpti
if err != nil {
return nil, err
}
networks := []string{}
if len(infraID) > 0 {
infra, err := p.InfraContainer()
if err != nil {
return nil, err
}
networks, _, err = infra.Networks()
if err != nil {
return nil, err
}
}
reports = append(reports, &entities.ListPodsReport{
Cgroup: p.CgroupParent(),
Containers: lpcs,
@ -341,6 +352,7 @@ func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOpti
InfraId: infraID,
Name: p.Name(),
Namespace: p.Namespace(),
Networks: networks,
Status: status,
Labels: p.Labels(),
})

View File

@ -178,6 +178,11 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
return entities.ListContainer{}, err
}
networks, _, err := ctr.Networks()
if err != nil {
return entities.ListContainer{}, err
}
ps := entities.ListContainer{
AutoRemove: ctr.AutoRemove(),
Command: conConfig.Command,
@ -192,6 +197,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
Labels: conConfig.Labels,
Mounts: ctr.UserVolumes(),
Names: []string{conConfig.Name},
Networks: networks,
Pid: pid,
Pod: conConfig.Pod,
Ports: portMappings,

View File

@ -305,6 +305,45 @@ var _ = Describe("Podman ps", func() {
Expect(session.OutputToString()).To(Not(ContainSubstring(podWithoutNet)))
})
It("podman pod ps --format networks", func() {
session := podmanTest.Podman([]string{"pod", "create"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
session = podmanTest.Podman([]string{"pod", "ps", "--format", "{{ .Networks }}"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
if isRootless() {
// rootless container don't have a network by default
Expect(session.OutputToString()).To(Equal(""))
} else {
// default network name is podman
Expect(session.OutputToString()).To(Equal("podman"))
}
net1 := stringid.GenerateNonCryptoID()
session = podmanTest.Podman([]string{"network", "create", net1})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
defer podmanTest.removeCNINetwork(net1)
net2 := stringid.GenerateNonCryptoID()
session = podmanTest.Podman([]string{"network", "create", net2})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
defer podmanTest.removeCNINetwork(net2)
session = podmanTest.Podman([]string{"pod", "create", "--network", net1 + "," + net2})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
pid := session.OutputToString()
session = podmanTest.Podman([]string{"pod", "ps", "--format", "{{ .Networks }}", "--filter", "id=" + pid})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
// the output is not deterministic so check both possible orders
Expect(session.OutputToString()).To(Or(Equal(net1+","+net2), Equal(net2+","+net1)))
})
It("pod no infra should ps", func() {
session := podmanTest.Podman([]string{"pod", "create", "--infra=false"})
session.WaitWithDefaultTimeout()

View File

@ -749,4 +749,43 @@ var _ = Describe("Podman ps", func() {
Expect(session.OutputToString()).To(Not(ContainSubstring(ctrWithoutNet)))
})
It("podman ps --format networks", func() {
session := podmanTest.Podman([]string{"create", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
session = podmanTest.Podman([]string{"ps", "--all", "--format", "{{ .Networks }}"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
if isRootless() {
// rootless container don't have a network by default
Expect(session.OutputToString()).To(Equal(""))
} else {
// default network name is podman
Expect(session.OutputToString()).To(Equal("podman"))
}
net1 := stringid.GenerateNonCryptoID()
session = podmanTest.Podman([]string{"network", "create", net1})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
defer podmanTest.removeCNINetwork(net1)
net2 := stringid.GenerateNonCryptoID()
session = podmanTest.Podman([]string{"network", "create", net2})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
defer podmanTest.removeCNINetwork(net2)
session = podmanTest.Podman([]string{"create", "--network", net1 + "," + net2, ALPINE})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
cid := session.OutputToString()
session = podmanTest.Podman([]string{"ps", "--all", "--format", "{{ .Networks }}", "--filter", "id=" + cid})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
// the output is not deterministic so check both possible orders
Expect(session.OutputToString()).To(Or(Equal(net1+","+net2), Equal(net2+","+net1)))
})
})