Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ps: query health check in batch mode #17211

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the syncContainer() here should be moved in the if !batched branch just like the other public accessor functions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sure there's more to polish in the health-check code but it's out of scope of this PR.

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