forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
54 lines (44 loc) · 1.29 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 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"
"github.com/kata-containers/runtime/virtcontainers/types"
"github.com/sirupsen/logrus"
)
func deleteContainer(ctx context.Context, s *service, c *container) error {
status, err := s.sandbox.StatusContainer(c.id)
if err != nil && !isNotFound(err) {
return err
}
if !c.cType.IsSandbox() && err == nil {
if status.State.State != types.StateStopped {
_, err = s.sandbox.StopContainer(c.id, false)
if err != nil {
return err
}
}
if _, err = s.sandbox.DeleteContainer(c.id); err != nil {
return err
}
}
// Run post-stop OCI hooks.
if err := katautils.PostStopHooks(ctx, *c.spec, s.sandbox.ID(), c.bundle); err != nil {
// log warning and continue, as defined in oci runtime spec
// https://github.com/opencontainers/runtime-spec/blob/master/runtime.md#lifecycle
logrus.WithError(err).Warn("Failed to run post-stop hooks")
}
if c.mounted {
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
}