Skip to content

Commit 54d6cfa

Browse files
committed
optimize: reduce unnecessary function calls
Signed-off-by: ningmingxiao <ning.mingxiao@zte.com.cn>
1 parent 5bcf21b commit 54d6cfa

6 files changed

Lines changed: 56 additions & 38 deletions

File tree

cmd/nerdctl/container/container_run.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,13 @@ func runAction(cmd *cobra.Command, args []string) error {
458458
if err != nil {
459459
return err
460460
}
461-
462-
statusC, err := task.Wait(ctx)
463-
if err != nil {
464-
return err
461+
var statusC <-chan containerd.ExitStatus
462+
if !createOpt.Detach {
463+
statusC, err = task.Wait(ctx)
464+
if err != nil {
465+
return err
466+
}
465467
}
466-
467468
if err := task.Start(ctx); err != nil {
468469
return err
469470
}
@@ -480,12 +481,14 @@ func runAction(cmd *cobra.Command, args []string) error {
480481
return err
481482
}
482483

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

491494
if createOpt.Detach {

pkg/containerutil/containerutil.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,15 @@ func Start(ctx context.Context, container containerd.Container, isAttach bool, i
304304
}
305305

306306
// 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)
307+
if hcStr, ok := lab[labels.HealthCheck]; ok && hcStr != "" {
308+
// If container has health checks configured, create and start systemd timer/service files.
309+
if err := healthcheck.CreateTimer(ctx, container, cfg, nerdctlCmd, nerdctlArgs, lab); err != nil {
310+
return fmt.Errorf("failed to create healthcheck timer: %w", err)
311+
}
312+
if err := healthcheck.StartTimer(ctx, container, cfg, lab); err != nil {
313+
return fmt.Errorf("failed to start healthcheck timer: %w", err)
314+
}
312315
}
313-
314316
if !isAttach {
315317
return nil
316318
}
@@ -542,12 +544,19 @@ 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+
if hcStr, ok := label[labels.HealthCheck]; ok && hcStr != "" {
553+
// Recreate healthcheck related systemd timer/service files.
554+
if err := healthcheck.CreateTimer(ctx, container, cfg, nerdctlCmd, nerdctlArgs, label); err != nil {
555+
return fmt.Errorf("failed to create healthcheck timer: %w", err)
556+
}
557+
if err := healthcheck.StartTimer(ctx, container, cfg, label); err != nil {
558+
return fmt.Errorf("failed to start healthcheck timer: %w", err)
559+
}
551560
}
552561

553562
switch status.Status {

pkg/healthcheck/healthcheck_manager_darwin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ 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

3232
// StartTimer starts the healthcheck timer unit.
33-
func StartTimer(ctx context.Context, container containerd.Container, cfg *config.Config) error {
33+
func StartTimer(ctx context.Context, container containerd.Container, cfg *config.Config, label map[string]string) error {
3434
return nil
3535
}
3636

pkg/healthcheck/healthcheck_manager_freebsd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ 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

3232
// StartTimer starts the healthcheck timer unit.
33-
func StartTimer(ctx context.Context, container containerd.Container, cfg *config.Config) error {
33+
func StartTimer(ctx context.Context, container containerd.Container, cfg *config.Config, label map[string]string) error {
3434
return nil
3535
}
3636

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ 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

3232
// StartTimer starts the healthcheck timer unit.
33-
func StartTimer(ctx context.Context, container containerd.Container, cfg *config.Config) error {
33+
func StartTimer(ctx context.Context, container containerd.Container, cfg *config.Config, label map[string]string) error {
3434
return nil
3535
}
3636

0 commit comments

Comments
 (0)