Skip to content

Commit

Permalink
fix restart policy behaviour inconsistent
Browse files Browse the repository at this point in the history
Signed-off-by: Kay Yan <kay.yan@daocloud.io>
  • Loading branch information
yankay committed Jun 16, 2023
1 parent 9f8655f commit 5c4f460
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cmd/nerdctl/container_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ func processCreateCommandFlagsInRun(cmd *cobra.Command) (opt types.ContainerCrea
if err != nil {
return
}

opt.InRun = true

opt.Interactive, err = cmd.Flags().GetBool("interactive")
if err != nil {
return
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/types/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type ContainerCreateOptions struct {
// NerdctlArgs is the arguments of nerdctl
NerdctlArgs []string

// InRun is true when it's generated in the `run` command
InRun bool

// #region for basic flags
// Interactive keep STDIN open even if not attached
Interactive bool
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
}
internalLabels.logURI = logConfig.LogURI

restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI)
restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun)
if err != nil {
return nil, nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/container/run_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func checkRestartCapabilities(ctx context.Context, client *containerd.Client, re
return true, nil
}

func generateRestartOpts(ctx context.Context, client *containerd.Client, restartFlag, logURI string) ([]containerd.NewContainerOpts, error) {
func generateRestartOpts(ctx context.Context, client *containerd.Client, restartFlag, logURI string, inRun bool) ([]containerd.NewContainerOpts, error) {
if restartFlag == "" || restartFlag == "no" {
return nil, nil
}
Expand All @@ -62,7 +62,11 @@ func generateRestartOpts(ctx context.Context, client *containerd.Client, restart
if err != nil {
return nil, err
}
opts := []containerd.NewContainerOpts{restart.WithPolicy(policy), restart.WithStatus(containerd.Running)}
desireStatus := containerd.Created
if inRun {
desireStatus = containerd.Running
}
opts := []containerd.NewContainerOpts{restart.WithPolicy(policy), restart.WithStatus(desireStatus)}
if logURI != "" {
opts = append(opts, restart.WithLogURIString(logURI))
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/containerutil/containerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ func ContainerNetNSPath(ctx context.Context, c containerd.Container) (string, er
return fmt.Sprintf("/proc/%d/ns/net", task.Pid()), nil
}

// UpdateStatusLabel updates the "containerd.io/restart.status"
// label of the container according to the value of restart desired status.
func UpdateStatusLabel(ctx context.Context, container containerd.Container, status containerd.ProcessStatus) error {
opt := containerd.WithAdditionalContainerLabels(map[string]string{
restart.StatusLabel: string(status),
})
return container.Update(ctx, containerd.UpdateContainerOpts(opt))
}

// UpdateExplicitlyStoppedLabel updates the "containerd.io/restart.explicitly-stopped"
// label of the container according to the value of explicitlyStopped.
func UpdateExplicitlyStoppedLabel(ctx context.Context, container containerd.Container, explicitlyStopped bool) error {
Expand Down Expand Up @@ -205,6 +214,10 @@ func Start(ctx context.Context, container containerd.Container, flagA bool, clie
UpdateErrorLabel(ctx, container, err)
}
}()
if err := UpdateStatusLabel(ctx, container, containerd.Running); err != nil {
return err
}

lab, err := container.Labels(ctx)
if err != nil {
return err
Expand Down

0 comments on commit 5c4f460

Please sign in to comment.