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 · 1 comment
Open

How to cancel remote command execution? #1335

DumbMachine opened this issue Mar 17, 2024 · 1 comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants