2016-11-22 16:49:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-09 10:10:35 +00:00
|
|
|
"time"
|
2016-11-22 16:49:54 +00:00
|
|
|
|
2017-06-10 16:18:59 +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"
|
2018-02-12 20:13:07 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
2016-11-22 16:49:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StartContainer starts the container.
|
2017-11-09 10:10:35 +00:00
|
|
|
func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerRequest) (resp *pb.StartContainerResponse, err error) {
|
|
|
|
const operation = "start_container"
|
|
|
|
defer func() {
|
|
|
|
recordOperation(operation, time.Now())
|
|
|
|
recordError(operation, err)
|
|
|
|
}()
|
2016-11-22 16:49:54 +00:00
|
|
|
logrus.Debugf("StartContainerRequest %+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
|
|
|
state := s.Runtime().ContainerStatus(c)
|
2017-06-10 16:18:59 +00:00
|
|
|
if state.Status != oci.ContainerStateCreated {
|
|
|
|
return nil, fmt.Errorf("container %s is not in created state: %s", c.ID(), state.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// if the call to StartContainer fails below we still want to fill
|
|
|
|
// some fields of a container status. In particular, we're going to
|
|
|
|
// adjust container started/finished time and set an error to be
|
|
|
|
// returned in the Reason field for container status call.
|
|
|
|
if err != nil {
|
2017-07-17 12:25:32 +00:00
|
|
|
s.Runtime().SetStartFailed(c, err)
|
2017-06-10 16:18:59 +00:00
|
|
|
}
|
2017-07-20 17:05:12 +00:00
|
|
|
s.ContainerStateToDisk(c)
|
2017-06-10 16:18:59 +00:00
|
|
|
}()
|
2016-11-22 16:49:54 +00:00
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
err = s.Runtime().StartContainer(c)
|
2017-06-10 16:18:59 +00:00
|
|
|
if err != nil {
|
2016-11-22 16:49:54 +00:00
|
|
|
return nil, fmt.Errorf("failed to start container %s: %v", c.ID(), err)
|
|
|
|
}
|
|
|
|
|
2017-11-09 10:10:35 +00:00
|
|
|
resp = &pb.StartContainerResponse{}
|
2016-11-22 16:49:54 +00:00
|
|
|
logrus.Debugf("StartContainerResponse %+v", resp)
|
|
|
|
return resp, nil
|
|
|
|
}
|