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

Fix: Do not emit health_status event for each health check attempt. #24005

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
19 changes: 12 additions & 7 deletions libpod/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
}

hcl := newHealthCheckLog(timeStart, timeEnd, returnCode, eventLog)
logStatus, err := c.updateHealthCheckLog(hcl, inStartPeriod, isStartup)
logStatus, isHCStausChanged, err := c.updateHealthCheckLog(hcl, inStartPeriod, isStartup)
if err != nil {
return hcResult, "", fmt.Errorf("unable to update health check log %s for %s: %w", c.healthCheckLogPath(), c.ID(), err)
}
Expand All @@ -164,7 +164,7 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
if hcResult == define.HealthCheckNotDefined || hcResult == define.HealthCheckInternalError {
return hcResult, logStatus, hcErr
}
if c.runtime.config.Engine.HealthcheckEvents {
if c.runtime.config.Engine.HealthcheckEvents && isHCStausChanged {
c.newContainerHealthCheckEvent(logStatus)
}

Expand Down Expand Up @@ -365,20 +365,21 @@ func (c *Container) isUnhealthy() (bool, error) {
}

// UpdateHealthCheckLog parses the health check results and writes the log
func (c *Container) updateHealthCheckLog(hcl define.HealthCheckLog, inStartPeriod, isStartup bool) (string, error) {
func (c *Container) updateHealthCheckLog(hcl define.HealthCheckLog, inStartPeriod, isStartup bool) (string, bool, error) {
c.lock.Lock()
defer c.lock.Unlock()

// If we are playing a kube yaml then let's honor the start period time for
// both failing and succeeding cases to match kube behavior.
// So don't update the health check log till the start period is over
if _, ok := c.config.Spec.Annotations[define.KubeHealthCheckAnnotation]; ok && inStartPeriod && !isStartup {
return "", nil
return "", false, nil
}

healthCheck, err := c.getHealthCheckLog()
oldHCStatus := healthCheck.Status
if err != nil {
return "", err
return "", false, err
}
if hcl.ExitCode == 0 {
// set status to healthy, reset failing state to 0
Expand All @@ -397,15 +398,19 @@ func (c *Container) updateHealthCheckLog(hcl define.HealthCheckLog, inStartPerio
}
}
}
isHCStausChanged := oldHCStatus != healthCheck.Status
healthCheck.Log = append(healthCheck.Log, hcl)
if len(healthCheck.Log) > MaxHealthCheckNumberLogs {
healthCheck.Log = healthCheck.Log[1:]
}
newResults, err := json.Marshal(healthCheck)
if err != nil {
return "", fmt.Errorf("unable to marshall healthchecks for writing: %w", err)
return "", isHCStausChanged, fmt.Errorf("unable to marshall healthchecks for writing: %w", err)
}
if isHCStausChanged {
err = os.WriteFile(c.healthCheckLogPath(), newResults, 0700)
harish2704 marked this conversation as resolved.
Show resolved Hide resolved
}
return healthCheck.Status, os.WriteFile(c.healthCheckLogPath(), newResults, 0700)
return healthCheck.Status, isHCStausChanged, err
}

// HealthCheckLogPath returns the path for where the health check log is
Expand Down