Merge pull request #20958 from rhatdan/ps

Support podman ps --format '{{ .Label label }}'
This commit is contained in:
openshift-merge-bot[bot]
2023-12-13 14:26:17 +00:00
committed by GitHub
4 changed files with 51 additions and 1 deletions

View File

@ -189,6 +189,11 @@ func ps(cmd *cobra.Command, _ []string) error {
if err := checkFlags(cmd); err != nil { if err := checkFlags(cmd); err != nil {
return err return err
} }
if !listOpts.Pod {
listOpts.Pod = strings.Contains(listOpts.Format, ".PodName")
}
for _, f := range filters { for _, f := range filters {
split := strings.SplitN(f, "=", 2) split := strings.SplitN(f, "=", 2)
if len(split) == 1 { if len(split) == 1 {
@ -336,6 +341,11 @@ func (l psReporter) ImageID() string {
return l.ListContainer.ImageID 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 // ID returns the ID of the container
func (l psReporter) ID() string { func (l psReporter) ID() string {
if !noTrunc { if !noTrunc {

View File

@ -199,6 +199,11 @@ func (l ListPodReporter) Labels() map[string]string {
return l.ListPodsReport.Labels 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 // Networks returns the infra container network names in string format
func (l ListPodReporter) Networks() string { func (l ListPodReporter) Networks() string {
return strings.Join(l.ListPodsReport.Networks, ",") return strings.Join(l.ListPodsReport.Networks, ",")

View File

@ -90,7 +90,7 @@ Valid placeholders for the Go template are listed below:
| .Networks | Show all networks connected to the container | | .Networks | Show all networks connected to the container |
| .Pid | Process ID on host system | | .Pid | Process ID on host system |
| .Pod | Pod the container is associated with (SHA) | | .Pod | Pod the container is associated with (SHA) |
| .PodName | Seems to be empty no matter what | | .PodName | PodName of the container |
| .Ports | Exposed ports | | .Ports | Exposed ports |
| .Restarts | Display the container restart count | | .Restarts | Display the container restart count |
| .RunningFor | Time elapsed since container was started | | .RunningFor | Time elapsed since container was started |

View File

@ -194,6 +194,41 @@ EOF
is "${#lines[@]}" "1" "storage container has been removed" 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 # vim: filetype=sh