Skip to content

Commit 64e2b91

Browse files
Honny1openshift-cherrypick-robot
authored and
openshift-cherrypick-robot
committed
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: containers#25473 Fixes: https://issues.redhat.com/browse/RHEL-83262 Signed-off-by: Jan Rodák <[email protected]>
1 parent 61d703b commit 64e2b91

18 files changed

+139
-135
lines changed

libpod/container.go

+21
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

+6-3
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

+3-3
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2845,19 +2845,19 @@ func (c *Container) updateGlobalHealthCheckConfiguration(globalOptions define.Gl
28452845
}
28462846

28472847
if globalOptions.HealthMaxLogCount != nil {
2848-
c.config.HealthMaxLogCount = *globalOptions.HealthMaxLogCount
2848+
c.config.HealthMaxLogCount = globalOptions.HealthMaxLogCount
28492849
}
28502850

28512851
if globalOptions.HealthMaxLogSize != nil {
2852-
c.config.HealthMaxLogSize = *globalOptions.HealthMaxLogSize
2852+
c.config.HealthMaxLogSize = globalOptions.HealthMaxLogSize
28532853
}
28542854

28552855
if globalOptions.HealthLogDestination != nil {
28562856
dest, err := define.GetValidHealthCheckDestination(*globalOptions.HealthLogDestination)
28572857
if err != nil {
28582858
return err
28592859
}
2860-
c.config.HealthLogDestination = dest
2860+
c.config.HealthLogDestination = &dest
28612861
}
28622862

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

libpod/events.go

+1-1
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

+6-6
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

+3-3
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

+2
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

+7-3
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

-4
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

pkg/domain/infra/abi/generate.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ func (ic *ContainerEngine) GenerateSpec(ctx context.Context, opts *entities.Gene
5959
} else if p, err := ic.Libpod.LookupPod(opts.ID); err == nil {
6060
pspec = &specgen.PodSpecGenerator{}
6161
pspec.Name = p.Name()
62-
_, err := generateUtils.PodConfigToSpec(ic.Libpod, pspec, &entities.ContainerCreateOptions{}, opts.ID)
62+
_, err := generateUtils.PodConfigToSpec(ic.Libpod, pspec,
63+
&entities.ContainerCreateOptions{
64+
HealthLogDestination: define.DefaultHealthCheckLocalDestination,
65+
HealthMaxLogCount: define.DefaultHealthMaxLogCount,
66+
HealthMaxLogSize: define.DefaultHealthMaxLogSize,
67+
}, opts.ID)
6368
if err != nil {
6469
return nil, err
6570
}

pkg/specgen/generate/container.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,23 @@ func ConfigToSpec(rt *libpod.Runtime, specg *specgen.SpecGenerator, containerID
444444
}
445445
}
446446

447-
specg.HealthLogDestination = conf.HealthLogDestination
448-
specg.HealthMaxLogCount = conf.HealthMaxLogCount
449-
specg.HealthMaxLogSize = conf.HealthMaxLogSize
447+
if conf.HealthLogDestination != nil {
448+
specg.HealthLogDestination = *conf.HealthLogDestination
449+
} else {
450+
specg.HealthLogDestination = define.DefaultHealthCheckLocalDestination
451+
}
452+
453+
if conf.HealthMaxLogCount != nil {
454+
specg.HealthMaxLogCount = *conf.HealthMaxLogCount
455+
} else {
456+
specg.HealthMaxLogCount = define.DefaultHealthMaxLogCount
457+
}
458+
459+
if conf.HealthMaxLogSize != nil {
460+
specg.HealthMaxLogSize = *conf.HealthMaxLogSize
461+
} else {
462+
specg.HealthMaxLogSize = define.DefaultHealthMaxLogSize
463+
}
450464

451465
specg.IDMappings = &conf.IDMappings
452466
specg.ContainerCreateCommand = conf.CreateCommand

pkg/specgen/generate/kube/kube.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
404404
s.Annotations[define.InspectAnnotationInit] = init
405405
}
406406

