Skip to content

Commit 9d2e54f

Browse files
Merge pull request #25587 from Honny1/v5.4-rhel-fix-hc-inf-log
[v5.4] Fix HealthCheck log destination, count, and size defaults
2 parents 45c2d1f + a6a7bcd commit 9d2e54f

File tree

18 files changed

+139
-135
lines changed

18 files changed

+139
-135
lines changed

libpod/container.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,27 @@ func (c *Container) HealthCheckConfig() *manifest.Schema2HealthConfig {
12911291
return c.config.HealthCheckConfig
12921292
}
12931293

1294+
func (c *Container) HealthCheckLogDestination() string {
1295+
if c.config.HealthLogDestination == nil {
1296+
return define.DefaultHealthCheckLocalDestination
1297+
}
1298+
return *c.config.HealthLogDestination
1299+
}
1300+
1301+
func (c *Container) HealthCheckMaxLogCount() uint {
1302+
if c.config.HealthMaxLogCount == nil {
1303+
return define.DefaultHealthMaxLogCount
1304+
}
1305+
return *c.config.HealthMaxLogCount
1306+
}
1307+
1308+
func (c *Container) HealthCheckMaxLogSize() uint {
1309+
if c.config.HealthMaxLogSize == nil {
1310+
return define.DefaultHealthMaxLogSize
1311+
}
1312+
return *c.config.HealthMaxLogSize
1313+
}
1314+
12941315
// AutoRemove indicates whether the container will be removed after it is executed
12951316
func (c *Container) AutoRemove() bool {
12961317
spec := c.config.Spec

libpod/container_config.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,16 @@ type ContainerMiscConfig struct {
416416
// HealthCheckOnFailureAction defines an action to take once the container turns unhealthy.
417417
HealthCheckOnFailureAction define.HealthCheckOnFailureAction `json:"healthcheck_on_failure_action"`
418418
// HealthLogDestination defines the destination where the log is stored
419-
HealthLogDestination string `json:"healthLogDestination,omitempty"`
419+
// Nil value means the default value (local).
420+
HealthLogDestination *string `json:"healthLogDestination,omitempty"`
420421
// HealthMaxLogCount is maximum number of attempts in the HealthCheck log file.
421422
// ('0' value means an infinite number of attempts in the log file)
422-
HealthMaxLogCount uint `json:"healthMaxLogCount,omitempty"`
423+
// Nil value means the default value (5).
424+
HealthMaxLogCount *uint `json:"healthMaxLogCount,omitempty"`
423425
// HealthMaxLogSize is the maximum length in characters of stored HealthCheck log
424426
// ("0" value means an infinite log length)
425-
HealthMaxLogSize uint `json:"healthMaxLogSize,omitempty"`
427+
// Nil value means the default value (500).
428+
HealthMaxLogSize *uint `json:"healthMaxLogSize,omitempty"`
426429
// StartupHealthCheckConfig is the configuration of the startup
427430
// healthcheck for the container. This will run before the regular HC
428431
// runs, and when it passes the regular HC will be activated.

libpod/container_inspect.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,11 @@ func (c *Container) generateInspectContainerConfig(spec *spec.Spec) *define.Insp
437437

438438
ctrConfig.HealthcheckOnFailureAction = c.config.HealthCheckOnFailureAction.String()
439439

440-
ctrConfig.HealthLogDestination = c.config.HealthLogDestination
440+
ctrConfig.HealthLogDestination = c.HealthCheckLogDestination()
441441

442-
ctrConfig.HealthMaxLogCount = c.config.HealthMaxLogCount
442+
ctrConfig.HealthMaxLogCount = c.HealthCheckMaxLogCount()
443443

444-
ctrConfig.HealthMaxLogSize = c.config.HealthMaxLogSize
444+
ctrConfig.HealthMaxLogSize = c.HealthCheckMaxLogSize()
445445

446446
ctrConfig.CreateCommand = c.config.CreateCommand
447447

libpod/container_internal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,19 +2839,19 @@ func (c *Container) updateGlobalHealthCheckConfiguration(globalOptions define.Gl
28392839
}
28402840

28412841
if globalOptions.HealthMaxLogCount != nil {
2842-
c.config.HealthMaxLogCount = *globalOptions.HealthMaxLogCount
2842+
c.config.HealthMaxLogCount = globalOptions.HealthMaxLogCount
28432843
}
28442844

28452845
if globalOptions.HealthMaxLogSize != nil {
2846-
c.config.HealthMaxLogSize = *globalOptions.HealthMaxLogSize
2846+
c.config.HealthMaxLogSize = globalOptions.HealthMaxLogSize
28472847
}
28482848

28492849
if globalOptions.HealthLogDestination != nil {
28502850
dest, err := define.GetValidHealthCheckDestination(*globalOptions.HealthLogDestination)
28512851
if err != nil {
28522852
return err
28532853
}
2854-
c.config.HealthLogDestination = dest
2854+
c.config.HealthLogDestination = &dest
28552855
}
28562856

28572857
if err := c.runtime.state.SafeRewriteContainerConfig(c, "", "", c.config); err != nil {

libpod/events.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (c *Container) newContainerEventWithInspectData(status events.Status, healt
4949
e.Image = c.config.RootfsImageName
5050
e.Type = events.Container
5151
e.HealthStatus = healthCheckResult.Status
52-
if c.config.HealthLogDestination == define.HealthCheckEventsLoggerDestination {
52+
if c.HealthCheckLogDestination() == define.HealthCheckEventsLoggerDestination {
5353
if len(healthCheckResult.Log) > 0 {
5454
logData, err := json.Marshal(healthCheckResult.Log[len(healthCheckResult.Log)-1])
5555
if err != nil {

libpod/healthcheck.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
136136
}
137137

138138
eventLog := output.String()
139-
if c.config.HealthMaxLogSize != 0 && len(eventLog) > int(c.config.HealthMaxLogSize) {
140-
eventLog = eventLog[:c.config.HealthMaxLogSize]
139+
if c.HealthCheckMaxLogSize() != 0 && len(eventLog) > int(c.HealthCheckMaxLogSize()) {
140+
eventLog = eventLog[:c.HealthCheckMaxLogSize()]
141141
}
142142

143143
if timeEnd.Sub(timeStart) > c.HealthCheckConfig().Timeout {
@@ -150,7 +150,7 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
150150

151151
healthCheckResult, err := c.updateHealthCheckLog(hcl, inStartPeriod, isStartup)
152152
if err != nil {
153-
return hcResult, "", fmt.Errorf("unable to update health check log %s for %s: %w", c.config.HealthLogDestination, c.ID(), err)
153+
return hcResult, "", fmt.Errorf("unable to update health check log %s for %s: %w", c.getHealthCheckLogDestination(), c.ID(), err)
154154
}
155155

156156
// Write HC event with appropriate status as the last thing before we
@@ -404,7 +404,7 @@ func (c *Container) updateHealthCheckLog(hcl define.HealthCheckLog, inStartPerio
404404
}
405405
}
406406
healthCheck.Log = append(healthCheck.Log, hcl)
407-
if c.config.HealthMaxLogCount != 0 && len(healthCheck.Log) > int(c.config.HealthMaxLogCount) {
407+
if c.HealthCheckMaxLogCount() != 0 && len(healthCheck.Log) > int(c.HealthCheckMaxLogCount()) {
408408
healthCheck.Log = healthCheck.Log[1:]
409409
}
410410
return healthCheck, c.writeHealthCheckLog(healthCheck)
@@ -420,11 +420,11 @@ func (c *Container) witeToFileHealthCheckResults(path string, result define.Heal
420420

421421
func (c *Container) getHealthCheckLogDestination() string {
422422
var destination string
423-
switch c.config.HealthLogDestination {
423+
switch c.HealthCheckLogDestination() {
424424
case define.DefaultHealthCheckLocalDestination, define.HealthCheckEventsLoggerDestination, "":
425425
destination = filepath.Join(filepath.Dir(c.state.RunDir), "healthcheck.log")
426426
default:
427-
destination = filepath.Join(c.config.HealthLogDestination, c.ID()+"-healthcheck.log")
427+
destination = filepath.Join(c.HealthCheckLogDestination(), c.ID()+"-healthcheck.log")
428428
}
429429
return destination
430430
}

libpod/options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ func WithHealthCheckLogDestination(destination string) CtrCreateOption {
15361536
if err != nil {
15371537
return err
15381538
}
1539-
ctr.config.HealthLogDestination = dest
1539+
ctr.config.HealthLogDestination = &dest
15401540
return nil
15411541
}
15421542
}
@@ -1547,7 +1547,7 @@ func WithHealthCheckMaxLogCount(maxLogCount uint) CtrCreateOption {
15471547
if ctr.valid {
15481548
return define.ErrCtrFinalized
15491549
}
1550-
ctr.config.HealthMaxLogCount = maxLogCount
1550+
ctr.config.HealthMaxLogCount = &maxLogCount
15511551
return nil
15521552
}
15531553
}
@@ -1558,7 +1558,7 @@ func WithHealthCheckMaxLogSize(maxLogSize uint) CtrCreateOption {
15581558
if ctr.valid {
15591559
return define.ErrCtrFinalized
15601560
}
1561-
ctr.config.HealthMaxLogSize = maxLogSize
1561+
ctr.config.HealthMaxLogSize = &maxLogSize
15621562
return nil
15631563
}
15641564
}

pkg/api/handlers/libpod/containers_create.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func CreateContainer(w http.ResponseWriter, r *http.Request) {
4545
},
4646
ContainerHealthCheckConfig: specgen.ContainerHealthCheckConfig{
4747
HealthLogDestination: define.DefaultHealthCheckLocalDestination,
48+
HealthMaxLogCount: define.DefaultHealthMaxLogCount,
49+
HealthMaxLogSize: define.DefaultHealthMaxLogSize,
4850
},
4951
}
5052

pkg/domain/entities/pods.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
commonFlag "github.com/containers/common/pkg/flag"
8+
"github.com/containers/podman/v5/libpod/define"
89
"github.com/containers/podman/v5/pkg/domain/entities/types"
910
"github.com/containers/podman/v5/pkg/specgen"
1011
"github.com/containers/podman/v5/pkg/util"
@@ -275,9 +276,12 @@ type ContainerCreateOptions struct {
275276

276277
func NewInfraContainerCreateOptions() ContainerCreateOptions {
277278
options := ContainerCreateOptions{
278-
IsInfra: true,
279-
ImageVolume: "anonymous",
280-
MemorySwappiness: -1,
279+
IsInfra: true,
280+
ImageVolume: "anonymous",
281+
MemorySwappiness: -1,
282+
HealthLogDestination: define.DefaultHealthCheckLocalDestination,
283+
HealthMaxLogCount: define.DefaultHealthMaxLogCount,
284+
HealthMaxLogSize: define.DefaultHealthMaxLogSize,
281285
}
282286
return options
283287
}

pkg/domain/infra/abi/containers.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,10 +1760,6 @@ func (ic *ContainerEngine) ContainerClone(ctx context.Context, ctrCloneOpts enti
17601760
spec.Name = generate.CheckName(ic.Libpod, n, true)
17611761
}
17621762

1763-
spec.HealthLogDestination = define.DefaultHealthCheckLocalDestination
1764-
spec.HealthMaxLogCount = define.DefaultHealthMaxLogCount
1765-
spec.HealthMaxLogSize = define.DefaultHealthMaxLogSize
1766-
17671763
rtSpec, spec, opts, err := generate.MakeContainer(context.Background(), ic.Libpod, spec, true, c)
17681764
if err != nil {
17691765
return nil, err

0 commit comments

Comments
 (0)