libpod: make healthcheck events more efficient

We already know the status of the healthcheck in the caller so calling
healthCheckStatus() just make the event code sync the container state
and reread the healthcheck file for no reason.

It is much better to directly pass the status down to the event call.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2024-04-18 17:58:23 +02:00
parent db93e2cf2b
commit b026e1d635
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
3 changed files with 12 additions and 15 deletions

View File

@ -28,19 +28,27 @@ func (r *Runtime) newEventer() (events.Eventer, error) {
// newContainerEvent creates a new event based on a container
func (c *Container) newContainerEvent(status events.Status) {
if err := c.newContainerEventWithInspectData(status, false); err != nil {
if err := c.newContainerEventWithInspectData(status, "", false); err != nil {
logrus.Errorf("Unable to write container event: %v", err)
}
}
// newContainerHealthCheckEvent creates a new healthcheck event with the given status
func (c *Container) newContainerHealthCheckEvent(healthStatus string) {
if err := c.newContainerEventWithInspectData(events.HealthStatus, healthStatus, false); err != nil {
logrus.Errorf("Unable to write container event: %v", err)
}
}
// newContainerEventWithInspectData creates a new event and sets the
// ContainerInspectData field if inspectData is set.
func (c *Container) newContainerEventWithInspectData(status events.Status, inspectData bool) error {
func (c *Container) newContainerEventWithInspectData(status events.Status, healthStatus string, inspectData bool) error {
e := events.NewEvent(status)
e.ID = c.ID()
e.Name = c.Name()
e.Image = c.config.RootfsImageName
e.Type = events.Container
e.HealthStatus = healthStatus
e.Details = events.Details{
PodID: c.PodID(),
@ -65,16 +73,6 @@ func (c *Container) newContainerEventWithInspectData(status events.Status, inspe
}
}
// if the current event is a HealthStatus event, we need to get the current
// status of the container to pass to the event
if status == events.HealthStatus {
containerHealthStatus, err := c.healthCheckStatus()
if err != nil {
e.HealthStatus = err.Error()
}
e.HealthStatus = containerHealthStatus
}
if status == events.Remove {
exitCode, err := c.runtime.state.GetContainerExitCode(c.ID())
if err == nil {

View File

@ -14,7 +14,6 @@ import (
"time"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/libpod/events"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
@ -178,7 +177,7 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
if hcResult == define.HealthCheckNotDefined || hcResult == define.HealthCheckInternalError {
return hcResult, logStatus, hcErr
}
c.newContainerEvent(events.HealthStatus)
c.newContainerHealthCheckEvent(logStatus)
return hcResult, logStatus, hcErr
}

View File

@ -585,7 +585,7 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
}
if ctr.runtime.config.Engine.EventsContainerCreateInspectData {
if err := ctr.newContainerEventWithInspectData(events.Create, true); err != nil {
if err := ctr.newContainerEventWithInspectData(events.Create, "", true); err != nil {
return nil, err
}
} else {