Swicth to 1.0.0-alpha2 containerd api.
Signed-off-by: Mike Brown <brownwm@us.ibm.com>
This commit is contained in:
@@ -20,11 +20,13 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/containerd/containerd/api/services/events/v1"
|
||||
"github.com/containerd/containerd/api/services/tasks/v1"
|
||||
"github.com/containerd/containerd/api/types/task"
|
||||
"github.com/containerd/containerd/typeurl"
|
||||
"github.com/golang/glog"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/containerd/containerd/api/services/execution"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||
)
|
||||
|
||||
@@ -77,16 +79,73 @@ func (c *criContainerdService) StopPodSandbox(ctx context.Context, r *runtime.St
|
||||
glog.V(2).Infof("TearDown network for sandbox %q successfully", id)
|
||||
|
||||
sandboxRoot := getSandboxRootDir(c.rootDir, id)
|
||||
if err = c.unmountSandboxFiles(sandboxRoot, sandbox.Config); err != nil {
|
||||
if err := c.unmountSandboxFiles(sandboxRoot, sandbox.Config); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmount sandbox files in %q: %v", sandboxRoot, err)
|
||||
}
|
||||
|
||||
// TODO(random-liu): [P1] Handle sandbox container graceful deletion.
|
||||
// Delete the sandbox container from containerd.
|
||||
_, err = c.taskService.Delete(ctx, &execution.DeleteRequest{ContainerID: id})
|
||||
if err != nil && !isContainerdGRPCNotFoundError(err) {
|
||||
return nil, fmt.Errorf("failed to delete sandbox container %q: %v", id, err)
|
||||
if err := c.stopSandboxContainer(ctx, id); err != nil {
|
||||
return nil, fmt.Errorf("failed to stop sandbox container %q: %v", id, err)
|
||||
}
|
||||
|
||||
return &runtime.StopPodSandboxResponse{}, nil
|
||||
}
|
||||
|
||||
// stopSandboxContainer kills and deletes sandbox container.
|
||||
func (c *criContainerdService) stopSandboxContainer(ctx context.Context, id string) error {
|
||||
cancellable, cancel := context.WithCancel(ctx)
|
||||
eventstream, err := c.eventService.Subscribe(cancellable, &events.SubscribeRequest{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get containerd event: %v", err)
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
resp, err := c.taskService.Get(ctx, &tasks.GetTaskRequest{ContainerID: id})
|
||||
if err != nil {
|
||||
if isContainerdGRPCNotFoundError(err) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to get sandbox container: %v", err)
|
||||
}
|
||||
if resp.Task.Status != task.StatusStopped {
|
||||
// TODO(random-liu): [P1] Handle sandbox container graceful deletion.
|
||||
if _, err := c.taskService.Kill(ctx, &tasks.KillRequest{
|
||||
ContainerID: id,
|
||||
Signal: uint32(unix.SIGKILL),
|
||||
All: true,
|
||||
}); err != nil && !isContainerdGRPCNotFoundError(err) && !isRuncProcessAlreadyFinishedError(err) {
|
||||
return fmt.Errorf("failed to kill sandbox container: %v", err)
|
||||
}
|
||||
|
||||
if err := c.waitSandboxContainer(eventstream, id, resp.Task.Pid); err != nil {
|
||||
return fmt.Errorf("failed to wait for pod sandbox to stop: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the sandbox container from containerd.
|
||||
_, err = c.taskService.Delete(ctx, &tasks.DeleteTaskRequest{ContainerID: id})
|
||||
if err != nil && !isContainerdGRPCNotFoundError(err) {
|
||||
return fmt.Errorf("failed to delete sandbox container: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// waitSandboxContainer wait sandbox container stop event.
|
||||
func (c *criContainerdService) waitSandboxContainer(eventstream events.Events_SubscribeClient, id string, pid uint32) error {
|
||||
for {
|
||||
evt, err := eventstream.Recv()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Continue until the event received is of type task exit.
|
||||
if !typeurl.Is(evt.Event, &events.TaskExit{}) {
|
||||
continue
|
||||
}
|
||||
any, err := typeurl.UnmarshalAny(evt.Event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e := any.(*events.TaskExit)
|
||||
if e.ContainerID == id && e.Pid == pid {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user