From 9d1c153cfcc104258f645a82e6233aec1caa89e7 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Wed, 25 Jan 2023 09:59:16 +0100 Subject: [PATCH] ps: query health check in batch mode Also do not return (and immediately suppress) an error if no health check is defined for a given container. Makes listing 100 containers around 10 percent faster. [NO NEW TESTS NEEDED] Signed-off-by: Valentin Rothberg --- libpod/healthcheck.go | 11 +++++++---- pkg/ps/ps.go | 13 +++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/libpod/healthcheck.go b/libpod/healthcheck.go index d10ba8f3ba..63f22ef9fb 100644 --- a/libpod/healthcheck.go +++ b/libpod/healthcheck.go @@ -407,10 +407,13 @@ func (c *Container) getHealthCheckLog() (define.HealthCheckResults, error) { return healthCheck, nil } -// HealthCheckStatus returns the current state of a container with a healthcheck +// HealthCheckStatus returns the current state of a container with a healthcheck. +// Returns an empty string if no health check is defined for the container. func (c *Container) HealthCheckStatus() (string, error) { - c.lock.Lock() - defer c.lock.Unlock() + if !c.batched { + c.lock.Lock() + defer c.lock.Unlock() + } return c.healthCheckStatus() } @@ -418,7 +421,7 @@ func (c *Container) HealthCheckStatus() (string, error) { // This function does not lock the container. func (c *Container) healthCheckStatus() (string, error) { if !c.HasHealthCheck() { - return "", fmt.Errorf("container %s has no defined healthcheck", c.ID()) + return "", nil } if err := c.syncContainer(); err != nil { diff --git a/pkg/ps/ps.go b/pkg/ps/ps.go index a710c175c0..b93f1eb745 100644 --- a/pkg/ps/ps.go +++ b/pkg/ps/ps.go @@ -137,6 +137,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities cgroup, ipc, mnt, net, pidns, user, uts string portMappings []libnetworkTypes.PortMapping networks []string + healthStatus string ) batchErr := ctr.Batch(func(c *libpod.Container) error { @@ -180,6 +181,11 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities return err } + healthStatus, err = c.HealthCheckStatus() + if err != nil { + return err + } + if !opts.Size && !opts.Namespace { return nil } @@ -237,6 +243,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities Size: size, StartedAt: startedTime.Unix(), State: conState.String(), + Status: healthStatus, } if opts.Pod && len(conConfig.Pod) > 0 { podName, err := rt.GetName(conConfig.Pod) @@ -261,12 +268,6 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities } } - if hc, err := ctr.HealthCheckStatus(); err == nil { - ps.Status = hc - } else { - logrus.Debug(err) - } - return ps, nil }