2016-11-22 16:49:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
|
|
|
"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-02-03 14:41:28 +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
|
|
|
if err := s.Runtime().UpdateStatus(c); err != nil {
|
2016-11-22 16:49:54 +00:00
|
|
|
return nil, fmt.Errorf("failed to update container state: %v", 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-05-21 13:47:01 +00:00
|
|
|
if err := s.storageRuntimeServer.StopContainer(c.ID()); err != nil {
|
|
|
|
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-03-03 18:19:19 +00:00
|
|
|
s.removeContainer(c)
|
|
|
|
|
2017-04-19 19:17:10 +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
|
|
|
}
|
|
|
|
|
|
|
|
s.releaseContainerName(c.Name())
|
|
|
|
|
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
|
|
|
|
}
|