Skip to content

Commit 38baf3d

Browse files
author
Paul Holzinger
committed
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 <[email protected]>
1 parent 1242e7b commit 38baf3d

File tree

10 files changed

+115
-2
lines changed

10 files changed

+115
-2
lines changed

cmd/podman/containers/ps.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ func (l psReporter) Names() string {
392392
return l.ListContainer.Names[0]
393393
}
394394

395+
// Networks returns the container network names in string format
396+
func (l psReporter) Networks() string {
397+
return strings.Join(l.ListContainer.Networks, ",")
398+
}
399+
395400
// Ports converts from Portmappings to the string form
396401
// required by ps
397402
func (l psReporter) Ports() string {

cmd/podman/pods/ps.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ func (l ListPodReporter) Labels() map[string]string {
191191
return l.ListPodsReport.Labels
192192
}
193193

194+
// Networks returns the infra container network names in string format
195+
func (l ListPodReporter) Networks() string {
196+
return strings.Join(l.ListPodsReport.Networks, ",")
197+
}
198+
194199
// NumberOfContainers returns an int representation for
195200
// the number of containers belonging to the pod
196201
func (l ListPodReporter) NumberOfContainers() int {

docs/source/markdown/podman-pod-ps.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Valid placeholders for the Go template are listed below:
7272
| .Cgroup | Cgroup path of pod |
7373
| .Created | Creation time of pod |
7474
| .InfraID | Pod infra container ID |
75+
| .Networks | Show all networks connected to the infra container |
76+
7577
#### **--sort**
7678

7779
Sort by created, ID, name, status, or number of containers

docs/source/markdown/podman-ps.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Valid placeholders for the Go template are listed below:
8080
| .Ports | Exposed ports |
8181
| .Size | Size of container |
8282
| .Names | Name of container |
83+
| .Networks | Show all networks connected to the container |
8384
| .Labels | All the labels assigned to the container |
8485
| .Mounts | Volumes mounted in the container |
8586

pkg/domain/entities/container_ps.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ type ListContainer struct {
4343
// Namespaces the container belongs to. Requires the
4444
// namespace boolean to be true
4545
Namespaces ListContainerNamespaces
46+
// The network names assigned to the container
47+
Networks []string
4648
// The process id of the container
4749
Pid int
4850
// If the container is part of Pod, the Pod ID. Requires the pod

pkg/domain/entities/pods.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ type ListPodsReport struct {
2828
InfraId string //nolint
2929
Name string
3030
Namespace string
31-
Status string
32-
Labels map[string]string
31+
// Network names connected to infra container
32+
Networks []string
33+
Status string
34+
Labels map[string]string
3335
}
3436

3537
type ListPodContainer struct {

pkg/domain/infra/abi/pods.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,17 @@ func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOpti
333333
if err != nil {
334334
return nil, err
335335
}
336+
networks := []string{}
337+
if len(infraID) > 0 {
338+
infra, err := p.InfraContainer()
339+
if err != nil {
340+
return nil, err
341+
}
342+
networks, _, err = infra.Networks()
343+
if err != nil {
344+
return nil, err
345+
}
346+
}
336347
reports = append(reports, &entities.ListPodsReport{
337348
Cgroup: p.CgroupParent(),
338349
Containers: lpcs,
@@ -341,6 +352,7 @@ func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOpti
341352
InfraId: infraID,
342353
Name: p.Name(),
343354
Namespace: p.Namespace(),
355+
Networks: networks,
344356
Status: status,
345357
Labels: p.Labels(),
346358
})

pkg/ps/ps.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
178178
return entities.ListContainer{}, err
179179
}
180180

181+
networks, _, err := ctr.Networks()
182+
if err != nil {
183+
return entities.ListContainer{}, err
184+
}
185+
181186
ps := entities.ListContainer{
182187
AutoRemove: ctr.AutoRemove(),
183188
Command: conConfig.Command,
@@ -192,6 +197,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
192197
Labels: conConfig.Labels,
193198
Mounts: ctr.UserVolumes(),
194199
Names: []string{conConfig.Name},
200+
Networks: networks,
195201
Pid: pid,
196202
Pod: conConfig.Pod,
197203
Ports: portMappings,

test/e2e/pod_ps_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,45 @@ var _ = Describe("Podman ps", func() {
305305
Expect(session.OutputToString()).To(Not(ContainSubstring(podWithoutNet)))
306306
})
307307

308+
It("podman pod ps --format networks", func() {
309+
session := podmanTest.Podman([]string{"pod", "create"})
310+
session.WaitWithDefaultTimeout()
311+
Expect(session.ExitCode()).To(BeZero())
312+
313+
session = podmanTest.Podman([]string{"pod", "ps", "--format", "{{ .Networks }}"})
314+
session.WaitWithDefaultTimeout()
315+
Expect(session.ExitCode()).To(BeZero())
316+
if isRootless() {
317+
// rootless container don't have a network by default
318+
Expect(session.OutputToString()).To(Equal(""))
319+
} else {
320+
// default network name is podman
321+
Expect(session.OutputToString()).To(Equal("podman"))
322+
}
323+
324+
net1 := stringid.GenerateNonCryptoID()
325+
session = podmanTest.Podman([]string{"network", "create", net1})
326+
session.WaitWithDefaultTimeout()
327+
Expect(session.ExitCode()).To(BeZero())
328+
defer podmanTest.removeCNINetwork(net1)
329+
net2 := stringid.GenerateNonCryptoID()
330+
session = podmanTest.Podman([]string{"network", "create", net2})
331+
session.WaitWithDefaultTimeout()
332+
Expect(session.ExitCode()).To(BeZero())
333+
defer podmanTest.removeCNINetwork(net2)
334+
335+
session = podmanTest.Podman([]string{"pod", "create", "--network", net1 + "," + net2})
336+
session.WaitWithDefaultTimeout()
337+
Expect(session.ExitCode()).To(BeZero())
338+
pid := session.OutputToString()
339+
340+
session = podmanTest.Podman([]string{"pod", "ps", "--format", "{{ .Networks }}", "--filter", "id=" + pid})
341+
session.WaitWithDefaultTimeout()
342+
Expect(session.ExitCode()).To(BeZero())
343+
// the output is not deterministic so check both possible orders
344+
Expect(session.OutputToString()).To(Or(Equal(net1+","+net2), Equal(net2+","+net1)))
345+
})
346+
308347
It("pod no infra should ps", func() {
309348
session := podmanTest.Podman([]string{"pod", "create", "--infra=false"})
310349
session.WaitWithDefaultTimeout()

test/e2e/ps_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,4 +749,43 @@ var _ = Describe("Podman ps", func() {
749749
Expect(session.OutputToString()).To(Not(ContainSubstring(ctrWithoutNet)))
750750
})
751751

752+
It("podman ps --format networks", func() {
753+
session := podmanTest.Podman([]string{"create", ALPINE})
754+
session.WaitWithDefaultTimeout()
755+
Expect(session.ExitCode()).To(BeZero())
756+
757+
session = podmanTest.Podman([]string{"ps", "--all", "--format", "{{ .Networks }}"})
758+
session.WaitWithDefaultTimeout()
759+
Expect(session.ExitCode()).To(BeZero())
760+
if isRootless() {
761+
// rootless container don't have a network by default
762+
Expect(session.OutputToString()).To(Equal(""))
763+
} else {
764+
// default network name is podman
765+
Expect(session.OutputToString()).To(Equal("podman"))
766+
}
767+
768+
net1 := stringid.GenerateNonCryptoID()
769+
session = podmanTest.Podman([]string{"network", "create", net1})
770+
session.WaitWithDefaultTimeout()
771+
Expect(session.ExitCode()).To(BeZero())
772+
defer podmanTest.removeCNINetwork(net1)
773+
net2 := stringid.GenerateNonCryptoID()
774+
session = podmanTest.Podman([]string{"network", "create", net2})
775+
session.WaitWithDefaultTimeout()
776+
Expect(session.ExitCode()).To(BeZero())
777+
defer podmanTest.removeCNINetwork(net2)
778+
779+
session = podmanTest.Podman([]string{"create", "--network", net1 + "," + net2, ALPINE})
780+
session.WaitWithDefaultTimeout()
781+
Expect(session.ExitCode()).To(BeZero())
782+
cid := session.OutputToString()
783+
784+
session = podmanTest.Podman([]string{"ps", "--all", "--format", "{{ .Networks }}", "--filter", "id=" + cid})
785+
session.WaitWithDefaultTimeout()
786+
Expect(session.ExitCode()).To(BeZero())
787+
// the output is not deterministic so check both possible orders
788+
Expect(session.OutputToString()).To(Or(Equal(net1+","+net2), Equal(net2+","+net1)))
789+
})
790+
752791
})

0 commit comments

Comments
 (0)