Skip to content

Commit

Permalink
Merge pull request #17211 from vrothberg/ps-health
Browse files Browse the repository at this point in the history
ps: query health check in batch mode
  • Loading branch information
openshift-merge-robot authored Jan 25, 2023
2 parents 64ea213 + 9d1c153 commit 4cd7f63
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
11 changes: 7 additions & 4 deletions libpod/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,18 +407,21 @@ 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()
}

// Internal function to return the current state of a container with a healthcheck.
// 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 {
Expand Down
13 changes: 7 additions & 6 deletions pkg/ps/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit 4cd7f63

Please sign in to comment.