Skip to content

Commit ec67596

Browse files
committed
[v5.2-rhel] 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-83558 Signed-off-by: Jan Rodák <[email protected]> (cherry picked from commit fff42ac) Signed-off-by: Jan Rodák <[email protected]>
1 parent 602a296 commit ec67596

17 files changed

+137
-133
lines changed

libpod/container.go

+21
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,27 @@ func (c *Container) HealthCheckConfig() *manifest.Schema2HealthConfig {
12521252
return c.config.HealthCheckConfig
12531253
}
12541254

1255+
func (c *Container) HealthCheckLogDestination() string {
1256+
if c.config.HealthLogDestination == nil {
1257+
return define.DefaultHealthCheckLocalDestination
1258+
}
1259+
return *c.config.HealthLogDestination
1260+
}
1261+
1262+
func (c *Container) HealthCheckMaxLogCount() uint {
1263+
if c.config.HealthMaxLogCount == nil {
1264+
return define.DefaultHealthMaxLogCount
1265+
}
1266+
return *c.config.HealthMaxLogCount
1267+
}
1268+
1269+
func (c *Container) HealthCheckMaxLogSize() uint {
1270+
if c.config.HealthMaxLogSize == nil {
1271+
return define.DefaultHealthMaxLogSize
1272+
}
1273+
return *c.config.HealthMaxLogSize
1274+
}
1275+
12551276
// AutoRemove indicates whether the container will be removed after it is executed
12561277
func (c *Container) AutoRemove() bool {
12571278
spec := c.config.Spec

libpod/container_config.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,16 @@ type ContainerMiscConfig struct {
414414
// HealthCheckOnFailureAction defines an action to take once the container turns unhealthy.
415415
HealthCheckOnFailureAction define.HealthCheckOnFailureAction `json:"healthcheck_on_failure_action"`
416416
// HealthLogDestination defines the destination where the log is stored
417-
HealthLogDestination string `json:"healthLogDestination,omitempty"`
417+
// Nil value means the default value (local).
418+
HealthLogDestination *string `json:"healthLogDestination,omitempty"`
418419
// HealthMaxLogCount is maximum number of attempts in the HealthCheck log file.
419420
// ('0' value means an infinite number of attempts in the log file)
420-
HealthMaxLogCount uint `json:"healthMaxLogCount,omitempty"`
421+
// Nil value means the default value (5).
422+
HealthMaxLogCount *uint `json:"healthMaxLogCount,omitempty"`
421423
// HealthMaxLogSize is the maximum length in characters of stored HealthCheck log
422424
// ("0" value means an infinite log length)
423-
HealthMaxLogSize uint `json:"healthMaxLogSize,omitempty"`
425+
// Nil value means the default value (500).
426+
HealthMaxLogSize *uint `json:"healthMaxLogSize,omitempty"`
424427
// StartupHealthCheckConfig is the configuration of the startup
425428
// healthcheck for the container. This will run before the regular HC
426429
// 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
@@ -417,11 +417,11 @@ func (c *Container) generateInspectContainerConfig(spec *spec.Spec) *define.Insp
417417

418418
ctrConfig.HealthcheckOnFailureAction = c.config.HealthCheckOnFailureAction.String()
419419

420-
ctrConfig.HealthLogDestination = c.config.HealthLogDestination
420+
ctrConfig.HealthLogDestination = c.HealthCheckLogDestination()
421421

422-
ctrConfig.HealthMaxLogCount = c.config.HealthMaxLogCount
422+
ctrConfig.HealthMaxLogCount = c.HealthCheckMaxLogCount()
423423

424-
ctrConfig.HealthMaxLogSize = c.config.HealthMaxLogSize
424+
ctrConfig.HealthMaxLogSize = c.HealthCheckMaxLogSize()
425425

426426
ctrConfig.CreateCommand = c.config.CreateCommand
427427

libpod/events.go

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

libpod/healthcheck.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
135135
}
136136

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

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

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

155155
// Write HC event with appropriate status as the last thing before we
@@ -387,7 +387,7 @@ func (c *Container) updateHealthCheckLog(hcl define.HealthCheckLog, inStartPerio
387387
}
388388
}
389389
healthCheck.Log = append(healthCheck.Log, hcl)
390-
if c.config.HealthMaxLogCount != 0 && len(healthCheck.Log) > int(c.config.HealthMaxLogCount) {
390+
if c.HealthCheckMaxLogCount() != 0 && len(healthCheck.Log) > int(c.HealthCheckMaxLogCount()) {
391391
healthCheck.Log = healthCheck.Log[1:]
392392
}
393393
return healthCheck, c.writeHealthCheckLog(healthCheck)
@@ -403,11 +403,11 @@ func (c *Container) witeToFileHealthCheckResults(path string, result define.Heal
403403

404404
func (c *Container) getHealthCheckLogDestination() string {
405405
var destination string
406-
switch c.config.HealthLogDestination {
406+
switch c.HealthCheckLogDestination() {
407407
case define.DefaultHealthCheckLocalDestination, define.HealthCheckEventsLoggerDestination, "":
408408
destination = filepath.Join(filepath.Dir(c.state.RunDir), "healthcheck.log")
409409
default:
410-
destination = filepath.Join(c.config.HealthLogDestination, c.ID()+"-healthcheck.log")
410+
destination = filepath.Join(c.HealthCheckLogDestination(), c.ID()+"-healthcheck.log")
411411
}
412412
return destination
413413
}

libpod/options.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ func WithHealthCheckLogDestination(destination string) CtrCreateOption {
15231523
}
15241524
switch destination {
15251525
case define.HealthCheckEventsLoggerDestination, define.DefaultHealthCheckLocalDestination:
1526-
ctr.config.HealthLogDestination = destination
1526+
ctr.config.HealthLogDestination = &destination
15271527
default:
15281528
fileInfo, err := os.Stat(destination)
15291529
if err != nil {
@@ -1538,7 +1538,7 @@ func WithHealthCheckLogDestination(destination string) CtrCreateOption {
15381538
if err != nil {
15391539
return err
15401540
}
1541-
ctr.config.HealthLogDestination = absPath
1541+
ctr.config.HealthLogDestination = &absPath
15421542
}
15431543
return nil
15441544
}
@@ -1550,7 +1550,7 @@ func WithHealthCheckMaxLogCount(maxLogCount uint) CtrCreateOption {
15501550
if ctr.valid {
15511551
return define.ErrCtrFinalized
15521552
}
1553-
ctr.config.HealthMaxLogCount = maxLogCount
1553+
ctr.config.HealthMaxLogCount = &maxLogCount
15541554
return nil
15551555
}
15561556
}
@@ -1561,7 +1561,7 @@ func WithHealthCheckMaxLogSize(maxLogSize uint) CtrCreateOption {
15611561
if ctr.valid {
15621562
return define.ErrCtrFinalized
15631563
}
1564-
ctr.config.HealthMaxLogSize = maxLogSize
1564+
ctr.config.HealthMaxLogSize = &maxLogSize
15651565
return nil
15661566
}
15671567
}

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
@@ -1736,10 +1736,6 @@ func (ic *ContainerEngine) ContainerClone(ctx context.Context, ctrCloneOpts enti
17361736
spec.Name = generate.CheckName(ic.Libpod, n, true)
17371737
}
17381738

1739-
spec.HealthLogDestination = define.DefaultHealthCheckLocalDestination
1740-
spec.HealthMaxLogCount = define.DefaultHealthMaxLogCount
1741-
spec.HealthMaxLogSize = define.DefaultHealthMaxLogSize
1742-
17431739
rtSpec, spec, opts, err := generate.MakeContainer(context.Background(), ic.Libpod, spec, true, c)
17441740
if err != nil {
17451741
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
@@ -425,6 +425,10 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
425425
s.Annotations[define.InspectAnnotationInit] = init
426426
}
427427

428+
s.HealthLogDestination = define.DefaultHealthCheckLocalDestination
429+
s.HealthMaxLogCount = define.DefaultHealthMaxLogCount
430+
s.HealthMaxLogSize = define.DefaultHealthMaxLogSize
431+
428432
if publishAll, ok := opts.Annotations[define.InspectAnnotationPublishAll+"/"+opts.Container.Name]; ok {
429433
if opts.IsInfra {
430434
publishAllAsBool, err := strconv.ParseBool(publishAll)
@@ -438,9 +442,6 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
438442
}
439443

440444
s.Annotations[define.KubeHealthCheckAnnotation] = "true"
441-
s.HealthLogDestination = define.DefaultHealthCheckLocalDestination
442-
s.HealthMaxLogCount = define.DefaultHealthMaxLogCount
443-
s.HealthMaxLogSize = define.DefaultHealthMaxLogSize
444445

445446
// Environment Variables
446447
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
@@ -599,14 +599,17 @@ type ContainerHealthCheckConfig struct {
599599
// Requires that HealthConfig be set.
600600
// Optional.
601601
StartupHealthConfig *define.StartupHealthCheck `json:"startupHealthConfig,omitempty"`
602-
// HealthLogDestination defines the destination where the log is stored
603-
HealthLogDestination string `json:"healthLogDestination,omitempty"`
602+
// HealthLogDestination defines the destination where the log is stored.
603+
// TODO (6.0): In next major release convert it to pointer and use omitempty
604+
HealthLogDestination string `json:"healthLogDestination"`
604605
// HealthMaxLogCount is maximum number of attempts in the HealthCheck log file.
605-
// ('0' value means an infinite number of attempts in the log file)
606-
HealthMaxLogCount uint `json:"healthMaxLogCount,omitempty"`
606+
// ('0' value means an infinite number of attempts in the log file).
607+
// TODO (6.0): In next major release convert it to pointer and use omitempty
608+
HealthMaxLogCount uint `json:"healthMaxLogCount"`
607609
// HealthMaxLogSize is the maximum length in characters of stored HealthCheck log
608-
// ("0" value means an infinite log length)
609-
HealthMaxLogSize uint `json:"healthMaxLogSize,omitempty"`
610+
// ("0" value means an infinite log length).
611+
// TODO (6.0): In next major release convert it to pointer and use omitempty
612+
HealthMaxLogSize uint `json:"healthMaxLogSize"`
610613
}
611614

612615
// 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
@@ -181,7 +181,10 @@ t GET libpod/containers/${cid}/json 200 \
181181
.State.Status~\\\(exited\\\|stopped\\\) \
182182
.State.Running=false \
183183
.State.ExitCode=0 \
184-
.Config.Umask=0022 # regression check for #15036
184+
.Config.Umask=0022 \
185+
.Config.HealthLogDestination=local \
186+
.Config.HealthcheckMaxLogCount=5 \
187+
.Config.HealthcheckMaxLogSize=500
185188
t DELETE libpod/containers/$cid 200 .[0].Id=$cid
186189

187190
CNAME=myfoo

0 commit comments

Comments
 (0)