Skip to content

Commit

Permalink
Add {{.Restarts}} to podman pod ps
Browse files Browse the repository at this point in the history
Add Restarts column to the podman pod ps output to show the total number
of times the containers in a pod were restarted. This is the same as the
restarts column displayed by kubernetes with kubectl get pods. This will
only be displayed when --format={{.Restarts}}.

Signed-off-by: Urvashi Mohnani <[email protected]>
  • Loading branch information
umohnani8 committed May 2, 2023
1 parent 0fef113 commit db4ad54
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
12 changes: 12 additions & 0 deletions cmd/podman/pods/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -147,6 +148,7 @@ func pods(cmd *cobra.Command, _ []string) error {
"ContainerStatuses": "STATUS",
"Cgroup": "CGROUP",
"Namespace": "NAMESPACES",
"Restarts": "RESTARTS",
})

if err := rpt.Execute(headers); err != nil {
Expand Down Expand Up @@ -178,6 +180,7 @@ func podPsFormat() string {
if !psInput.CtrStatus && !psInput.CtrNames && !psInput.CtrIds {
row = append(row, "{{.NumberOfContainers}}")
}

return "{{range . }}" + strings.Join(row, "\t") + "\n" + "{{end -}}"
}

Expand Down Expand Up @@ -265,6 +268,15 @@ func (l ListPodReporter) ContainerStatuses() string {
return strings.Join(statuses, ",")
}

// Restarts returns the total number of restarts for all the containers in the pod
func (l ListPodReporter) Restarts() string {
restarts := 0
for _, c := range l.Containers {
restarts += int(c.RestartCount)
}
return strconv.Itoa(restarts)
}

func sortPodPsOutput(sortBy string, lprs []*entities.ListPodsReport) error {
switch sortBy {
case "created":
Expand Down
29 changes: 15 additions & 14 deletions docs/source/markdown/podman-pod-ps.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,21 @@ Pretty-print containers to JSON or using a Go template

Valid placeholders for the Go template are listed below:

| **Placeholder** | **Description** |
|---------------------|----------------------------------------------------|
| .Cgroup | Cgroup path of pod |
| .ContainerIds | Comma-separated list of container IDs in the pod |
| .ContainerNames | Comma-separated list of container names in the pod |
| .ContainerStatuses | Comma-separated list of container statuses |
| .Created | Creation time of pod |
| .ID | Container ID |
| .InfraID | Pod infra container ID |
| .Labels | All the labels assigned to the pod |
| .Name | Name of pod |
| .Networks | Show all networks connected to the infra container |
| .NumberOfContainers | Show the number of containers attached to pod |
| .Status | Status of pod |
| **Placeholder** | **Description** |
|---------------------|------------------------------------------------------|
| .Cgroup | Cgroup path of pod |
| .ContainerIds | Comma-separated list of container IDs in the pod |
| .ContainerNames | Comma-separated list of container names in the pod |
| .ContainerStatuses | Comma-separated list of container statuses |
| .Created | Creation time of pod |
| .ID | Container ID |
| .InfraID | Pod infra container ID |
| .Labels | All the labels assigned to the pod |
| .Name | Name of pod |
| .Networks | Show all networks connected to the infra container |
| .NumberOfContainers | Show the number of containers attached to pod |
| .Restarts | Show the total number of container restarts in a pod |
| .Status | Status of pod |

#### **--help**, **-h**

Expand Down
7 changes: 4 additions & 3 deletions pkg/domain/entities/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ type ListPodsReport struct {
}

type ListPodContainer struct {
Id string //nolint:revive,stylecheck
Names string
Status string
Id string //nolint:revive,stylecheck
Names string
Status string
RestartCount uint
}

type PodPauseOptions struct {
Expand Down
11 changes: 8 additions & 3 deletions pkg/domain/infra/abi/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,15 @@ func (ic *ContainerEngine) listPodReportFromPod(p *libpod.Pod) (*entities.ListPo
if err != nil {
return nil, err
}
restartCount, err := c.RestartCount()
if err != nil {
return nil, err
}
lpcs[i] = &entities.ListPodContainer{
Id: c.ID(),
Names: c.Name(),
Status: state.String(),
Id: c.ID(),
Names: c.Name(),
Status: state.String(),
RestartCount: restartCount,
}
}
infraID, err := p.InfraContainerID()
Expand Down

0 comments on commit db4ad54

Please sign in to comment.