1/*
2   Copyright The containerd Authors.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17package server
18
19import (
20	"github.com/containerd/containerd"
21	"github.com/containerd/containerd/errdefs"
22	"github.com/containerd/containerd/log"
23	"github.com/pkg/errors"
24	"github.com/sirupsen/logrus"
25	"golang.org/x/net/context"
26	runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
27
28	"github.com/containerd/containerd/pkg/cri/store"
29	containerstore "github.com/containerd/containerd/pkg/cri/store/container"
30)
31
32// RemoveContainer removes the container.
33func (c *criService) RemoveContainer(ctx context.Context, r *runtime.RemoveContainerRequest) (_ *runtime.RemoveContainerResponse, retErr error) {
34	container, err := c.containerStore.Get(r.GetContainerId())
35	if err != nil {
36		if err != store.ErrNotExist {
37			return nil, errors.Wrapf(err, "an error occurred when try to find container %q", r.GetContainerId())
38		}
39		// Do not return error if container metadata doesn't exist.
40		log.G(ctx).Tracef("RemoveContainer called for container %q that does not exist", r.GetContainerId())
41		return &runtime.RemoveContainerResponse{}, nil
42	}
43	id := container.ID
44
45	// Forcibly stop the containers if they are in running or unknown state
46	state := container.Status.Get().State()
47	if state == runtime.ContainerState_CONTAINER_RUNNING ||
48		state == runtime.ContainerState_CONTAINER_UNKNOWN {
49		logrus.Infof("Forcibly stopping container %q", id)
50		if err := c.stopContainer(ctx, container, 0); err != nil {
51			return nil, errors.Wrapf(err, "failed to forcibly stop container %q", id)
52		}
53
54	}
55
56	// Set removing state to prevent other start/remove operations against this container
57	// while it's being removed.
58	if err := setContainerRemoving(container); err != nil {
59		return nil, errors.Wrapf(err, "failed to set removing state for container %q", id)
60	}
61	defer func() {
62		if retErr != nil {
63			// Reset removing if remove failed.
64			if err := resetContainerRemoving(container); err != nil {
65				log.G(ctx).WithError(err).Errorf("failed to reset removing state for container %q", id)
66			}
67		}
68	}()
69
70	// NOTE(random-liu): Docker set container to "Dead" state when start removing the
71	// container so as to avoid start/restart the container again. However, for current
72	// kubelet implementation, we'll never start a container once we decide to remove it,
73	// so we don't need the "Dead" state for now.
74
75	// Delete containerd container.
76	if err := container.Container.Delete(ctx, containerd.WithSnapshotCleanup); err != nil {
77		if !errdefs.IsNotFound(err) {
78			return nil, errors.Wrapf(err, "failed to delete containerd container %q", id)
79		}
80		log.G(ctx).Tracef("Remove called for containerd container %q that does not exist", id)
81	}
82
83	// Delete container checkpoint.
84	if err := container.Delete(); err != nil {
85		return nil, errors.Wrapf(err, "failed to delete container checkpoint for %q", id)
86	}
87
88	containerRootDir := c.getContainerRootDir(id)
89	if err := ensureRemoveAll(ctx, containerRootDir); err != nil {
90		return nil, errors.Wrapf(err, "failed to remove container root directory %q",
91			containerRootDir)
92	}
93	volatileContainerRootDir := c.getVolatileContainerRootDir(id)
94	if err := ensureRemoveAll(ctx, volatileContainerRootDir); err != nil {
95		return nil, errors.Wrapf(err, "failed to remove volatile container root directory %q",
96			volatileContainerRootDir)
97	}
98
99	c.containerStore.Delete(id)
100
101	c.containerNameIndex.ReleaseByKey(id)
102
103	return &runtime.RemoveContainerResponse{}, nil
104}
105
106// setContainerRemoving sets the container into removing state. In removing state, the
107// container will not be started or removed again.
108func setContainerRemoving(container containerstore.Container) error {
109	return container.Status.Update(func(status containerstore.Status) (containerstore.Status, error) {
110		// Do not remove container if it's still running or unknown.
111		if status.State() == runtime.ContainerState_CONTAINER_RUNNING {
112			return status, errors.New("container is still running, to stop first")
113		}
114		if status.State() == runtime.ContainerState_CONTAINER_UNKNOWN {
115			return status, errors.New("container state is unknown, to stop first")
116		}
117		if status.Starting {
118			return status, errors.New("container is in starting state, can't be removed")
119		}
120		if status.Removing {
121			return status, errors.New("container is already in removing state")
122		}
123		status.Removing = true
124		return status, nil
125	})
126}
127
128// resetContainerRemoving resets the container removing state on remove failure. So
129// that we could remove the container again.
130func resetContainerRemoving(container containerstore.Container) error {
131	return container.Status.Update(func(status containerstore.Status) (containerstore.Status, error) {
132		status.Removing = false
133		return status, nil
134	})
135}
136