Skip to content

Commit 7acc864

Browse files
committed
fix: update status label should call after task is started
Signed-off-by: ningmingxiao <ning.mingxiao@zte.com.cn>
1 parent 0b388bb commit 7acc864

4 files changed

Lines changed: 26 additions & 15 deletions

File tree

cmd/nerdctl/container/container_run.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/containerd/console"
2929
containerd "github.com/containerd/containerd/v2/client"
30+
"github.com/containerd/containerd/v2/core/runtime/restart"
3031
"github.com/containerd/log"
3132

3233
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
@@ -467,6 +468,18 @@ func runAction(cmd *cobra.Command, args []string) error {
467468
return err
468469
}
469470

471+
// Set status label running should call after task is started.
472+
_, restartPolicyExist := lab[restart.PolicyLabel]
473+
if restartPolicyExist {
474+
if err := containerutil.UpdateStatusLabel(ctx, c, containerd.Running); err != nil {
475+
return err
476+
}
477+
}
478+
479+
if err := containerutil.UpdateExplicitlyStoppedLabel(ctx, c, false); err != nil {
480+
return err
481+
}
482+
470483
// Setup container healthchecks.
471484
if err := healthcheck.CreateTimer(ctx, c, (*config.Config)(&createOpt.GOptions), createOpt.NerdctlCmd, createOpt.NerdctlArgs); err != nil {
472485
return fmt.Errorf("failed to create healthcheck timer: %w", err)

pkg/cmd/container/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
280280
internalLabels.logConfig.Driver = "json-file"
281281
}
282282

283-
restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun)
283+
restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI)
284284
if err != nil {
285285
return nil, generateRemoveStateDirFunc(ctx, id, internalLabels), err
286286
}

pkg/cmd/container/run_restart.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func checkRestartCapabilities(ctx context.Context, client *containerd.Client, re
5151
return true, nil
5252
}
5353

54-
func generateRestartOpts(ctx context.Context, client *containerd.Client, restartFlag, logURI string, inRun bool) ([]containerd.NewContainerOpts, error) {
54+
func generateRestartOpts(ctx context.Context, client *containerd.Client, restartFlag, logURI string) ([]containerd.NewContainerOpts, error) {
5555
if restartFlag == "" || restartFlag == "no" {
5656
return nil, nil
5757
}
@@ -64,9 +64,6 @@ func generateRestartOpts(ctx context.Context, client *containerd.Client, restart
6464
return nil, err
6565
}
6666
desireStatus := containerd.Created
67-
if inRun {
68-
desireStatus = containerd.Running
69-
}
7067
opts := []containerd.NewContainerOpts{restart.WithPolicy(policy), restart.WithStatus(desireStatus)}
7168
if logURI != "" {
7269
opts = append(opts, restart.WithLogURIString(logURI))

pkg/containerutil/containerutil.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,6 @@ func Start(ctx context.Context, container containerd.Container, isAttach bool, i
257257
return nil
258258
}
259259

260-
_, restartPolicyExist := lab[restart.PolicyLabel]
261-
if restartPolicyExist {
262-
if err := UpdateStatusLabel(ctx, container, containerd.Running); err != nil {
263-
return err
264-
}
265-
}
266-
267-
if err := UpdateExplicitlyStoppedLabel(ctx, container, false); err != nil {
268-
return err
269-
}
270260
if oldTask, err := container.Task(ctx, nil); err == nil {
271261
if _, err := oldTask.Delete(ctx); err != nil {
272262
log.G(ctx).WithError(err).Debug("failed to delete old task")
@@ -302,6 +292,17 @@ func Start(ctx context.Context, container containerd.Container, isAttach bool, i
302292
return err
303293
}
304294

295+
// Set status label running should call after task is started.
296+
_, restartPolicyExist := lab[restart.PolicyLabel]
297+
if restartPolicyExist {
298+
if err := UpdateStatusLabel(ctx, container, containerd.Running); err != nil {
299+
return err
300+
}
301+
}
302+
if err := UpdateExplicitlyStoppedLabel(ctx, container, false); err != nil {
303+
return err
304+
}
305+
305306
// If container has health checks configured, create and start systemd timer/service files.
306307
if err := healthcheck.CreateTimer(ctx, container, cfg, nerdctlCmd, nerdctlArgs); err != nil {
307308
return fmt.Errorf("failed to create healthcheck timer: %w", err)

0 commit comments

Comments
 (0)