exec: Add endpoint for streaming server
Signed-off-by: Jacek J. Łakis <jacek.lakis@intel.com>
This commit is contained in:
parent
203a52487c
commit
b4e9023102
2 changed files with 50 additions and 20 deletions
|
@ -2,37 +2,52 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/kubernetes-incubator/cri-o/oci"
|
"github.com/kubernetes-incubator/cri-o/oci"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
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.
|
// 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) {
|
func (s *Server) Exec(ctx context.Context, req *pb.ExecRequest) (*pb.ExecResponse, error) {
|
||||||
logrus.Debugf("ExecRequest %+v", req)
|
logrus.Debugf("ExecRequest %+v", req)
|
||||||
c, err := s.getContainerFromRequest(req.ContainerId)
|
|
||||||
|
resp, err := s.GetExec(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("unable to prepare exec endpoint")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.runtime.UpdateStatus(c); err != nil {
|
return resp, nil
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cState := s.runtime.ContainerStatus(c)
|
// 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) {
|
if !(cState.Status == oci.ContainerStateRunning || cState.Status == oci.ContainerStateCreated) {
|
||||||
return nil, fmt.Errorf("container is not created or running")
|
return fmt.Errorf("container is not created or running")
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.Cmd == nil {
|
args := []string{"exec", c.Name()} // exec ctr
|
||||||
return nil, fmt.Errorf("exec command cannot be empty")
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
url := "url"
|
return nil
|
||||||
|
|
||||||
return &pb.ExecResponse{
|
|
||||||
Url: url,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,6 +62,21 @@ type Server struct {
|
||||||
stream streamService
|
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 {
|
func (s *Server) loadContainer(id string) error {
|
||||||
config, err := s.store.GetFromContainerDirectory(id, "config.json")
|
config, err := s.store.GetFromContainerDirectory(id, "config.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue