Skip to content

Commit 4582b61

Browse files
Merge pull request #20958 from rhatdan/ps
Support podman ps --format '{{ .Label label }}'
2 parents bc5dc03 + f51ff77 commit 4582b61

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

cmd/podman/containers/ps.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ func ps(cmd *cobra.Command, _ []string) error {
189189
if err := checkFlags(cmd); err != nil {
190190
return err
191191
}
192+
193+
if !listOpts.Pod {
194+
listOpts.Pod = strings.Contains(listOpts.Format, ".PodName")
195+
}
196+
192197
for _, f := range filters {
193198
split := strings.SplitN(f, "=", 2)
194199
if len(split) == 1 {
@@ -336,6 +341,11 @@ func (l psReporter) ImageID() string {
336341
return l.ListContainer.ImageID
337342
}
338343

344+
// Labels returns a map of the pod's labels
345+
func (l psReporter) Label(name string) string {
346+
return l.ListContainer.Labels[name]
347+
}
348+
339349
// ID returns the ID of the container
340350
func (l psReporter) ID() string {
341351
if !noTrunc {

cmd/podman/pods/ps.go

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

202+
// Label returns a map of the pod's labels
203+
func (l ListPodReporter) Label(name string) string {
204+
return l.ListPodsReport.Labels[name]
205+
}
206+
202207
// Networks returns the infra container network names in string format
203208
func (l ListPodReporter) Networks() string {
204209
return strings.Join(l.ListPodsReport.Networks, ",")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Valid placeholders for the Go template are listed below:
9090
| .Networks | Show all networks connected to the container |
9191
| .Pid | Process ID on host system |
9292
| .Pod | Pod the container is associated with (SHA) |
93-
| .PodName | Seems to be empty no matter what |
93+
| .PodName | PodName of the container |
9494
| .Ports | Exposed ports |
9595
| .Restarts | Display the container restart count |
9696
| .RunningFor | Time elapsed since container was started |

test/system/040-ps.bats

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,41 @@ EOF
194194
is "${#lines[@]}" "1" "storage container has been removed"
195195
}
196196

197+
@test "podman ps --format label" {
198+
rand_value=$(random_string 10)
197199

200+
run_podman run -d --label mylabel=$rand_value $IMAGE sleep inf
201+
cid=$output
202+
is "$cid" "[0-9a-f]\{64\}$"
203+
204+
run_podman ps --format '{{ .Label "mylabel" }}'
205+
is "$output" "$rand_value"
206+
207+
run_podman rm -t 0 -f $cid
208+
}
209+
210+
@test "podman pod ps --format label" {
211+
rand_value=$(random_string 10)
212+
213+
run_podman pod create --label mylabel=${rand_value} test
214+
215+
run_podman pod ps --format '{{ .Label "mylabel" }}'
216+
is "$output" "$rand_value"
217+
218+
run_podman pod rm -t 0 -f test
219+
}
220+
221+
@test "podman ps --format PodName" {
222+
rand_value=$(random_string 10)
223+
224+
run_podman run -d --pod new:${rand_value} --label mylabel=$rand_value $IMAGE sleep inf
225+
cid=$output
226+
is "$cid" "[0-9a-f]\{64\}$"
227+
228+
run_podman ps --format '{{ .PodName }}'
229+
is "$output" ".*$rand_value"
230+
231+
run_podman rm -t 0 -f $cid
232+
}
198233

199234
# vim: filetype=sh

0 commit comments

Comments
 (0)