Add {{.Restarts}} to podman ps

Add Restarts column to the podman ps output to show how many times a
container was restarted based on its restart policy. This column will be
displayed when --format={{.Restarts}}.

Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
This commit is contained in:
Urvashi Mohnani 2023-03-19 20:16:38 -04:00
parent edbeee5238
commit 0fef113a4b
5 changed files with 30 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
@ -300,6 +301,7 @@ func createPsOut() ([]map[string]string, string) {
"PIDNS": "pidns",
"Pod": "pod id",
"PodName": "podname", // undo camelcase space break
"Restarts": "restarts",
"RunningFor": "running for",
"UTS": "uts",
"User": "userns",
@ -377,6 +379,10 @@ func (l psReporter) Status() string {
return state
}
func (l psReporter) Restarts() string {
return strconv.Itoa(int(l.ListContainer.Restarts))
}
func (l psReporter) RunningFor() string {
return l.CreatedHuman()
}

View File

@ -89,6 +89,7 @@ Valid placeholders for the Go template are listed below:
| .Pod | Pod the container is associated with (SHA) |
| .PodName | Seems to be empty no matter what |
| .Ports | Exposed ports |
| .Restarts | Display the container restart count |
| .RunningFor | Time elapsed since container was started |
| .Size | Size of container |
| .StartedAt | Time (epoch seconds) the container started |

View File

@ -203,7 +203,6 @@ type ContainerState struct {
// restart policy. This is NOT incremented by normal container restarts
// (only by restart policy).
RestartCount uint `json:"restartCount,omitempty"`
// StartupHCPassed indicates that the startup healthcheck has
// succeeded and the main healthcheck can begin.
StartupHCPassed bool `json:"startupHCPassed,omitempty"`
@ -730,6 +729,18 @@ func (c *Container) State() (define.ContainerStatus, error) {
return c.state.State, nil
}
func (c *Container) RestartCount() (uint, error) {
if !c.batched {
c.lock.Lock()
defer c.lock.Unlock()
if err := c.syncContainer(); err != nil {
return 0, err
}
}
return c.state.RestartCount, nil
}
// Mounted returns whether the container is mounted and the path it is mounted
// at (if it is mounted).
// If the container is not mounted, no error is returned, and the mountpoint

View File

@ -55,6 +55,10 @@ type ListContainer struct {
PodName string
// Port mappings
Ports []types.PortMapping
// Restarts is how many times the container was restarted by its
// restart policy. This is NOT incremented by normal container restarts
// (only by restart policy).
Restarts uint
// Size of the container rootfs. Requires the size boolean to be true
Size *define.ContainerSize
// Time when container started

View File

@ -145,6 +145,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
portMappings []libnetworkTypes.PortMapping
networks []string
healthStatus string
restartCount uint
)
batchErr := ctr.Batch(func(c *libpod.Container) error {
@ -193,6 +194,11 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
return err
}
restartCount, err = c.RestartCount()
if err != nil {
return err
}
if !opts.Size && !opts.Namespace {
return nil
}
@ -251,6 +257,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
StartedAt: startedTime.Unix(),
State: conState.String(),
Status: healthStatus,
Restarts: restartCount,
}
if opts.Pod && len(conConfig.Pod) > 0 {
podName, err := rt.GetPodName(conConfig.Pod)