2016-11-22 16:49:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-13 01:35:55 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-11-22 16:49:54 +00:00
|
|
|
|
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-11-22 16:49:54 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RemoveContainer removes the container. If the container is running, the container
|
|
|
|
// should be force removed.
|
|
|
|
func (s *Server) RemoveContainer(ctx context.Context, req *pb.RemoveContainerRequest) (*pb.RemoveContainerResponse, error) {
|
|
|
|
logrus.Debugf("RemoveContainerRequest %+v", req)
|
2017-07-31 23:23:20 +00:00
|
|
|
c, err := s.GetContainerFromRequest(req.ContainerId)
|
2016-11-22 16:49:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
cState := s.Runtime().ContainerStatus(c)
|
2016-11-22 16:49:54 +00:00
|
|
|
if cState.Status == oci.ContainerStateCreated || cState.Status == oci.ContainerStateRunning {
|
2017-07-17 12:25:32 +00:00
|
|
|
if err := s.Runtime().StopContainer(c, -1); err != nil {
|
2016-11-22 16:49:54 +00:00
|
|
|
return nil, fmt.Errorf("failed to stop container %s: %v", c.ID(), err)
|
|
|
|
}
|
2017-07-31 18:38:45 +00:00
|
|
|
if err := s.StorageRuntimeServer().StopContainer(c.ID()); err != nil {
|
2017-05-21 13:47:01 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmount container %s: %v", c.ID(), err)
|
|
|
|
}
|
2016-11-22 16:49:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
if err := s.Runtime().DeleteContainer(c); err != nil {
|
2016-11-22 16:49:54 +00:00
|
|
|
return nil, fmt.Errorf("failed to delete container %s: %v", c.ID(), err)
|
|
|
|
}
|
|
|
|
|
2017-08-13 01:35:55 +00:00
|
|
|
if err := os.Remove(filepath.Join(s.config.ContainerExitsDir, c.ID())); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to remove container exit file %s: %v", c.ID(), err)
|
|
|
|
}
|
|
|
|
|
2017-03-03 18:19:19 +00:00
|
|
|
s.removeContainer(c)
|
|
|
|
|
2017-07-31 18:38:45 +00:00
|
|
|
if err := s.StorageRuntimeServer().DeleteContainer(c.ID()); err != nil {
|
2016-10-18 14:48:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to delete storage for container %s: %v", c.ID(), err)
|
2016-11-22 16:49:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 17:10:16 +00:00
|
|
|
s.ReleaseContainerName(c.Name())
|
2016-11-22 16:49:54 +00:00
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
if err := s.CtrIDIndex().Delete(c.ID()); err != nil {
|
2016-11-22 16:49:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &pb.RemoveContainerResponse{}
|
|
|
|
logrus.Debugf("RemoveContainerResponse: %+v", resp)
|
|
|
|
return resp, nil
|
|
|
|
}
|