2016-11-22 16:49:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-05-30 22:15:30 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-06-07 02:43:44 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/utils"
|
2016-11-22 16:49:54 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-06-28 15:47:31 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2016-11-22 16:49:54 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2017-05-30 22:15:30 +00:00
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
|
|
|
"k8s.io/kubernetes/pkg/util/term"
|
2016-11-22 16:49:54 +00:00
|
|
|
)
|
|
|
|
|
2017-06-08 20:08:29 +00:00
|
|
|
/* Sync with stdpipe_t in conmon.c */
|
|
|
|
const (
|
|
|
|
AttachPipeStdin = 1
|
|
|
|
AttachPipeStdout = 2
|
|
|
|
AttachPipeStderr = 3
|
|
|
|
)
|
|
|
|
|
2016-11-22 16:49:54 +00:00
|
|
|
// Attach prepares a streaming endpoint to attach to a running container.
|
|
|
|
func (s *Server) Attach(ctx context.Context, req *pb.AttachRequest) (*pb.AttachResponse, error) {
|
2017-05-30 22:15:30 +00:00
|
|
|
logrus.Debugf("AttachRequest %+v", req)
|
|
|
|
|
|
|
|
resp, err := s.GetAttach(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to prepare attach endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach endpoint for streaming.Runtime
|
|
|
|
func (ss streamService) Attach(containerID string, inputStream io.Reader, outputStream, errorStream io.WriteCloser, tty bool, resize <-chan term.Size) error {
|
|
|
|
c := ss.runtimeServer.GetContainer(containerID)
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
return fmt.Errorf("could not find container %q", containerID)
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
if err := ss.runtimeServer.Runtime().UpdateStatus(c); err != nil {
|
2017-05-30 22:15:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
cState := ss.runtimeServer.Runtime().ContainerStatus(c)
|
2017-05-30 22:15:30 +00:00
|
|
|
if !(cState.Status == oci.ContainerStateRunning || cState.Status == oci.ContainerStateCreated) {
|
|
|
|
return fmt.Errorf("container is not created or running")
|
|
|
|
}
|
|
|
|
|
|
|
|
controlPath := filepath.Join(c.BundlePath(), "ctl")
|
2017-06-28 15:47:31 +00:00
|
|
|
controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0)
|
2017-05-30 22:15:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open container ctl file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kubecontainer.HandleResizing(resize, func(size term.Size) {
|
|
|
|
logrus.Infof("Got a resize event: %+v", size)
|
|
|
|
_, err := fmt.Fprintf(controlFile, "%d %d %d\n", 1, size.Height, size.Width)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Infof("Failed to write to control file to resize terminal: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
attachSocketPath := filepath.Join("/var/run/crio", c.ID(), "attach")
|
2017-06-08 20:08:29 +00:00
|
|
|
conn, err := net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: attachSocketPath, Net: "unixpacket"})
|
2017-05-30 22:15:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect to container %s attach socket: %v", c.ID(), err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
receiveStdout := make(chan error)
|
|
|
|
if outputStream != nil || errorStream != nil {
|
|
|
|
go func() {
|
2017-06-08 20:08:29 +00:00
|
|
|
receiveStdout <- redirectResponseToOutputStreams(outputStream, errorStream, conn)
|
2017-05-30 22:15:30 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-06-07 02:43:44 +00:00
|
|
|
stdinDone := make(chan error)
|
2017-05-30 22:15:30 +00:00
|
|
|
go func() {
|
2017-06-07 02:43:44 +00:00
|
|
|
var err error
|
2017-05-30 22:15:30 +00:00
|
|
|
if inputStream != nil {
|
2017-06-07 02:43:44 +00:00
|
|
|
_, err = utils.CopyDetachable(conn, inputStream, nil)
|
2017-06-08 20:08:29 +00:00
|
|
|
conn.CloseWrite()
|
2017-05-30 22:15:30 +00:00
|
|
|
}
|
2017-06-07 02:43:44 +00:00
|
|
|
stdinDone <- err
|
2017-05-30 22:15:30 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-receiveStdout:
|
|
|
|
return err
|
2017-06-07 02:43:44 +00:00
|
|
|
case err := <-stdinDone:
|
|
|
|
if _, ok := err.(utils.DetachError); ok {
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-30 22:15:30 +00:00
|
|
|
if outputStream != nil || errorStream != nil {
|
|
|
|
return <-receiveStdout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:08:29 +00:00
|
|
|
func redirectResponseToOutputStreams(outputStream, errorStream io.Writer, conn io.Reader) error {
|
2017-05-30 22:15:30 +00:00
|
|
|
var err error
|
2017-06-08 20:08:29 +00:00
|
|
|
buf := make([]byte, 8192+1) /* Sync with conmon STDIO_BUF_SIZE */
|
|
|
|
|
|
|
|
for {
|
|
|
|
nr, er := conn.Read(buf)
|
|
|
|
if nr > 0 {
|
|
|
|
var dst io.Writer
|
|
|
|
if buf[0] == AttachPipeStdout {
|
|
|
|
dst = outputStream
|
|
|
|
} else if buf[0] == AttachPipeStderr {
|
|
|
|
dst = errorStream
|
|
|
|
} else {
|
|
|
|
logrus.Infof("Got unexpected attach type %+d", buf[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if dst != nil {
|
|
|
|
nw, ew := dst.Write(buf[1:nr])
|
|
|
|
if ew != nil {
|
|
|
|
err = ew
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nr != nw+1 {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if er == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
err = er
|
|
|
|
break
|
|
|
|
}
|
2017-05-30 22:15:30 +00:00
|
|
|
}
|
2017-06-08 20:08:29 +00:00
|
|
|
|
2017-05-30 22:15:30 +00:00
|
|
|
return err
|
2016-11-22 16:49:54 +00:00
|
|
|
}
|