Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to cancel remote command execution? #1335

Open
DumbMachine opened this issue Mar 17, 2024 · 3 comments
Open

How to cancel remote command execution? #1335

DumbMachine opened this issue Mar 17, 2024 · 3 comments
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@DumbMachine
Copy link

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.

@DanMHammer
Copy link

DanMHammer commented Apr 4, 2024

@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,
	})

@k8s-triage-robot
Copy link

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

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 3, 2024
@k8s-triage-robot
Copy link

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

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

No branches or pull requests

4 participants