Skip to content

Commit d129a95

Browse files
committed
add default values to healthConfig
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 93d74e6 commit d129a95

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

cmd/nerdctl/container/container_health_check_linux_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,47 @@ func TestContainerHealthCheckAdvance(t *testing.T) {
336336
}
337337
},
338338
},
339+
{
340+
Description: "Health check applies default values when not explicitly set",
341+
Setup: func(data test.Data, helpers test.Helpers) {
342+
// Create container with only --health-cmd, no other health flags
343+
helpers.Ensure("run", "-d", "--name", data.Identifier(),
344+
"--health-cmd", "echo healthy",
345+
testutil.CommonImage, "sleep", nerdtest.Infinity)
346+
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
347+
},
348+
Cleanup: func(data test.Data, helpers test.Helpers) {
349+
helpers.Anyhow("rm", "-f", data.Identifier())
350+
},
351+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
352+
return helpers.Command("inspect", data.Identifier())
353+
},
354+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
355+
return &test.Expected{
356+
ExitCode: 0,
357+
Output: expect.All(func(stdout string, t tig.T) {
358+
inspect := nerdtest.InspectContainer(helpers, data.Identifier())
359+
360+
// Parse the healthcheck config from container labels
361+
hcLabel := inspect.Config.Labels["nerdctl/healthcheck"]
362+
assert.Assert(t, hcLabel != "", "expected healthcheck label to be present")
363+
364+
var hc healthcheck.Healthcheck
365+
err := json.Unmarshal([]byte(hcLabel), &hc)
366+
assert.NilError(t, err, "failed to parse healthcheck config")
367+
368+
// Verify default values are applied
369+
assert.Equal(t, hc.Interval, 30*time.Second, "expected default interval of 30s")
370+
assert.Equal(t, hc.Timeout, 30*time.Second, "expected default timeout of 30s")
371+
assert.Equal(t, hc.Retries, 3, "expected default retries of 3")
372+
assert.Equal(t, hc.StartPeriod, 0*time.Second, "expected default start period of 0s")
373+
374+
// Verify the command was set correctly
375+
assert.DeepEqual(t, hc.Test, []string{"CMD-SHELL", "echo healthy"})
376+
}),
377+
}
378+
},
379+
},
339380
{
340381
Description: "Health check uses container environment variables",
341382
Setup: func(data test.Data, helpers test.Helpers) {

pkg/cmd/container/create.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,11 @@ func withHealthcheck(options types.ContainerCreateOptions, ensuredImage *imgutil
892892
hc.StartPeriod = options.HealthStartPeriod
893893
}
894894

895+
// Apply defaults for any unset values, but only if we have a healthcheck configured
896+
if len(hc.Test) > 0 && hc.Test[0] != "NONE" {
897+
hc.ApplyDefaults()
898+
}
899+
895900
// If no healthcheck config is set (via CLI or image), return empty string so we skip adding to container config.
896901
if reflect.DeepEqual(hc, &healthcheck.Healthcheck{}) {
897902
return "", nil

pkg/healthcheck/health.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,19 @@ func HealthcheckResultFromJSON(s string) (*HealthcheckResult, error) {
136136
}
137137
return &r, nil
138138
}
139+
140+
// ApplyDefaults sets default values for unset healthcheck fields
141+
func (hc *Healthcheck) ApplyDefaults() {
142+
if hc.Interval == 0 {
143+
hc.Interval = DefaultProbeInterval
144+
}
145+
if hc.Timeout == 0 {
146+
hc.Timeout = DefaultProbeTimeout
147+
}
148+
if hc.StartPeriod == 0 {
149+
hc.StartPeriod = DefaultStartPeriod
150+
}
151+
if hc.Retries == 0 {
152+
hc.Retries = DefaultProbeRetries
153+
}
154+
}

pkg/healthcheck/healthcheck_manager_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func shouldSkipHealthCheckSystemd(hc *Healthcheck, cfg *config.Config) bool {
265265
}
266266

267267
// Don't proceed if health check is nil, empty, explicitly NONE or interval is 0.
268-
if hc == nil || len(hc.Test) == 0 || hc.Test[0] == "NONE" || hc.Interval == 0 {
268+
if hc == nil || len(hc.Test) == 0 || hc.Test[0] == "NONE" {
269269
return true
270270
}
271271
return false

0 commit comments

Comments
 (0)