Skip to content

Commit 2c6cf80

Browse files
committed
add healthcheck orchestration logic
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent b78a05a commit 2c6cf80

20 files changed

+816
-79
lines changed

cmd/nerdctl/container/container_create.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,6 @@ func createOptions(cmd *cobra.Command) (types.ContainerCreateOptions, error) {
279279
if err != nil {
280280
return opt, err
281281
}
282-
opt.HealthStartInterval, err = cmd.Flags().GetDuration("health-start-interval")
283-
if err != nil {
284-
return opt, err
285-
}
286282
opt.NoHealthcheck, err = cmd.Flags().GetBool("no-healthcheck")
287283
if err != nil {
288284
return opt, err

cmd/nerdctl/container/container_health_check_test.go renamed to cmd/nerdctl/container/container_health_check_linux_test.go

Lines changed: 368 additions & 38 deletions
Large diffs are not rendered by default.

cmd/nerdctl/container/container_run.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/containerd/nerdctl/v2/pkg/containerutil"
3838
"github.com/containerd/nerdctl/v2/pkg/defaults"
3939
"github.com/containerd/nerdctl/v2/pkg/errutil"
40+
"github.com/containerd/nerdctl/v2/pkg/healthcheck"
4041
"github.com/containerd/nerdctl/v2/pkg/labels"
4142
"github.com/containerd/nerdctl/v2/pkg/logging"
4243
"github.com/containerd/nerdctl/v2/pkg/netutil"
@@ -240,7 +241,6 @@ func setCreateFlags(cmd *cobra.Command) {
240241
cmd.Flags().Duration("health-timeout", 0, "Maximum time to allow one check to run (default: 30s)")
241242
cmd.Flags().Int("health-retries", 0, "Consecutive failures needed to report unhealthy (default: 3)")
242243
cmd.Flags().Duration("health-start-period", 0, "Start period for the container to initialize before starting health-retries countdown")
243-
cmd.Flags().Duration("health-start-interval", 0, "Time between running the checks during the start period")
244244
cmd.Flags().Bool("no-healthcheck", false, "Disable any container-specified HEALTHCHECK")
245245

246246
// #region env flags
@@ -439,6 +439,14 @@ func runAction(cmd *cobra.Command, args []string) error {
439439
return err
440440
}
441441

442+
// Setup container healthchecks.
443+
if err := healthcheck.CreateTimer(ctx, c); err != nil {
444+
return fmt.Errorf("failed to create healthcheck timer: %w", err)
445+
}
446+
if err := healthcheck.StartTimer(ctx, c); err != nil {
447+
return fmt.Errorf("failed to start healthcheck timer: %w", err)
448+
}
449+
442450
if createOpt.Detach {
443451
fmt.Fprintln(createOpt.Stdout, id)
444452
return nil

cmd/nerdctl/container/container_run_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,9 @@ func TestRunDomainname(t *testing.T) {
841841
}
842842

843843
func TestRunHealthcheckFlags(t *testing.T) {
844+
if rootlessutil.IsRootless() {
845+
t.Skip("healthcheck tests are skipped in rootless environment")
846+
}
844847
testCase := nerdtest.Setup()
845848

846849
testCases := []struct {
@@ -990,6 +993,9 @@ func TestRunHealthcheckFlags(t *testing.T) {
990993
}
991994

992995
func TestRunHealthcheckFromImage(t *testing.T) {
996+
if rootlessutil.IsRootless() {
997+
t.Skip("healthcheck tests are skipped in rootless environment")
998+
}
993999
nerdtest.Setup()
9941000

9951001
dockerfile := fmt.Sprintf(`FROM %s

cmd/nerdctl/helpers/flagutil.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ func ValidateHealthcheckFlags(options types.ContainerCreateOptions) error {
5252
options.HealthInterval != 0 ||
5353
options.HealthTimeout != 0 ||
5454
options.HealthRetries != 0 ||
55-
options.HealthStartPeriod != 0 ||
56-
options.HealthStartInterval != 0
55+
options.HealthStartPeriod != 0
5756

5857
if options.NoHealthcheck {
5958
if options.HealthCmd != "" || healthFlagsSet {
@@ -74,9 +73,6 @@ func ValidateHealthcheckFlags(options types.ContainerCreateOptions) error {
7473
if options.HealthStartPeriod < 0 {
7574
return fmt.Errorf("--health-start-period cannot be negative")
7675
}
77-
if options.HealthStartInterval < 0 {
78-
return fmt.Errorf("--health-start-interval cannot be negative")
79-
}
8076
return nil
8177
}
8278

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ require (
120120
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 // indirect
121121
github.com/sasha-s/go-deadlock v0.3.5 // indirect
122122
//gomodjail:unconfined
123-
github.com/sirupsen/logrus v1.9.3 // indirect
123+
github.com/sirupsen/logrus v1.9.3
124124
github.com/smallstep/pkcs7 v0.1.1 // indirect
125125
github.com/spaolacci/murmur3 v1.1.0 // indirect
126126
github.com/stefanberger/go-pkcs11uri v0.0.0-20230803200340-78284954bff6 // indirect

pkg/api/types/container_types.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,12 @@ type ContainerCreateOptions struct {
285285
ImagePullOpt ImagePullOptions
286286

287287
// Healthcheck related fields
288-
HealthCmd string
289-
HealthInterval time.Duration
290-
HealthTimeout time.Duration
291-
HealthRetries int
292-
HealthStartPeriod time.Duration
293-
HealthStartInterval time.Duration
294-
NoHealthcheck bool
288+
HealthCmd string
289+
HealthInterval time.Duration
290+
HealthTimeout time.Duration
291+
HealthRetries int
292+
HealthStartPeriod time.Duration
293+
NoHealthcheck bool
295294

296295
// UserNS name for user namespace mapping of container
297296
UserNS string

pkg/cmd/container/create.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,6 @@ func withHealthcheck(options types.ContainerCreateOptions, ensuredImage *imgutil
891891
if options.HealthStartPeriod != 0 {
892892
hc.StartPeriod = options.HealthStartPeriod
893893
}
894-
if options.HealthStartInterval != 0 {
895-
hc.StartInterval = options.HealthStartInterval
896-
}
897894

898895
// If no healthcheck config is set (via CLI or image), return empty string so we skip adding to container config.
899896
if reflect.DeepEqual(hc, &healthcheck.Healthcheck{}) {

pkg/cmd/container/health_check.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func HealthCheck(ctx context.Context, client *containerd.Client, container conta
5959
hcConfig.Interval = timeoutWithDefault(hcConfig.Interval, healthcheck.DefaultProbeInterval)
6060
hcConfig.Timeout = timeoutWithDefault(hcConfig.Timeout, healthcheck.DefaultProbeTimeout)
6161
hcConfig.StartPeriod = timeoutWithDefault(hcConfig.StartPeriod, healthcheck.DefaultStartPeriod)
62-
hcConfig.StartInterval = timeoutWithDefault(hcConfig.StartInterval, healthcheck.DefaultStartInterval)
6362
if hcConfig.Retries == 0 {
6463
hcConfig.Retries = healthcheck.DefaultProbeRetries
6564
}

pkg/cmd/container/kill.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/containerd/nerdctl/v2/pkg/api/types"
3636
"github.com/containerd/nerdctl/v2/pkg/clientutil"
3737
"github.com/containerd/nerdctl/v2/pkg/containerutil"
38+
"github.com/containerd/nerdctl/v2/pkg/healthcheck"
3839
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
3940
"github.com/containerd/nerdctl/v2/pkg/labels"
4041
"github.com/containerd/nerdctl/v2/pkg/netutil"
@@ -111,6 +112,11 @@ func killContainer(ctx context.Context, container containerd.Container, signal s
111112
return err
112113
}
113114

115+
// Clean up healthcheck systemd units
116+
if err := healthcheck.RemoveTransientHealthCheckFiles(ctx, container); err != nil {
117+
log.G(ctx).Warnf("failed to clean up healthcheck units for container %s: %s", container.ID(), err)
118+
}
119+
114120
// signal will be sent once resume is finished
115121
if paused {
116122
if err := task.Resume(ctx); err != nil {

0 commit comments

Comments
 (0)