diff --git a/internal/services/executor/driver/docker.go b/internal/services/executor/driver/docker.go index 4eb5ded..375ab03 100644 --- a/internal/services/executor/driver/docker.go +++ b/internal/services/executor/driver/docker.go @@ -395,7 +395,7 @@ func (dp *DockerPod) Exec(ctx context.Context, execConfig *ExecConfig) (Containe Env: makeEnvSlice(execConfig.Env), Tty: execConfig.Tty, WorkingDir: execConfig.WorkingDir, - AttachStdin: true, + AttachStdin: execConfig.AttachStdin, AttachStdout: execConfig.Stdout != nil, AttachStderr: execConfig.Stderr != nil, 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 <-e.endCh - resp, err := e.client.ContainerExecInspect(ctx, e.execID) - if err != nil { - return -1, err + var exitCode int + for { + resp, err := e.client.ContainerExecInspect(ctx, e.execID) + if err != nil { + return -1, err + } + if !resp.Running { + exitCode = resp.ExitCode + break + } + time.Sleep(500 * time.Millisecond) } - exitCode := resp.ExitCode e.hresp.Close() diff --git a/internal/services/executor/driver/docker_test.go b/internal/services/executor/driver/docker_test.go index b9a5c0d..20159b0 100644 --- a/internal/services/executor/driver/docker_test.go +++ b/internal/services/executor/driver/docker_test.go @@ -137,7 +137,6 @@ func TestDockerPod(t *testing.T) { t.Fatalf("unexpected err: %v", err) } - ce.Stdin().Close() code, err := ce.Wait(ctx) if err != nil { t.Fatalf("unexpected err: %v", err) @@ -180,7 +179,6 @@ func TestDockerPod(t *testing.T) { t.Fatalf("unexpected err: %v", err) } - ce.Stdin().Close() code, err := ce.Wait(ctx) if err != nil { t.Fatalf("unexpected err: %v", err) diff --git a/internal/services/executor/driver/driver.go b/internal/services/executor/driver/driver.go index 8fde764..353b3ad 100644 --- a/internal/services/executor/driver/driver.go +++ b/internal/services/executor/driver/driver.go @@ -96,13 +96,14 @@ type ContainerConfig struct { } type ExecConfig struct { - Cmd []string - Env map[string]string - WorkingDir string - User string - Stdout io.Writer - Stderr io.Writer - Tty bool + Cmd []string + Env map[string]string + WorkingDir string + User string + AttachStdin bool + Stdout io.Writer + Stderr io.Writer + Tty bool } func toolboxExecPath(toolboxDir string, arch common.Arch) (string, error) { diff --git a/internal/services/executor/driver/k8s.go b/internal/services/executor/driver/k8s.go index 1d434c2..6a55b45 100644 --- a/internal/services/executor/driver/k8s.go +++ b/internal/services/executor/driver/k8s.go @@ -670,7 +670,7 @@ func (p *K8sPod) Exec(ctx context.Context, execConfig *ExecConfig) (ContainerExe VersionedParams(&corev1.PodExecOptions{ Container: mainContainerName, Command: cmd, - Stdin: true, + Stdin: execConfig.AttachStdin, Stdout: execConfig.Stdout != nil, Stderr: execConfig.Stderr != nil, TTY: execConfig.Tty, @@ -683,9 +683,14 @@ func (p *K8sPod) Exec(ctx context.Context, execConfig *ExecConfig) (ContainerExe reader, writer := io.Pipe() + var stdin io.Reader + if execConfig.AttachStdin { + stdin = reader + } + go func() { err := exec.Stream(remotecommand.StreamOptions{ - Stdin: reader, + Stdin: stdin, Stdout: execConfig.Stdout, Stderr: execConfig.Stderr, Tty: execConfig.Tty, diff --git a/internal/services/executor/driver/k8s_test.go b/internal/services/executor/driver/k8s_test.go index 1f91c83..260da68 100644 --- a/internal/services/executor/driver/k8s_test.go +++ b/internal/services/executor/driver/k8s_test.go @@ -81,14 +81,15 @@ func TestK8sPod(t *testing.T) { } defer pod.Remove(ctx) + var buf bytes.Buffer ce, err := pod.Exec(ctx, &ExecConfig{ - Cmd: []string{"ls"}, + Cmd: []string{"ls"}, + Stdout: &buf, }) if err != nil { t.Fatalf("unexpected err: %v", err) } - ce.Stdin().Close() code, err := ce.Wait(ctx) if err != nil { t.Fatalf("unexpected err: %v", err) @@ -131,7 +132,6 @@ func TestK8sPod(t *testing.T) { t.Fatalf("unexpected err: %v", err) } - ce.Stdin().Close() code, err := ce.Wait(ctx) if err != nil { t.Fatalf("unexpected err: %v", err) diff --git a/internal/services/executor/executor.go b/internal/services/executor/executor.go index ac7e794..bc331f2 100644 --- a/internal/services/executor/executor.go +++ b/internal/services/executor/executor.go @@ -70,10 +70,11 @@ func (e *Executor) createFile(ctx context.Context, pod driver.Pod, command, user var buf bytes.Buffer execConfig := &driver.ExecConfig{ - Cmd: cmd, - Stdout: &buf, - Stderr: outf, - User: user, + Cmd: cmd, + AttachStdin: true, + Stdout: &buf, + Stderr: outf, + User: user, } ce, err := pod.Exec(ctx, execConfig) @@ -161,13 +162,14 @@ func (e *Executor) doRunStep(ctx context.Context, s *types.RunStep, t *types.Exe } execConfig := &driver.ExecConfig{ - Cmd: cmd, - Env: environment, - WorkingDir: workingDir, - User: user, - Stdout: outf, - Stderr: outf, - Tty: true, + Cmd: cmd, + Env: environment, + WorkingDir: workingDir, + User: user, + AttachStdin: true, + Stdout: outf, + Stderr: outf, + Tty: true, } ce, err := pod.Exec(ctx, execConfig) @@ -211,11 +213,12 @@ func (e *Executor) doSaveToWorkspaceStep(ctx context.Context, s *types.SaveToWor } execConfig := &driver.ExecConfig{ - Cmd: cmd, - Env: t.Environment, - WorkingDir: workingDir, - Stdout: archivef, - Stderr: logf, + Cmd: cmd, + Env: t.Environment, + WorkingDir: workingDir, + AttachStdin: true, + Stdout: archivef, + Stderr: logf, } ce, err := pod.Exec(ctx, execConfig) @@ -271,10 +274,11 @@ func (e *Executor) expandDir(ctx context.Context, t *types.ExecutorTask, pod dri stdout := &bytes.Buffer{} execConfig := &driver.ExecConfig{ - Cmd: cmd, - Env: t.Environment, - Stdout: stdout, - Stderr: logf, + Cmd: cmd, + Env: t.Environment, + AttachStdin: true, + Stdout: stdout, + Stderr: logf, } ce, err := pod.Exec(ctx, execConfig) @@ -298,10 +302,11 @@ func (e *Executor) mkdir(ctx context.Context, t *types.ExecutorTask, pod driver. cmd := append([]string{toolboxContainerPath, "mkdir"}, args...) execConfig := &driver.ExecConfig{ - Cmd: cmd, - Env: t.Environment, - Stdout: logf, - Stderr: logf, + Cmd: cmd, + Env: t.Environment, + AttachStdin: true, + Stdout: logf, + Stderr: logf, } ce, err := pod.Exec(ctx, execConfig) @@ -333,11 +338,12 @@ func (e *Executor) template(ctx context.Context, t *types.ExecutorTask, pod driv } execConfig := &driver.ExecConfig{ - Cmd: cmd, - Env: t.Environment, - WorkingDir: workingDir, - Stdout: stdout, - Stderr: logf, + Cmd: cmd, + Env: t.Environment, + WorkingDir: workingDir, + AttachStdin: true, + Stdout: stdout, + Stderr: logf, } ce, err := pod.Exec(ctx, execConfig) @@ -379,11 +385,12 @@ func (e *Executor) unarchive(ctx context.Context, t *types.ExecutorTask, source } execConfig := &driver.ExecConfig{ - Cmd: cmd, - Env: t.Environment, - WorkingDir: workingDir, - Stdout: logf, - Stderr: logf, + Cmd: cmd, + Env: t.Environment, + WorkingDir: workingDir, + AttachStdin: true, + Stdout: logf, + Stderr: logf, } ce, err := pod.Exec(ctx, execConfig) @@ -496,11 +503,12 @@ func (e *Executor) doSaveCacheStep(ctx context.Context, s *types.SaveCacheStep, } execConfig := &driver.ExecConfig{ - Cmd: cmd, - Env: t.Environment, - WorkingDir: workingDir, - Stdout: archivef, - Stderr: logf, + Cmd: cmd, + Env: t.Environment, + WorkingDir: workingDir, + AttachStdin: true, + Stdout: archivef, + Stderr: logf, } ce, err := pod.Exec(ctx, execConfig)