Skip to content

Commit 1242e7b

Browse files
author
Paul Holzinger
committed
Add network filter for podman ps and pod ps
Allow to filter on the network name or full id. For pod ps it will filter on the infra container networks. Signed-off-by: Paul Holzinger <[email protected]>
1 parent 49db79e commit 1242e7b

File tree

7 files changed

+101
-4
lines changed

7 files changed

+101
-4
lines changed

cmd/podman/common/completion.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -982,9 +982,10 @@ func AutocompletePsFilters(cmd *cobra.Command, args []string, toComplete string)
982982
return []string{define.HealthCheckHealthy,
983983
define.HealthCheckUnhealthy}, cobra.ShellCompDirectiveNoFileComp
984984
},
985-
"label=": nil,
986-
"exited=": nil,
987-
"until=": nil,
985+
"network=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s) },
986+
"label=": nil,
987+
"exited=": nil,
988+
"until=": nil,
988989
}
989990
return completeKeyValues(toComplete, kv)
990991
}
@@ -1004,7 +1005,8 @@ func AutocompletePodPsFilters(cmd *cobra.Command, args []string, toComplete stri
10041005
"ctr-status=": func(_ string) ([]string, cobra.ShellCompDirective) {
10051006
return containerStatuses, cobra.ShellCompDirectiveNoFileComp
10061007
},
1007-
"label=": nil,
1008+
"network=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s) },
1009+
"label=": nil,
10081010
}
10091011
return completeKeyValues(toComplete, kv)
10101012
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Valid filters are listed below:
9393
| name | [Name] Pod's name (accepts regex) |
9494
| label | [Key] or [Key=Value] Label assigned to a container |
9595
| status | Pod's status: `stopped`, `running`, `paused`, `exited`, `dead`, `created`, `degraded` |
96+
| network | [Network] name or full ID of network |
9697
| ctr-names | Container name within the pod (accepts regex) |
9798
| ctr-ids | Container ID within the pod (accepts regex) |
9899
| ctr-status | Container status within the pod |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Valid filters are listed below:
5858
| volume | [VolumeName] or [MountpointDestination] Volume mounted in container |
5959
| health | [Status] healthy or unhealthy |
6060
| pod | [Pod] name or full or partial ID of pod |
61+
| network | [Network] name or full ID of network |
6162

6263

6364
#### **--format**=*format*

pkg/domain/filters/containers.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/containers/podman/v2/libpod"
99
"github.com/containers/podman/v2/libpod/define"
10+
"github.com/containers/podman/v2/libpod/network"
1011
"github.com/containers/podman/v2/pkg/timetype"
1112
"github.com/containers/podman/v2/pkg/util"
1213
"github.com/pkg/errors"
@@ -233,6 +234,24 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
233234
}
234235
return false
235236
}, nil
237+
case "network":
238+
return func(c *libpod.Container) bool {
239+
networks, _, err := c.Networks()
240+
// if err or no networks, quick out
241+
if err != nil || len(networks) == 0 {
242+
return false
243+
}
244+
for _, net := range networks {
245+
netID := network.GetNetworkID(net)
246+
for _, val := range filterValues {
247+
// match by network name or id
248+
if val == net || val == netID {
249+
return true
250+
}
251+
}
252+
}
253+
return false
254+
}, nil
236255
}
237256
return nil, errors.Errorf("%s is an invalid filter", filter)
238257
}

pkg/domain/filters/pods.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/containers/podman/v2/libpod"
88
"github.com/containers/podman/v2/libpod/define"
9+
"github.com/containers/podman/v2/libpod/network"
910
"github.com/containers/podman/v2/pkg/util"
1011
"github.com/pkg/errors"
1112
)
@@ -134,6 +135,29 @@ func GeneratePodFilterFunc(filter string, filterValues []string) (
134135
}
135136
return true
136137
}, nil
138+
case "network":
139+
return func(p *libpod.Pod) bool {
140+
infra, err := p.InfraContainer()
141+
// no infra, quick out
142+
if err != nil {
143+
return false
144+
}
145+
networks, _, err := infra.Networks()
146+
// if err or no networks, quick out
147+
if err != nil || len(networks) == 0 {
148+
return false
149+
}
150+
for _, net := range networks {
151+
netID := network.GetNetworkID(net)
152+
for _, val := range filterValues {
153+
// match by network name or id
154+
if val == net || val == netID {
155+
return true
156+
}
157+
}
158+
}
159+
return false
160+
}, nil
137161
}
138162
return nil, errors.Errorf("%s is an invalid filter", filter)
139163
}

