Skip to content

Commit

Permalink
Merge pull request containers#11173 from jmguzik/pod-ps-until-filter
Browse files Browse the repository at this point in the history
Add until filter to podman pod ps
  • Loading branch information
openshift-ci[bot] authored Aug 11, 2021
2 parents 99e7ea5 + ed30ae4 commit 1968fdc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/source/markdown/podman-pod-ps.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Valid filters are listed below:
| id | [ID] Pod's ID (accepts regex) |
| name | [Name] Pod's name (accepts regex) |
| label | [Key] or [Key=Value] Label assigned to a container |
| until | Only list pods created before given timestamp |
| status | Pod's status: `stopped`, `running`, `paused`, `exited`, `dead`, `created`, `degraded` |
| network | [Network] name or full ID of network |
| ctr-names | Container name within the pod (accepts regex) |
Expand Down
13 changes: 12 additions & 1 deletion pkg/api/server/register_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ func (s *APIServer) registerPodsHandlers(r *mux.Router) error {
// - in: query
// name: filters
// type: string
// description: needs description and plumbing for filters
// description: |
// JSON encoded value of the filters (a map[string][]string) to process on the pods list. Available filters:
// - `id=<pod-id>` Matches all of pod id.
// - `label=<key>` or `label=<key>:<value>` Matches pods based on the presence of a label alone or a label and a value.
// - `name=<pod-name>` Matches all of pod name.
// - `until=<timestamp>` List pods created before this timestamp. The `<timestamp>` can be Unix timestamps, date formatted timestamps, or Go duration strings (e.g. `10m`, `1h30m`) computed relative to the daemon machine’s time.
// - `status=<pod-status>` Pod's status: `stopped`, `running`, `paused`, `exited`, `dead`, `created`, `degraded`.
// - `network=<pod-network>` Name or full ID of network.
// - `ctr-names=<pod-ctr-names>` Container name within the pod.
// - `ctr-ids=<pod-ctr-ids>` Container ID within the pod.
// - `ctr-status=<pod-ctr-status>` Container status within the pod.
// - `ctr-number=<pod-ctr-number>` Number of containers in the pod.
// responses:
// 200:
// $ref: "#/responses/ListPodsResponse"
Expand Down
11 changes: 11 additions & 0 deletions pkg/domain/filters/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ func GeneratePodFilterFunc(filter string, filterValues []string) (
labels := p.Labels()
return util.MatchLabelFilters(filterValues, labels)
}, nil
case "until":
return func(p *libpod.Pod) bool {
until, err := util.ComputeUntilTimestamp(filterValues)
if err != nil {
return false
}
if p.CreatedTime().Before(until) {
return true
}
return false
}, nil
case "network":
return func(p *libpod.Pod) bool {
infra, err := p.InfraContainer()
Expand Down
3 changes: 3 additions & 0 deletions test/apiv2/40-pods.at
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ t GET libpod/pods/json 200 \
.[0].Id=$pod_id \
.[0].Containers\|length=1

t GET libpod/pods/json?filters='{"until":["500000"]}' 200 length=0
t GET libpod/pods/json?filters='{"until":["5000000000"]}' 200 length=1

# Cannot create a dup pod with the same name
t POST "libpod/pods/create (dup pod)" name=foo 409 \
.cause="pod already exists"
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/pod_ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ var _ = Describe("Podman ps", func() {
Expect(result).Should(Exit(0))
})

It("podman pod ps --filter until", func() {
name := "mypod"
_, ec, _ := podmanTest.CreatePod(map[string][]string{"--name": {name}})
Expect(ec).To(Equal(0))

result := podmanTest.Podman([]string{"pod", "ps", "--filter", "until=50"})
result.WaitWithDefaultTimeout()
Expect(result).Should(Exit(0))
Expect(result.OutputToString()).To(Not(ContainSubstring(name)))

result = podmanTest.Podman([]string{"pod", "ps", "--filter", "until=5000000000"})
result.WaitWithDefaultTimeout()
Expect(result).Should(Exit(0))
Expect(result.OutputToString()).To(ContainSubstring(name))
})

It("podman pod ps filter name regexp", func() {
_, ec, podid := podmanTest.CreatePod(map[string][]string{"--name": {"mypod"}})
Expect(ec).To(Equal(0))
Expand Down

0 comments on commit 1968fdc

Please sign in to comment.