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

Commit

Permalink
containerd-shim-kata-v2: cleanup all of the containers in a sandbox
Browse files Browse the repository at this point in the history
There are maybe more than one containers in a sandbox, thus
it's needed to cleanup all of those containers' mount and
id mapping files when do cleanup a sandbox.

Fixes: kata-containers#572

Signed-off-by: fupan <[email protected]>
  • Loading branch information
lifupan committed Aug 10, 2018
1 parent 76ffe80 commit 8fdf25e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
5 changes: 4 additions & 1 deletion cli/containerd-shim-kata-v2/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

// Copyright (c) 2018 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//

package main

Expand Down
2 changes: 1 addition & 1 deletion containerd-shim/kata/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
vc "github.com/kata-containers/runtime/virtcontainers"
vf "github.com/kata-containers/runtime/virtcontainers/factory"
"github.com/kata-containers/runtime/virtcontainers/pkg/oci"
"github.com/sirupsen/logrus"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
)

func create(s *service, containerID, bundlePath, netns string, detach bool,
Expand Down
4 changes: 2 additions & 2 deletions containerd-shim/kata/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ func delContainerIDMapping(containerID string) error {
return os.RemoveAll(path)
}

func removeNameSpace(s *oci.CompatOCISpec, nsType specs.LinuxNamespaceType){
func removeNameSpace(s *oci.CompatOCISpec, nsType specs.LinuxNamespaceType) {
for i, n := range s.Linux.Namespaces {
if n.Type == nsType {
s.Linux.Namespaces = append(s.Linux.Namespaces[:i], s.Linux.Namespaces[i+1:]...)
return
}
}
}
}
26 changes: 19 additions & 7 deletions containerd-shim/kata/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
"github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/containerd/typeurl"
ptypes "github.com/gogo/protobuf/types"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"path/filepath"
"github.com/opencontainers/runtime-spec/specs-go"
)

const bufferSize = 32
Expand Down Expand Up @@ -266,16 +266,25 @@ func (s *service) Cleanup(ctx context.Context) (*taskAPI.DeleteResponse, error)
return nil, errdefs.ToGRPCf(errdefs.ErrInvalidArgument, "the container id is empty, please specify the container id")
}

var containers []vc.VCContainer

path, err := os.Getwd()
if err != nil {
return nil, err
}

status, err := vci.StatusSandbox(s.id)
//get the bundle parent path, thus we can form a specific
//container's bundle path by "bundleParentPath/id"
bundleParentPath := filepath.Dir(path)

sandbox, err := vci.FetchSandbox(s.id)
if err != nil {
return nil, err
}

containers = sandbox.GetAllContainers()
status := sandbox.Status()

if oci.StateToOCIState(status.State) != oci.StateStopped {
if _, err := vci.StopSandbox(s.id); err != nil {
logrus.WithError(err).Warn("failed to stop kata container")
Expand All @@ -286,12 +295,15 @@ func (s *service) Cleanup(ctx context.Context) (*taskAPI.DeleteResponse, error)
logrus.WithError(err).Warn("failed to remove kata container")
}

if err := delContainerIDMapping(s.id); err != nil {
logrus.WithError(err).Warn("failed to remove kata container id mapping files")
}
for _, c := range containers {
if err := delContainerIDMapping(s.id); err != nil {
logrus.WithError(err).Warnf("failed to remove kata container %s id mapping files", c.ID())
}

if err := mount.UnmountAll(filepath.Join(path, "rootfs"), 0); err != nil {
logrus.WithError(err).Warn("failed to cleanup rootfs mount")
rootfs := filepath.Join(bundleParentPath, c.ID(), "rootfs")
if err := mount.UnmountAll(rootfs, 0); err != nil {
logrus.WithError(err).Warnf("failed to cleanup container %s rootfs mount", c.ID())
}
}

return &taskAPI.DeleteResponse{
Expand Down

0 comments on commit 8fdf25e

Please sign in to comment.