Skip to content

Commit 32becea

Browse files
committed
optimize: optimize healthcheck
Signed-off-by: ningmingxiao <ning.mingxiao@zte.com.cn>
1 parent 5bcf21b commit 32becea

7 files changed

Lines changed: 59 additions & 39 deletions

File tree

cmd/nerdctl/container/container_run.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,14 @@ func runAction(cmd *cobra.Command, args []string) error {
480480
return err
481481
}
482482

483-
// Setup container healthchecks.
484-
if err := healthcheck.CreateTimer(ctx, c, (*config.Config)(&createOpt.GOptions), createOpt.NerdctlCmd, createOpt.NerdctlArgs); err != nil {
485-
return fmt.Errorf("failed to create healthcheck timer: %w", err)
486-
}
487-
if err := healthcheck.StartTimer(ctx, c, (*config.Config)(&createOpt.GOptions)); err != nil {
488-
return fmt.Errorf("failed to start healthcheck timer: %w", err)
483+
if hcStr, ok := lab[labels.HealthCheck]; ok && hcStr != "" {
484+
// Setup container healthchecks.
485+
if err := healthcheck.CreateTimer(ctx, c, (*config.Config)(&createOpt.GOptions), createOpt.NerdctlCmd, createOpt.NerdctlArgs, lab); err != nil {
486+
return fmt.Errorf("failed to create healthcheck timer: %w", err)
487+
}
488+
if err := healthcheck.StartTimer(ctx, c, (*config.Config)(&createOpt.GOptions), lab); err != nil {
489+
return fmt.Errorf("failed to start healthcheck timer: %w", err)
490+
}
489491
}
490492

491493
if createOpt.Detach {

pkg/cmd/container/create.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,15 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
355355
}
356356
cOpts = append(cOpts, rtCOpts...)
357357

358-
// Generate health check config based on CLI flags and image.
359-
healthcheckConfig, err := withHealthcheck(options, ensuredImage)
360-
if err != nil {
361-
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
362-
}
363-
if healthcheckConfig != "" {
364-
internalLabels.healthcheck = healthcheckConfig
358+
if options.HealthCmd != "" {
359+
// Generate health check config based on CLI flags and image.
360+
healthcheckConfig, err := withHealthcheck(options, ensuredImage)
361+
if err != nil {
362+
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
363+
}
364+
if healthcheckConfig != "" {
365+
internalLabels.healthcheck = healthcheckConfig
366+
}
365367
}
366368

367369
lCOpts, err := withContainerLabels(options.Label, options.LabelFile)

pkg/containerutil/containerutil.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ func Start(ctx context.Context, container containerd.Container, isAttach bool, i
292292
return err
293293
}
294294

295+
if hcStr, ok := lab[labels.HealthCheck]; ok && hcStr != "" {
296+
// If container has health checks configured, create and start systemd timer/service files.
297+
if err := healthcheck.CreateTimer(ctx, container, cfg, nerdctlCmd, nerdctlArgs, lab); err != nil {
298+
return fmt.Errorf("failed to create healthcheck timer: %w", err)
299+
}
300+
if err := healthcheck.StartTimer(ctx, container, cfg, lab); err != nil {
301+
return fmt.Errorf("failed to start healthcheck timer: %w", err)
302+
}
303+
}
304+
295305
// Set status label running should call after task is started.
296306
_, restartPolicyExist := lab[restart.PolicyLabel]
297307
if restartPolicyExist {
@@ -303,14 +313,6 @@ func Start(ctx context.Context, container containerd.Container, isAttach bool, i
303313
return err
304314
}
305315

306-
// If container has health checks configured, create and start systemd timer/service files.
307-
if err := healthcheck.CreateTimer(ctx, container, cfg, nerdctlCmd, nerdctlArgs); err != nil {
308-
return fmt.Errorf("failed to create healthcheck timer: %w", err)
309-
}
310-
if err := healthcheck.StartTimer(ctx, container, cfg); err != nil {
311-
return fmt.Errorf("failed to start healthcheck timer: %w", err)
312-
}
313-
314316
if !isAttach {
315317
return nil
316318
}
@@ -542,12 +544,20 @@ func Unpause(ctx context.Context, client *containerd.Client, id string, cfg *con
542544
return err
543545
}
544546

545-
// Recreate healthcheck related systemd timer/service files.
546-
if err := healthcheck.CreateTimer(ctx, container, cfg, nerdctlCmd, nerdctlArgs); err != nil {
547-
return fmt.Errorf("failed to create healthcheck timer: %w", err)
547+
label, err := container.Labels(ctx)
548+
if err != nil {
549+
return err
548550
}
549-
if err := healthcheck.StartTimer(ctx, container, cfg); err != nil {
550-
return fmt.Errorf("failed to start healthcheck timer: %w", err)
551+
552+
hcStr, ok := label[labels.HealthCheck]
553+
if !ok || hcStr == "" {
554+
// Recreate healthcheck related systemd timer/service files.
555+
if err := healthcheck.CreateTimer(ctx, container, cfg, nerdctlCmd, nerdctlArgs, label); err != nil {
556+
return fmt.Errorf("failed to create healthcheck timer: %w", err)
557+
}
558+
if err := healthcheck.StartTimer(ctx, container, cfg, label); err != nil {
559+
return fmt.Errorf("failed to start healthcheck timer: %w", err)
560+
}
551561
}
552562

553563
switch status.Status {

pkg/healthcheck/healthcheck_manager_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
// CreateTimer sets up the transient systemd timer and service for healthchecks.
28-
func CreateTimer(ctx context.Context, container containerd.Container, cfg *config.Config, nerdctlCmd string, nerdctlArgs []string) error {
28+
func CreateTimer(ctx context.Context, container containerd.Container, cfg *config.Config, nerdctlCmd string, nerdctlArgs []string, label map[string]string) error {
2929
return nil
3030
}
3131

pkg/healthcheck/healthcheck_manager_freebsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
// CreateTimer sets up the transient systemd timer and service for healthchecks.
28-
func CreateTimer(ctx context.Context, container containerd.Container, cfg *config.Config, nerdctlCmd string, nerdctlArgs []string) error {
28+
func CreateTimer(ctx context.Context, container containerd.Container, cfg *config.Config, nerdctlCmd string, nerdctlArgs []string, label map[string]string) error {
2929
return nil
3030
}
3131

pkg/healthcheck/healthcheck_manager_linux.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import (
3636
)
3737

3838
// CreateTimer sets up the transient systemd timer and service for healthchecks.
39-
func CreateTimer(ctx context.Context, container containerd.Container, cfg *config.Config, nerdctlCmd string, nerdctlArgs []string) error {
40-
hc := extractHealthcheck(ctx, container)
39+
func CreateTimer(ctx context.Context, container containerd.Container, cfg *config.Config, nerdctlCmd string, nerdctlArgs []string, label map[string]string) error {
40+
hc := extractHealthcheck(ctx, container, label)
4141
if hc == nil {
4242
return nil
4343
}
@@ -106,8 +106,8 @@ func createDbusConn(ctx context.Context) (*dbus.Conn, error) {
106106
}
107107

108108
// StartTimer starts the healthcheck timer unit.
109-
func StartTimer(ctx context.Context, container containerd.Container, cfg *config.Config) error {
110-
hc := extractHealthcheck(ctx, container)
109+
func StartTimer(ctx context.Context, container containerd.Container, cfg *config.Config, label map[string]string) error {
110+
hc := extractHealthcheck(ctx, container, label)
111111
if hc == nil {
112112
return nil
113113
}
@@ -135,7 +135,7 @@ func StartTimer(ctx context.Context, container containerd.Container, cfg *config
135135

136136
// RemoveTransientHealthCheckFiles stops and cleans up the transient timer and service.
137137
func RemoveTransientHealthCheckFiles(ctx context.Context, container containerd.Container) error {
138-
hc := extractHealthcheck(ctx, container)
138+
hc := extractHealthcheck(ctx, container, nil)
139139
if hc == nil {
140140
return nil
141141
}
@@ -254,11 +254,17 @@ func ForceRemoveTransientHealthCheckFiles(ctx context.Context, containerID strin
254254
return nil
255255
}
256256

257-
func extractHealthcheck(ctx context.Context, container containerd.Container) *Healthcheck {
258-
l, err := container.Labels(ctx)
259-
if err != nil {
260-
log.G(ctx).WithError(err).Debugf("could not get labels for container %s", container.ID())
261-
return nil
257+
func extractHealthcheck(ctx context.Context, container containerd.Container, label map[string]string) *Healthcheck {
258+
var l map[string]string
259+
var err error
260+
if label == nil {
261+
l, err = container.Labels(ctx)
262+
if err != nil {
263+
log.G(ctx).WithError(err).Debugf("could not get labels for container %s", container.ID())
264+
return nil
265+
}
266+
} else {
267+
l = label
262268
}
263269
hcStr, ok := l[labels.HealthCheck]
264270
if !ok || hcStr == "" {

pkg/healthcheck/healthcheck_manager_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
// CreateTimer sets up the transient systemd timer and service for healthchecks.
28-
func CreateTimer(ctx context.Context, container containerd.Container, cfg *config.Config, nerdctlCmd string, nerdctlArgs []string) error {
28+
func CreateTimer(ctx context.Context, container containerd.Container, cfg *config.Config, nerdctlCmd string, nerdctlArgs []string, label map[string]string) error {
2929
return nil
3030
}
3131

0 commit comments

Comments
 (0)