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
3 changes: 2 additions & 1 deletion cmd/nerdctl/restart.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/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -67,7 +68,7 @@ func restartAction(cmd *cobra.Command, args []string) error {
if found.MatchCount > 1 {
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
}
if err := stopContainer(ctx, found.Container, timeout); err != nil {
if err := containerutil.Stop(ctx, found.Container, timeout); err != nil {
return err
}
if err := startContainer(ctx, found.Container, false, client); err != nil {
Expand Down
125 changes: 1 addition & 124 deletions cmd/nerdctl/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ import (
"github.com/spf13/cobra"

"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/containerd/nerdctl/pkg/labels"

"github.com/moby/sys/signal"
"github.com/sirupsen/logrus"
)

func newStopCommand() *cobra.Command {
Expand Down Expand Up @@ -76,7 +71,7 @@ func stopAction(cmd *cobra.Command, args []string) error {
if found.MatchCount > 1 {
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
}
if err := stopContainer(ctx, found.Container, timeout); err != nil {
if err := containerutil.Stop(ctx, found.Container, timeout); err != nil {
if errdefs.IsNotFound(err) {
fmt.Fprintf(cmd.ErrOrStderr(), "No such container: %s\n", found.Req)
return nil
Expand All @@ -98,124 +93,6 @@ func stopAction(cmd *cobra.Command, args []string) error {
return nil
}

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

l, err := container.Labels(ctx)
if err != nil {
return err
}

if timeout == nil {
t, ok := l[labels.StopTimout]
if !ok {
// Default is 10 seconds.
t = "10"
}
td, err := time.ParseDuration(t + "s")
if err != nil {
return err
}
timeout = &td
}

task, err := container.Task(ctx, cio.Load)
if err != nil {
return err
}

status, err := task.Status(ctx)
if err != nil {
return err
}

paused := false

switch status.Status {
case containerd.Created, containerd.Stopped:
return nil
case containerd.Paused, containerd.Pausing:
paused = true
default:
}

// NOTE: ctx is main context so that it's ok to use for task.Wait().
exitCh, err := task.Wait(ctx)
if err != nil {
return err
}

if *timeout > 0 {
sig, err := signal.ParseSignal("SIGTERM")
if err != nil {
return err
}
if stopSignal, ok := l[containerd.StopSignalLabel]; ok {
sig, err = signal.ParseSignal(stopSignal)
if err != nil {
return err
}
}

if err := task.Kill(ctx, sig); err != nil {
return err
}

// signal will be sent once resume is finished
if paused {
if err := task.Resume(ctx); err != nil {
logrus.Warnf("Cannot unpause container %s: %s", container.ID(), err)
} else {
// no need to do it again when send sigkill signal
paused = false
}
}

sigtermCtx, sigtermCtxCancel := context.WithTimeout(ctx, *timeout)
defer sigtermCtxCancel()

err = waitContainerStop(sigtermCtx, exitCh, container.ID())
if err == nil {
return nil
}

if ctx.Err() != nil {
return ctx.Err()
}
}

sig, err := signal.ParseSignal("SIGKILL")
if err != nil {
return err
}

if err := task.Kill(ctx, sig); err != nil {
return err
}

// signal will be sent once resume is finished
if paused {
if err := task.Resume(ctx); err != nil {
logrus.Warnf("Cannot unpause container %s: %s", container.ID(), err)
}
}
return waitContainerStop(ctx, exitCh, container.ID())
}

func waitContainerStop(ctx context.Context, exitCh <-chan containerd.ExitStatus, id string) error {
select {
case <-ctx.Done():
if err := ctx.Err(); err != nil {
return fmt.Errorf("wait container %v: %w", id, err)
}
return nil
case status := <-exitCh:
return status.Error()
}
}

func stopShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// show non-stopped container names
statusFilterFn := func(st containerd.ProcessStatus) bool {
Expand Down
123 changes: 123 additions & 0 deletions pkg/containerutil/containerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ import (
"time"

"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/runtime/restart"
"github.com/containerd/nerdctl/pkg/labels"
"github.com/containerd/nerdctl/pkg/portutil"
"github.com/containerd/nerdctl/pkg/rootlessutil"
"github.com/moby/sys/signal"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
)

// PrintHostPort writes to `writer` the public (HostIP:HostPort) of a given `containerPort/protocol` in a container.
Expand Down Expand Up @@ -174,3 +178,122 @@ func GenerateSharingPIDOpts(ctx context.Context, targetCon containerd.Container)
}
return opts, nil
}

// Stop stops `container` by sending SIGTERM. If the container is not stopped after `timeout`, it sends a SIGKILL.
func Stop(ctx context.Context, container containerd.Container, timeout *time.Duration) error {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some comments here, something like

// Stop stops `container` by sending SIGTERM. If the container is not stopped after `timeout`, it sends a SIGKILL.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks. Updated.

if err := UpdateExplicitlyStoppedLabel(ctx, container, true); err != nil {
return err
}

l, err := container.Labels(ctx)
if err != nil {
return err
}

if timeout == nil {
t, ok := l[labels.StopTimout]
if !ok {
// Default is 10 seconds.
t = "10"
}
td, err := time.ParseDuration(t + "s")
if err != nil {
return err
}
timeout = &td
}

task, err := container.Task(ctx, cio.Load)
if err != nil {
return err
}

status, err := task.Status(ctx)
if err != nil {
return err
}

paused := false

switch status.Status {
case containerd.Created, containerd.Stopped:
return nil
case containerd.Paused, containerd.Pausing:
paused = true
default:
}

// NOTE: ctx is main context so that it's ok to use for task.Wait().
exitCh, err := task.Wait(ctx)
if err != nil {
return err
}

if *timeout > 0 {
sig, err := signal.ParseSignal("SIGTERM")
if err != nil {
return err
}
if stopSignal, ok := l[containerd.StopSignalLabel]; ok {
sig, err = signal.ParseSignal(stopSignal)
if err != nil {
return err
}
}

if err := task.Kill(ctx, sig); err != nil {
return err
}

// signal will be sent once resume is finished
if paused {
if err := task.Resume(ctx); err != nil {
logrus.Warnf("Cannot unpause container %s: %s", container.ID(), err)
} else {
// no need to do it again when send sigkill signal
paused = false
}
}

sigtermCtx, sigtermCtxCancel := context.WithTimeout(ctx, *timeout)
defer sigtermCtxCancel()

err = waitContainerStop(sigtermCtx, exitCh, container.ID())
if err == nil {
return nil
}

if ctx.Err() != nil {
return ctx.Err()
}
}

sig, err := signal.ParseSignal("SIGKILL")
if err != nil {
return err
}

if err := task.Kill(ctx, sig); err != nil {
return err
}

// signal will be sent once resume is finished
if paused {
if err := task.Resume(ctx); err != nil {
logrus.Warnf("Cannot unpause container %s: %s", container.ID(), err)
}
}
return waitContainerStop(ctx, exitCh, container.ID())
}

func waitContainerStop(ctx context.Context, exitCh <-chan containerd.ExitStatus, id string) error {
select {
case <-ctx.Done():
if err := ctx.Err(); err != nil {
return fmt.Errorf("wait container %v: %w", id, err)
}
return nil
case status := <-exitCh:
return status.Error()
}
}