test/e2e/pod_ps_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sort"
77

88
. "github.com/containers/podman/v2/test/utils"
9+
"github.com/containers/storage/pkg/stringid"
910
. "github.com/onsi/ginkgo"
1011
. "github.com/onsi/gomega"
1112
. "github.com/onsi/gomega/gexec"
@@ -280,6 +281,30 @@ var _ = Describe("Podman ps", func() {
280281
Expect(session.OutputToString()).To(Not(ContainSubstring(podid3)))
281282
})
282283

284+
It("podman pod ps filter network", func() {
285+
net := stringid.GenerateNonCryptoID()
286+
session := podmanTest.Podman([]string{"network", "create", net})
287+
session.WaitWithDefaultTimeout()
288+
Expect(session.ExitCode()).To(BeZero())
289+
defer podmanTest.removeCNINetwork(net)
290+
291+
session = podmanTest.Podman([]string{"pod", "create", "--network", net})
292+
session.WaitWithDefaultTimeout()
293+
Expect(session.ExitCode()).To(BeZero())
294+
podWithNet := session.OutputToString()
295+
296+
session = podmanTest.Podman([]string{"pod", "create"})
297+
session.WaitWithDefaultTimeout()
298+
Expect(session.ExitCode()).To(BeZero())
299+
podWithoutNet := session.OutputToString()
300+
301+
session = podmanTest.Podman([]string{"pod", "ps", "--no-trunc", "--filter", "network=" + net})
302+
session.WaitWithDefaultTimeout()
303+
Expect(session.ExitCode()).To(BeZero())
304+
Expect(session.OutputToString()).To(ContainSubstring(podWithNet))
305+
Expect(session.OutputToString()).To(Not(ContainSubstring(podWithoutNet)))
306+
})
307+
283308
It("pod no infra should ps", func() {
284309
session := podmanTest.Podman([]string{"pod", "create", "--infra=false"})
285310
session.WaitWithDefaultTimeout()

test/e2e/ps_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
. "github.com/containers/podman/v2/test/utils"
11+
"github.com/containers/storage/pkg/stringid"
1112
"github.com/docker/go-units"
1213
. "github.com/onsi/ginkgo"
1314
. "github.com/onsi/gomega"
@@ -724,4 +725,28 @@ var _ = Describe("Podman ps", func() {
724725

725726
})
726727

728+
It("podman ps filter network", func() {
729+
net := stringid.GenerateNonCryptoID()
730+
session := podmanTest.Podman([]string{"network", "create", net})
731+
session.WaitWithDefaultTimeout()
732+
Expect(session.ExitCode()).To(BeZero())
733+
defer podmanTest.removeCNINetwork(net)
734+
735+
session = podmanTest.Podman([]string{"create", "--network", net, ALPINE})
736+
session.WaitWithDefaultTimeout()
737+
Expect(session.ExitCode()).To(BeZero())
738+
ctrWithNet := session.OutputToString()
739+
740+
session = podmanTest.Podman([]string{"create", ALPINE})
741+
session.WaitWithDefaultTimeout()
742+
Expect(session.ExitCode()).To(BeZero())
743+
ctrWithoutNet := session.OutputToString()
744+
745+
session = podmanTest.Podman([]string{"ps", "--all", "--no-trunc", "--filter", "network=" + net})
746+
session.WaitWithDefaultTimeout()
747+
Expect(session.ExitCode()).To(BeZero())
748+
Expect(session.OutputToString()).To(ContainSubstring(ctrWithNet))
749+
Expect(session.OutputToString()).To(Not(ContainSubstring(ctrWithoutNet)))
750+
})
751+
727752
})

0 commit comments

Comments
 (0)