Fix HealthCheck log destination, count, and size defaults

GoLang sets unset values to the default value of the type. This means that the destination of the log is an empty string and the count and size are set to 0. However, this means that size and count are unbounded, and this is not the default behavior.

Fixes: https://github.com/containers/podman/issues/25473
Fixes: https://issues.redhat.com/browse/RHEL-83262

Signed-off-by: Jan Rodák <hony.com@seznam.cz>
This commit is contained in:
Jan Rodák
2025-03-09 17:59:53 +01:00
committed by openshift-cherrypick-robot
parent 61d703ba85
commit 64e2b91ab4
18 changed files with 138 additions and 134 deletions

View File

@ -1291,6 +1291,27 @@ func (c *Container) HealthCheckConfig() *manifest.Schema2HealthConfig {
return c.config.HealthCheckConfig
}
func (c *Container) HealthCheckLogDestination() string {
if c.config.HealthLogDestination == nil {
return define.DefaultHealthCheckLocalDestination
}
return *c.config.HealthLogDestination
}
func (c *Container) HealthCheckMaxLogCount() uint {
if c.config.HealthMaxLogCount == nil {
return define.DefaultHealthMaxLogCount
}
return *c.config.HealthMaxLogCount
}
func (c *Container) HealthCheckMaxLogSize() uint {
if c.config.HealthMaxLogSize == nil {
return define.DefaultHealthMaxLogSize
}
return *c.config.HealthMaxLogSize
}
// AutoRemove indicates whether the container will be removed after it is executed
func (c *Container) AutoRemove() bool {
spec := c.config.Spec

View File

@ -416,13 +416,16 @@ type ContainerMiscConfig struct {
// HealthCheckOnFailureAction defines an action to take once the container turns unhealthy.
HealthCheckOnFailureAction define.HealthCheckOnFailureAction `json:"healthcheck_on_failure_action"`
// HealthLogDestination defines the destination where the log is stored
HealthLogDestination string `json:"healthLogDestination,omitempty"`
// Nil value means the default value (local).
HealthLogDestination *string `json:"healthLogDestination,omitempty"`
// HealthMaxLogCount is maximum number of attempts in the HealthCheck log file.
// ('0' value means an infinite number of attempts in the log file)
HealthMaxLogCount uint `json:"healthMaxLogCount,omitempty"`
// Nil value means the default value (5).
HealthMaxLogCount *uint `json:"healthMaxLogCount,omitempty"`
// HealthMaxLogSize is the maximum length in characters of stored HealthCheck log
// ("0" value means an infinite log length)
HealthMaxLogSize uint `json:"healthMaxLogSize,omitempty"`
// Nil value means the default value (500).
HealthMaxLogSize *uint `json:"healthMaxLogSize,omitempty"`
// StartupHealthCheckConfig is the configuration of the startup
// healthcheck for the container. This will run before the regular HC
// runs, and when it passes the regular HC will be activated.

View File

@ -437,11 +437,11 @@ func (c *Container) generateInspectContainerConfig(spec *spec.Spec) *define.Insp
ctrConfig.HealthcheckOnFailureAction = c.config.HealthCheckOnFailureAction.String()
ctrConfig.HealthLogDestination = c.config.HealthLogDestination
ctrConfig.HealthLogDestination = c.HealthCheckLogDestination()
ctrConfig.HealthMaxLogCount = c.config.HealthMaxLogCount
ctrConfig.HealthMaxLogCount = c.HealthCheckMaxLogCount()
ctrConfig.HealthMaxLogSize = c.config.HealthMaxLogSize
ctrConfig.HealthMaxLogSize = c.HealthCheckMaxLogSize()
ctrConfig.CreateCommand = c.config.CreateCommand

View File

@ -2845,11 +2845,11 @@ func (c *Container) updateGlobalHealthCheckConfiguration(globalOptions define.Gl
}
if globalOptions.HealthMaxLogCount != nil {
c.config.HealthMaxLogCount = *globalOptions.HealthMaxLogCount
c.config.HealthMaxLogCount = globalOptions.HealthMaxLogCount
}
if globalOptions.HealthMaxLogSize != nil {
c.config.HealthMaxLogSize = *globalOptions.HealthMaxLogSize
c.config.HealthMaxLogSize = globalOptions.HealthMaxLogSize
}
if globalOptions.HealthLogDestination != nil {
@ -2857,7 +2857,7 @@ func (c *Container) updateGlobalHealthCheckConfiguration(globalOptions define.Gl
if err != nil {
return err
}
c.config.HealthLogDestination = dest
c.config.HealthLogDestination = &dest
}
if err := c.runtime.state.SafeRewriteContainerConfig(c, "", "", c.config); err != nil {

View File

@ -49,7 +49,7 @@ func (c *Container) newContainerEventWithInspectData(status events.Status, healt
e.Image = c.config.RootfsImageName
e.Type = events.Container
e.HealthStatus = healthCheckResult.Status
if c.config.HealthLogDestination == define.HealthCheckEventsLoggerDestination {
if c.HealthCheckLogDestination() == define.HealthCheckEventsLoggerDestination {
if len(healthCheckResult.Log) > 0 {
logData, err := json.Marshal(healthCheckResult.Log[len(healthCheckResult.Log)-1])
if err != nil {

View File

@ -136,8 +136,8 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
}
eventLog := output.String()
if c.config.HealthMaxLogSize != 0 && len(eventLog) > int(c.config.HealthMaxLogSize) {
eventLog = eventLog[:c.config.HealthMaxLogSize]
if c.HealthCheckMaxLogSize() != 0 && len(eventLog) > int(c.HealthCheckMaxLogSize()) {
eventLog = eventLog[:c.HealthCheckMaxLogSize()]
}
if timeEnd.Sub(timeStart) > c.HealthCheckConfig().Timeout {
@ -150,7 +150,7 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
healthCheckResult, err := c.updateHealthCheckLog(hcl, inStartPeriod, isStartup)
if err != nil {
return hcResult, "", fmt.Errorf("unable to update health check log %s for %s: %w", c.config.HealthLogDestination, c.ID(), err)
return hcResult, "", fmt.Errorf("unable to update health check log %s for %s: %w", c.getHealthCheckLogDestination(), c.ID(), err)
}
// Write HC event with appropriate status as the last thing before we
@ -404,7 +404,7 @@ func (c *Container) updateHealthCheckLog(hcl define.HealthCheckLog, inStartPerio
}
}
healthCheck.Log = append(healthCheck.Log, hcl)
if c.config.HealthMaxLogCount != 0 && len(healthCheck.Log) > int(c.config.HealthMaxLogCount) {
if c.HealthCheckMaxLogCount() != 0 && len(healthCheck.Log) > int(c.HealthCheckMaxLogCount()) {
healthCheck.Log = healthCheck.Log[1:]
}
return healthCheck, c.writeHealthCheckLog(healthCheck)
@ -420,11 +420,11 @@ func (c *Container) witeToFileHealthCheckResults(path string, result define.Heal
func (c *Container) getHealthCheckLogDestination() string {
var destination string
switch c.config.HealthLogDestination {
switch c.HealthCheckLogDestination() {
case define.DefaultHealthCheckLocalDestination, define.HealthCheckEventsLoggerDestination, "":
destination = filepath.Join(filepath.Dir(c.state.RunDir), "healthcheck.log")
default:
destination = filepath.Join(c.config.HealthLogDestination, c.ID()+"-healthcheck.log")
destination = filepath.Join(c.HealthCheckLogDestination(), c.ID()+"-healthcheck.log")
}
return destination
}

View File

@ -1536,7 +1536,7 @@ func WithHealthCheckLogDestination(destination string) CtrCreateOption {
if err != nil {
return err
}
ctr.config.HealthLogDestination = dest
ctr.config.HealthLogDestination = &dest
return nil
}
}
@ -1547,7 +1547,7 @@ func WithHealthCheckMaxLogCount(maxLogCount uint) CtrCreateOption {
if ctr.valid {
return define.ErrCtrFinalized
}
ctr.config.HealthMaxLogCount = maxLogCount
ctr.config.HealthMaxLogCount = &maxLogCount
return nil
}
}
@ -1558,7 +1558,7 @@ func WithHealthCheckMaxLogSize(maxLogSize uint) CtrCreateOption {
if ctr.valid {
return define.ErrCtrFinalized
}
ctr.config.HealthMaxLogSize = maxLogSize
ctr.config.HealthMaxLogSize = &maxLogSize
return nil
}
}