exec: Add endpoint for streaming server

Signed-off-by: Jacek J. Łakis <jacek.lakis@intel.com>
This commit is contained in:
Jacek J. Łakis 2017-02-08 15:11:52 +01:00 committed by Samuel Ortiz
parent 203a52487c
commit b4e9023102
2 changed files with 50 additions and 20 deletions

View file

@ -2,37 +2,52 @@ package server
import (
"fmt"
"io"
"os/exec"
"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"
"k8s.io/kubernetes/pkg/util/term"
)
// Exec prepares a streaming endpoint to execute a command in the container.
func (s *Server) Exec(ctx context.Context, req *pb.ExecRequest) (*pb.ExecResponse, error) {
logrus.Debugf("ExecRequest %+v", req)
c, err := s.getContainerFromRequest(req.ContainerId)
resp, err := s.GetExec(req)
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to prepare exec endpoint")
}
if err = s.runtime.UpdateStatus(c); err != nil {
return nil, err
}
cState := s.runtime.ContainerStatus(c)
if !(cState.Status == oci.ContainerStateRunning || cState.Status == oci.ContainerStateCreated) {
return nil, fmt.Errorf("container is not created or running")
}
if req.Cmd == nil {
return nil, fmt.Errorf("exec command cannot be empty")
}
url := "url"
return &pb.ExecResponse{
Url: url,
}, nil
return resp, nil
}
// Exec endpoint for streaming.Runtime
func (ss streamService) Exec(containerID string, cmd []string, in io.Reader, out, errOut io.WriteCloser, tty bool, resize <-chan term.Size) error {
fmt.Println(containerID, cmd, in, out, errOut, tty, resize)
c := ss.runtimeServer.state.containers.Get(containerID)
if err := ss.runtimeServer.runtime.UpdateStatus(c); err != nil {
return err
}
cState := ss.runtimeServer.runtime.ContainerStatus(c)
if !(cState.Status == oci.ContainerStateRunning || cState.Status == oci.ContainerStateCreated) {
return fmt.Errorf("container is not created or running")
}
args := []string{"exec", c.Name()} // exec ctr
args = append(args, cmd...) // exec ctr cmd
execCmd := exec.Command(ss.runtimeServer.runtime.Path(c), args...) // runc exec ctr cmd
execCmd.Stdin = in
execCmd.Stdout = out
execCmd.Stderr = errOut
if err := execCmd.Run(); err != nil {
return err
}
return nil
}

View file

@ -62,6 +62,21 @@ type Server struct {
stream streamService
}
// GetExec returns exec stream request
func (s *Server) GetExec(req *pb.ExecRequest) (*pb.ExecResponse, error) {
return s.stream.streamServer.GetExec(req)
}
// GetAttach returns attach stream request
func (s *Server) GetAttach(req *pb.AttachRequest) (*pb.AttachResponse, error) {
return s.stream.streamServer.GetAttach(req)
}
// GetPortForward returns port forward stream request
func (s *Server) GetPortForward(req *pb.PortForwardRequest) (*pb.PortForwardResponse, error) {
return s.stream.streamServer.GetPortForward(req)
}
func (s *Server) loadContainer(id string) error {
config, err := s.store.GetFromContainerDirectory(id, "config.json")
if err != nil {