From a0e645645530496bff80b07e400a56d456340112 Mon Sep 17 00:00:00 2001 From: fupan Date: Mon, 19 Nov 2018 11:17:40 +0800 Subject: [PATCH] containerd-shim-kata-v2: add the service Delete support Add the Delete api support to delete a stopped container or pod. Signed-off-by: fupan --- containerd-shim-v2/delete.go | 49 +++++++++++++++++++++++++++++++++++ containerd-shim-v2/service.go | 34 +++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 containerd-shim-v2/delete.go diff --git a/containerd-shim-v2/delete.go b/containerd-shim-v2/delete.go new file mode 100644 index 0000000000..7d2fa491b5 --- /dev/null +++ b/containerd-shim-v2/delete.go @@ -0,0 +1,49 @@ +// Copyright (c) 2018 HyperHQ Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// + +package containerdshim + +import ( + "context" + "path" + + "github.com/containerd/containerd/mount" + "github.com/kata-containers/runtime/pkg/katautils" + vc "github.com/kata-containers/runtime/virtcontainers" + "github.com/sirupsen/logrus" +) + +func deleteContainer(ctx context.Context, s *service, c *container) error { + + status, err := s.sandbox.StatusContainer(c.id) + if err != nil { + return err + } + if status.State.State != vc.StateStopped { + _, err = s.sandbox.StopContainer(c.id) + if err != nil { + return err + } + } + + _, err = s.sandbox.DeleteContainer(c.id) + if err != nil { + return err + } + + // Run post-stop OCI hooks. + if err := katautils.PostStopHooks(ctx, *c.spec, s.sandbox.ID(), c.bundle); err != nil { + return err + } + + rootfs := path.Join(c.bundle, "rootfs") + if err := mount.UnmountAll(rootfs, 0); err != nil { + logrus.WithError(err).Warn("failed to cleanup rootfs mount") + } + + delete(s.containers, c.id) + + return nil +} diff --git a/containerd-shim-v2/service.go b/containerd-shim-v2/service.go index e8870dd058..0fd9237e44 100644 --- a/containerd-shim-v2/service.go +++ b/containerd-shim-v2/service.go @@ -313,7 +313,39 @@ func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (*taskAPI. // Delete the initial process and container func (s *service) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (*taskAPI.DeleteResponse, error) { - return nil, errdefs.ErrNotImplemented + s.Lock() + defer s.Unlock() + + c, err := s.getContainer(r.ID) + if err != nil { + return nil, err + } + + if r.ExecID == "" { + err = deleteContainer(ctx, s, c) + if err != nil { + return nil, err + } + + return &taskAPI.DeleteResponse{ + ExitStatus: c.exit, + ExitedAt: c.time, + Pid: s.pid, + }, nil + } + //deal with the exec case + execs, err := c.getExec(r.ExecID) + if err != nil { + return nil, err + } + + delete(c.execs, r.ExecID) + + return &taskAPI.DeleteResponse{ + ExitStatus: uint32(execs.exitCode), + ExitedAt: execs.exitTime, + Pid: s.pid, + }, nil } // Exec an additional process inside the container