kubernetes/client-go

How to cancel remote command execution?

DumbMachine opened this issue · 6 comments

I'm executing some interactive commands on a pod with the following code:

ctx, cancel := context.WithCancel(context.TODO())
exec, err := remotecommand.NewSPDYExecutor(config, method, url)
	if err != nil {
		return err
	}

return exec.StreamWithContext(ctx, remotecommand.StreamOptions{
	Stdin:             stdin,
	Stdout:            stdout,
	Stderr:            stderr,
	Tty:               tty,
	TerminalSizeQueue: terminalSizeQueue,
})

Have a background goroutine that cancels the context if I'm idle. Cancellation works fine, the network connection is closed, but the process on the pod is still running. I want to kill the process on the pod when the context is cancelled. I couldn't find a way for it, is it possible out of the box in k8s client?

If not, what would be the best way to achieve this? I'm thinking making signature of the process and killing it with another command when the context is cancelled. But I'm not sure if it's the best way to do it.

@DumbMachine I recently encountered the same issue. I hope the maintainers of client-go can put in a more elegant fix, but I did find a workaround. Here's a quick general example. This example also assumes you're passing cancel() down to the goroutine you mentioned and calling it after the idle timeout.

	ctx, cancel := context.WithCancel(context.TODO())

	stdinPipeReader, stdinPipeWriter := io.Pipe()

	go func() {
		// copy the original stdin to the pipe
                // this will also continue to copy new input in an interactive shell session
		io.Copy(stdinPipeWriter, stdin)
	}()

	go func() {
		// on context cancel, send ctrl-c to the process by writing to the pipe
		<-ctx.Done()
		stdinPipeWriter.Write([]byte("\x03"))
		// if you're using an interactive shell, you may need to send ctrl-c and exit
		// stdinPipeWriter.Write([]byte("\x03\nexit\n"))
	}()

	exec, err := remotecommand.NewSPDYExecutor(config, method, url)
	if err != nil {
		return err
	}

	exec.StreamWithContext(ctx, remotecommand.StreamOptions{
		// pass the pipe reader instead of the original stdin
		Stdin:             stdinPipeReader,
		Stdout:            stdout,
		Stderr:            stderr,
		Tty:               tty,
		TerminalSizeQueue: terminalSizeQueue,
	})

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten