Changes to attach to enable per-stream attaching

This allows us to attach to attach to just stdout or stderr or
stdin, or any combination of these.

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>

Closes: #608
Approved by: baude
This commit is contained in:
Matthew Heon
2018-04-11 13:09:41 -04:00
committed by Atomic Bot
parent b8394600d8
commit 5e03cec7ec
6 changed files with 158 additions and 68 deletions

View File

@ -5,16 +5,13 @@ import (
"io"
"net"
"os"
gosignal "os/signal"
"path/filepath"
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/term"
"github.com/pkg/errors"
"github.com/projectatomic/libpod/pkg/kubeutils"
"github.com/projectatomic/libpod/utils"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/unix"
"k8s.io/client-go/tools/remotecommand"
)
@ -26,33 +23,19 @@ const (
AttachPipeStderr = 3
)
// resizeTty handles TTY resizing for Attach()
func resizeTty(resize chan remotecommand.TerminalSize) {
sigchan := make(chan os.Signal, 1)
gosignal.Notify(sigchan, signal.SIGWINCH)
sendUpdate := func() {
winsize, err := term.GetWinsize(os.Stdin.Fd())
if err != nil {
logrus.Warnf("Could not get terminal size %v", err)
return
}
resize <- remotecommand.TerminalSize{
Width: winsize.Width,
Height: winsize.Height,
}
// Attach to the given container
// Does not check if state is appropriate
func (c *Container) attach(outputStream, errorStream io.WriteCloser, inputStream io.Reader, keys string, resize <-chan remotecommand.TerminalSize) error {
if outputStream == nil && errorStream == nil && inputStream == nil {
return errors.Wrapf(ErrInvalidArg, "must provide at least one stream to attach to")
}
// Do not allow stdin on containers without Stdin set
// Conmon was not configured properly
if inputStream != nil && !c.config.Stdin {
return errors.Wrapf(ErrInvalidArg, "this container was not created as interactive, cannot attach stdin")
}
go func() {
defer close(resize)
// Update the terminal size immediately without waiting
// for a SIGWINCH to get the correct initial size.
sendUpdate()
for range sigchan {
sendUpdate()
}
}()
}
func (c *Container) attach(noStdin bool, keys string) error {
// Check the validity of the provided keys first
var err error
detachKeys := []byte{}
@ -63,40 +46,14 @@ func (c *Container) attach(noStdin bool, keys string) error {
}
}
// TODO: allow resize channel to be passed in for CRI-O use
resize := make(chan remotecommand.TerminalSize)
if terminal.IsTerminal(int(os.Stdin.Fd())) {
resizeTty(resize)
} else {
defer close(resize)
}
logrus.Debugf("Attaching to container %s", c.ID())
return c.attachContainerSocket(resize, noStdin, detachKeys)
return c.attachContainerSocket(resize, detachKeys, outputStream, errorStream, inputStream)
}
// attachContainerSocket connects to the container's attach socket and deals with the IO
// TODO add a channel to allow interruptiong
func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSize, noStdIn bool, detachKeys []byte) error {
inputStream := os.Stdin
outputStream := os.Stdout
errorStream := os.Stderr
defer inputStream.Close()
if terminal.IsTerminal(int(inputStream.Fd())) {
oldTermState, err := term.SaveState(inputStream.Fd())
if err != nil {
return errors.Wrapf(err, "unable to save terminal state")
}
defer term.RestoreTerminal(inputStream.Fd(), oldTermState)
}
// Put both input and output into raw when we have a terminal
if !noStdIn && c.config.Spec.Process.Terminal {
term.SetRawTerminal(inputStream.Fd())
}
func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSize, detachKeys []byte, outputStream, errorStream io.WriteCloser, inputStream io.Reader) error {
kubeutils.HandleResizing(resize, func(size remotecommand.TerminalSize) {
controlPath := filepath.Join(c.bundlePath(), "ctl")
controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0)
@ -129,7 +86,7 @@ func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSi
stdinDone := make(chan error)
go func() {
var err error
if inputStream != nil && !noStdIn {
if inputStream != nil {
_, err = utils.CopyDetachable(conn, inputStream, detachKeys)
conn.CloseWrite()
}