-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Comments
@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 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:
You can:
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:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. This bot triages issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /close not-planned |
I'm executing some interactive commands on a pod with the following code:
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.
The text was updated successfully, but these errors were encountered: