runservice drivers: define ExecConfig AttachStdin

This commit is contained in:
Simone Gotti 2019-05-10 16:50:31 +02:00
parent f8b0061844
commit 7adca3ea30
6 changed files with 77 additions and 58 deletions

View File

@ -395,7 +395,7 @@ func (dp *DockerPod) Exec(ctx context.Context, execConfig *ExecConfig) (Containe
Env: makeEnvSlice(execConfig.Env), Env: makeEnvSlice(execConfig.Env),
Tty: execConfig.Tty, Tty: execConfig.Tty,
WorkingDir: execConfig.WorkingDir, WorkingDir: execConfig.WorkingDir,
AttachStdin: true, AttachStdin: execConfig.AttachStdin,
AttachStdout: execConfig.Stdout != nil, AttachStdout: execConfig.Stdout != nil,
AttachStderr: execConfig.Stderr != nil, AttachStderr: execConfig.Stderr != nil,
User: execConfig.User, User: execConfig.User,
@ -451,11 +451,18 @@ func (e *DockerContainerExec) Wait(ctx context.Context) (int, error) {
// ignore error, we'll use the exit code of the exec // ignore error, we'll use the exit code of the exec
<-e.endCh <-e.endCh
var exitCode int
for {
resp, err := e.client.ContainerExecInspect(ctx, e.execID) resp, err := e.client.ContainerExecInspect(ctx, e.execID)
if err != nil { if err != nil {
return -1, err return -1, err
} }
exitCode := resp.ExitCode if !resp.Running {
exitCode = resp.ExitCode
break
}
time.Sleep(500 * time.Millisecond)
}
e.hresp.Close() e.hresp.Close()

View File

@ -137,7 +137,6 @@ func TestDockerPod(t *testing.T) {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
ce.Stdin().Close()
code, err := ce.Wait(ctx) code, err := ce.Wait(ctx)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
@ -180,7 +179,6 @@ func TestDockerPod(t *testing.T) {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
ce.Stdin().Close()
code, err := ce.Wait(ctx) code, err := ce.Wait(ctx)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)

View File

@ -100,6 +100,7 @@ type ExecConfig struct {
Env map[string]string Env map[string]string
WorkingDir string WorkingDir string
User string User string
AttachStdin bool
Stdout io.Writer Stdout io.Writer
Stderr io.Writer Stderr io.Writer
Tty bool Tty bool

View File

@ -670,7 +670,7 @@ func (p *K8sPod) Exec(ctx context.Context, execConfig *ExecConfig) (ContainerExe
VersionedParams(&corev1.PodExecOptions{ VersionedParams(&corev1.PodExecOptions{
Container: mainContainerName, Container: mainContainerName,
Command: cmd, Command: cmd,
Stdin: true, Stdin: execConfig.AttachStdin,
Stdout: execConfig.Stdout != nil, Stdout: execConfig.Stdout != nil,
Stderr: execConfig.Stderr != nil, Stderr: execConfig.Stderr != nil,
TTY: execConfig.Tty, TTY: execConfig.Tty,
@ -683,9 +683,14 @@ func (p *K8sPod) Exec(ctx context.Context, execConfig *ExecConfig) (ContainerExe
reader, writer := io.Pipe() reader, writer := io.Pipe()
var stdin io.Reader
if execConfig.AttachStdin {
stdin = reader
}
go func() { go func() {
err := exec.Stream(remotecommand.StreamOptions{ err := exec.Stream(remotecommand.StreamOptions{
Stdin: reader, Stdin: stdin,
Stdout: execConfig.Stdout, Stdout: execConfig.Stdout,
Stderr: execConfig.Stderr, Stderr: execConfig.Stderr,
Tty: execConfig.Tty, Tty: execConfig.Tty,

View File

@ -81,14 +81,15 @@ func TestK8sPod(t *testing.T) {
} }
defer pod.Remove(ctx) defer pod.Remove(ctx)
var buf bytes.Buffer
ce, err := pod.Exec(ctx, &ExecConfig{ ce, err := pod.Exec(ctx, &ExecConfig{
Cmd: []string{"ls"}, Cmd: []string{"ls"},
Stdout: &buf,
}) })
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
ce.Stdin().Close()
code, err := ce.Wait(ctx) code, err := ce.Wait(ctx)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
@ -131,7 +132,6 @@ func TestK8sPod(t *testing.T) {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
ce.Stdin().Close()
code, err := ce.Wait(ctx) code, err := ce.Wait(ctx)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)

View File

@ -71,6 +71,7 @@ func (e *Executor) createFile(ctx context.Context, pod driver.Pod, command, user
var buf bytes.Buffer var buf bytes.Buffer
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
AttachStdin: true,
Stdout: &buf, Stdout: &buf,
Stderr: outf, Stderr: outf,
User: user, User: user,
@ -165,6 +166,7 @@ func (e *Executor) doRunStep(ctx context.Context, s *types.RunStep, t *types.Exe
Env: environment, Env: environment,
WorkingDir: workingDir, WorkingDir: workingDir,
User: user, User: user,
AttachStdin: true,
Stdout: outf, Stdout: outf,
Stderr: outf, Stderr: outf,
Tty: true, Tty: true,
@ -214,6 +216,7 @@ func (e *Executor) doSaveToWorkspaceStep(ctx context.Context, s *types.SaveToWor
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
WorkingDir: workingDir, WorkingDir: workingDir,
AttachStdin: true,
Stdout: archivef, Stdout: archivef,
Stderr: logf, Stderr: logf,
} }
@ -273,6 +276,7 @@ func (e *Executor) expandDir(ctx context.Context, t *types.ExecutorTask, pod dri
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
AttachStdin: true,
Stdout: stdout, Stdout: stdout,
Stderr: logf, Stderr: logf,
} }
@ -300,6 +304,7 @@ func (e *Executor) mkdir(ctx context.Context, t *types.ExecutorTask, pod driver.
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
AttachStdin: true,
Stdout: logf, Stdout: logf,
Stderr: logf, Stderr: logf,
} }
@ -336,6 +341,7 @@ func (e *Executor) template(ctx context.Context, t *types.ExecutorTask, pod driv
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
WorkingDir: workingDir, WorkingDir: workingDir,
AttachStdin: true,
Stdout: stdout, Stdout: stdout,
Stderr: logf, Stderr: logf,
} }
@ -382,6 +388,7 @@ func (e *Executor) unarchive(ctx context.Context, t *types.ExecutorTask, source
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
WorkingDir: workingDir, WorkingDir: workingDir,
AttachStdin: true,
Stdout: logf, Stdout: logf,
Stderr: logf, Stderr: logf,
} }
@ -499,6 +506,7 @@ func (e *Executor) doSaveCacheStep(ctx context.Context, s *types.SaveCacheStep,
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
WorkingDir: workingDir, WorkingDir: workingDir,
AttachStdin: true,
Stdout: archivef, Stdout: archivef,
Stderr: logf, Stderr: logf,
} }