From f51ff77644b98a00d54c7d6a0c337f07f2bdb8db Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Sat, 9 Dec 2023 07:44:26 -0500 Subject: [PATCH] Support podman ps --format '{{ .Label label }}' 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: https://github.com/containers/podman/issues/20957 Signed-off-by: Daniel J Walsh --- cmd/podman/containers/ps.go | 10 +++++++++ cmd/podman/pods/ps.go | 5 +++++ docs/source/markdown/podman-ps.1.md | 2 +- test/system/040-ps.bats | 35 +++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/cmd/podman/containers/ps.go b/cmd/podman/containers/ps.go index 05df8faa6e..0c21a30ae8 100644 --- a/cmd/podman/containers/ps.go +++ b/cmd/podman/containers/ps.go @@ -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 { @@ -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 { diff --git a/cmd/podman/pods/ps.go b/cmd/podman/pods/ps.go index 2b9683ae43..45fbd86165 100644 --- a/cmd/podman/pods/ps.go +++ b/cmd/podman/pods/ps.go @@ -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, ",") diff --git a/docs/source/markdown/podman-ps.1.md b/docs/source/markdown/podman-ps.1.md index e2936552a7..b1967a8844 100644 --- a/docs/source/markdown/podman-ps.1.md +++ b/docs/source/markdown/podman-ps.1.md @@ -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 | diff --git a/test/system/040-ps.bats b/test/system/040-ps.bats index 80500a84c1..84ec7e2b72 100644 --- a/test/system/040-ps.bats +++ b/test/system/040-ps.bats @@ -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