Skip to content

Commit

Permalink
Support podman ps --format '{{ .Label label }}'
Browse files Browse the repository at this point in the history
Also Support for podman pod ps  --format '{{ .Label label }}'

Finally fix support for --format '{{ .Podname }}'
   When user specifies .Podname this implies --pod was passed.

Fixes: containers#20957

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Dec 13, 2023
1 parent a5c8bb2 commit f51ff77
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cmd/podman/containers/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ func ps(cmd *cobra.Command, _ []string) error {
if err := checkFlags(cmd); err != nil {
return err
}

if !listOpts.Pod {
listOpts.Pod = strings.Contains(listOpts.Format, ".PodName")
}

for _, f := range filters {
split := strings.SplitN(f, "=", 2)
if len(split) == 1 {
Expand Down Expand Up @@ -336,6 +341,11 @@ func (l psReporter) ImageID() string {
return l.ListContainer.ImageID
}

// Labels returns a map of the pod's labels
func (l psReporter) Label(name string) string {
return l.ListContainer.Labels[name]
}

// ID returns the ID of the container
func (l psReporter) ID() string {
if !noTrunc {
Expand Down
5 changes: 5 additions & 0 deletions cmd/podman/pods/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ func (l ListPodReporter) Labels() map[string]string {
return l.ListPodsReport.Labels
}

// Label returns a map of the pod's labels
func (l ListPodReporter) Label(name string) string {
return l.ListPodsReport.Labels[name]
}

// Networks returns the infra container network names in string format
func (l ListPodReporter) Networks() string {
return strings.Join(l.ListPodsReport.Networks, ",")
Expand Down
2 changes: 1 addition & 1 deletion docs/source/markdown/podman-ps.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Valid placeholders for the Go template are listed below:
| .Networks | Show all networks connected to the container |
| .Pid | Process ID on host system |
| .Pod | Pod the container is associated with (SHA) |
| .PodName | Seems to be empty no matter what |
| .PodName | PodName of the container |
| .Ports | Exposed ports |
| .Restarts | Display the container restart count |
| .RunningFor | Time elapsed since container was started |
Expand Down
35 changes: 35 additions & 0 deletions test/system/040-ps.bats
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,41 @@ EOF
is "${#lines[@]}" "1" "storage container has been removed"
}

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

run_podman run -d --label mylabel=$rand_value $IMAGE sleep inf
cid=$output
is "$cid" "[0-9a-f]\{64\}$"

run_podman ps --format '{{ .Label "mylabel" }}'
is "$output" "$rand_value"

run_podman rm -t 0 -f $cid
}

@test "podman pod ps --format label" {
rand_value=$(random_string 10)

run_podman pod create --label mylabel=${rand_value} test

run_podman pod ps --format '{{ .Label "mylabel" }}'
is "$output" "$rand_value"

run_podman pod rm -t 0 -f test
}

@test "podman ps --format PodName" {
rand_value=$(random_string 10)

run_podman run -d --pod new:${rand_value} --label mylabel=$rand_value $IMAGE sleep inf
cid=$output
is "$cid" "[0-9a-f]\{64\}$"

run_podman ps --format '{{ .PodName }}'
is "$output" ".*$rand_value"

run_podman rm -t 0 -f $cid
}

# vim: filetype=sh

0 comments on commit f51ff77

Please sign in to comment.