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 <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2023-01-25 09:59:16 +01:00
parent c35e74f4cc
commit 9d1c153cfc
2 changed files with 14 additions and 10 deletions

View File

@ -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 {

View File

@ -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
}