407+
s.HealthLogDestination = define.DefaultHealthCheckLocalDestination
408+
s.HealthMaxLogCount = define.DefaultHealthMaxLogCount
409+
s.HealthMaxLogSize = define.DefaultHealthMaxLogSize
410+
407411
if publishAll, ok := opts.Annotations[define.InspectAnnotationPublishAll+"/"+opts.Container.Name]; ok {
408412
if opts.IsInfra {
409413
publishAllAsBool, err := strconv.ParseBool(publishAll)
@@ -417,9 +421,6 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
417421
}
418422

419423
s.Annotations[define.KubeHealthCheckAnnotation] = "true"
420-
s.HealthLogDestination = define.DefaultHealthCheckLocalDestination
421-
s.HealthMaxLogCount = define.DefaultHealthMaxLogCount
422-
s.HealthMaxLogSize = define.DefaultHealthMaxLogSize
423424

424425
// Environment Variables
425426
envs := map[string]string{}

pkg/specgen/generate/pod_create.go

-5
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ func MakePod(p *entities.PodSpec, rt *libpod.Runtime) (_ *libpod.Pod, finalErr e
8686
p.PodSpecGen.InfraContainerSpec.ResourceLimits = nil
8787
p.PodSpecGen.InfraContainerSpec.WeightDevice = nil
8888

89-
// Set default for HealthCheck
90-
p.PodSpecGen.InfraContainerSpec.HealthLogDestination = define.DefaultHealthCheckLocalDestination
91-
p.PodSpecGen.InfraContainerSpec.HealthMaxLogCount = define.DefaultHealthMaxLogCount
92-
p.PodSpecGen.InfraContainerSpec.HealthMaxLogSize = define.DefaultHealthMaxLogSize
93-
9489
rtSpec, spec, opts, err := MakeContainer(context.Background(), rt, p.PodSpecGen.InfraContainerSpec, false, nil)
9590
if err != nil {
9691
return nil, err

pkg/specgen/specgen.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -602,14 +602,17 @@ type ContainerHealthCheckConfig struct {
602602
// Requires that HealthConfig be set.
603603
// Optional.
604604
StartupHealthConfig *define.StartupHealthCheck `json:"startupHealthConfig,omitempty"`
605-
// HealthLogDestination defines the destination where the log is stored
606-
HealthLogDestination string `json:"healthLogDestination,omitempty"`
605+
// HealthLogDestination defines the destination where the log is stored.
606+
// TODO (6.0): In next major release convert it to pointer and use omitempty
607+
HealthLogDestination string `json:"healthLogDestination"`
607608
// HealthMaxLogCount is maximum number of attempts in the HealthCheck log file.
608-
// ('0' value means an infinite number of attempts in the log file)
609-
HealthMaxLogCount uint `json:"healthMaxLogCount,omitempty"`
609+
// ('0' value means an infinite number of attempts in the log file).
610+
// TODO (6.0): In next major release convert it to pointer and use omitempty
611+
HealthMaxLogCount uint `json:"healthMaxLogCount"`
610612
// HealthMaxLogSize is the maximum length in characters of stored HealthCheck log
611-
// ("0" value means an infinite log length)
612-
HealthMaxLogSize uint `json:"healthMaxLogSize,omitempty"`
613+
// ("0" value means an infinite log length).
614+
// TODO (6.0): In next major release convert it to pointer and use omitempty
615+
HealthMaxLogSize uint `json:"healthMaxLogSize"`
613616
}
614617

615618
// SpecGenerator creates an OCI spec and Libpod configuration options to create

test/apiv2/20-containers.at

+4-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ t GET libpod/containers/${cid}/json 200 \
199199
.State.Running=false \
200200
.State.ExitCode=0 \
201201
.Config.Umask=0022 \
202-
.Config.CreateCommand=null
202+
.Config.CreateCommand=null \
203+
.Config.HealthLogDestination=local \
204+
.Config.HealthcheckMaxLogCount=5 \
205+
.Config.HealthcheckMaxLogSize=500
203206
t DELETE libpod/containers/$cid 200 .[0].Id=$cid
204207

205208
CNAME=myfoo

0 commit comments

Comments
 (0)