Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions cmd/nerdctl/run_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"context"
"fmt"
"strconv"
"strings"

"github.com/containerd/containerd"
Expand Down Expand Up @@ -70,13 +69,6 @@ func generateRestartOpts(ctx context.Context, client *containerd.Client, restart
return opts, nil
}

func updateContainerStoppedLabel(ctx context.Context, container containerd.Container, stopped bool) error {
opt := containerd.WithAdditionalContainerLabels(map[string]string{
restart.ExplicitlyStoppedLabel: strconv.FormatBool(stopped),
})
return container.Update(ctx, containerd.UpdateContainerOpts(opt))
}

func updateContainerRestartPolicyLabel(ctx context.Context, client *containerd.Client, container containerd.Container, restartFlag string) error {
if _, err := checkRestartCapabilities(ctx, client, restartFlag); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func startContainer(ctx context.Context, container containerd.Container, flagA b
logrus.Warnf("container %s is already running", container.ID())
return nil
}
if err := updateContainerStoppedLabel(ctx, container, false); err != nil {
if err := containerutil.UpdateExplicitlyStoppedLabel(ctx, container, false); err != nil {
return err
}
if oldTask, err := container.Task(ctx, nil); err == nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/nerdctl/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/containerd/nerdctl/pkg/clientutil"
"github.com/containerd/nerdctl/pkg/containerutil"
"github.com/spf13/cobra"

"github.com/containerd/containerd"
Expand Down Expand Up @@ -98,7 +99,7 @@ func stopAction(cmd *cobra.Command, args []string) error {
}

func stopContainer(ctx context.Context, container containerd.Container, timeout *time.Duration) error {
if err := updateContainerStoppedLabel(ctx, container, true); err != nil {
if err := containerutil.UpdateExplicitlyStoppedLabel(ctx, container, true); err != nil {
return err
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/containerutil/containerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"context"
"fmt"
"io"
"strconv"
"strings"
"time"

"github.com/containerd/containerd"
"github.com/containerd/containerd/runtime/restart"
"github.com/containerd/nerdctl/pkg/portutil"
)

Expand Down Expand Up @@ -84,3 +86,12 @@ func ContainerNetNSPath(ctx context.Context, c containerd.Container) (string, er
}
return fmt.Sprintf("/proc/%d/ns/net", task.Pid()), nil
}

// 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 {
opt := containerd.WithAdditionalContainerLabels(map[string]string{
restart.ExplicitlyStoppedLabel: strconv.FormatBool(explicitlyStopped),
})
return container.Update(ctx, containerd.UpdateContainerOpts(opt))
}