Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
containerd-shim-kata-v2: add the service Delete support
Browse files Browse the repository at this point in the history
Add the Delete api support to delete a stopped
container or pod.

Signed-off-by: fupan <[email protected]>
  • Loading branch information
lifupan committed Nov 28, 2018
1 parent fd18b22 commit a0e6456
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
49 changes: 49 additions & 0 deletions containerd-shim-v2/delete.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 33 additions & 1 deletion containerd-shim-v2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a0e6456

Please sign in to comment.