2016-11-22 16:49:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-12-06 11:39:38 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2016-11-22 16:49:54 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StopContainer stops a running container with a grace period (i.e., timeout).
|
|
|
|
func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest) (*pb.StopContainerResponse, error) {
|
|
|
|
logrus.Debugf("StopContainerRequest %+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-12-06 11:39:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-17 12:25:32 +00:00
|
|
|
cStatus := s.Runtime().ContainerStatus(c)
|
2016-12-06 11:39:38 +00:00
|
|
|
if cStatus.Status != oci.ContainerStateStopped {
|
2017-07-17 12:25:32 +00:00
|
|
|
if err := s.Runtime().StopContainer(c, req.Timeout); err != nil {
|
2016-12-06 11:39:38 +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-20 17:05:12 +00:00
|
|
|
s.ContainerStateToDisk(c)
|
2017-05-11 10:03:59 +00:00
|
|
|
|
2016-11-22 16:49:54 +00:00
|
|
|
resp := &pb.StopContainerResponse{}
|
|
|
|
logrus.Debugf("StopContainerResponse: %+v", resp)
|
|
|
|
return resp, nil
|
|
|
|
}
|