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
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// ExecSync runs a command in a container synchronously.
|
2017-11-09 10:10:35 +00:00
|
|
|
func (s *Server) ExecSync(ctx context.Context, req *pb.ExecSyncRequest) (resp *pb.ExecSyncResponse, err error) {
|
|
|
|
const operation = "exec_sync"
|
|
|
|
defer func() {
|
|
|
|
recordOperation(operation, time.Now())
|
|
|
|
recordError(operation, err)
|
|
|
|
}()
|
2016-11-22 16:49:54 +00:00
|
|
|
logrus.Debugf("ExecSyncRequest %+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
|
|
|
if err = s.Runtime().UpdateStatus(c); err != nil {
|
2016-11-22 16:49:54 +00:00
|
|
|
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.ContainerStateRunning || cState.Status == oci.ContainerStateCreated) {
|
|
|
|
return nil, fmt.Errorf("container is not created or running")
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:41:28 +00:00
|
|
|
cmd := req.Cmd
|
2016-11-22 16:49:54 +00:00
|
|
|
if cmd == nil {
|
|
|
|
return nil, fmt.Errorf("exec command cannot be empty")
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
execResp, err := s.Runtime().ExecSync(c, cmd, req.Timeout)
|
2016-11-24 09:57:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-09 10:10:35 +00:00
|
|
|
resp = &pb.ExecSyncResponse{
|
2016-11-22 16:49:54 +00:00
|
|
|
Stdout: execResp.Stdout,
|
|
|
|
Stderr: execResp.Stderr,
|
2017-02-03 14:41:28 +00:00
|
|
|
ExitCode: execResp.ExitCode,
|
2016-11-22 16:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("ExecSyncResponse: %+v", resp)
|
2016-11-24 09:57:10 +00:00
|
|
|
return resp, nil
|
2016-11-22 16:49:54 +00:00
|
|
|
}
|