Skip to content

Commit 6c3332e

Browse files
authored
Merge pull request kubernetes#130747 from soltysh/exec_context
kubectl: expose context parameter in DefaultRemoteExecutor
2 parents 69467d3 + 78e58b8 commit 6c3332e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/exec/exec.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,26 @@ func NewCmdExec(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Co
114114

115115
// RemoteExecutor defines the interface accepted by the Exec command - provided for test stubbing
116116
type RemoteExecutor interface {
117+
// Execute supports executing remote command in a pod.
117118
Execute(url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error
119+
120+
// ExecuteWithContext, in contrast to Execute, supports stopping the remote command via context cancellation.
121+
ExecuteWithContext(ctx context.Context, url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error
118122
}
119123

120124
// DefaultRemoteExecutor is the standard implementation of remote command execution
121125
type DefaultRemoteExecutor struct{}
122126

123-
func (*DefaultRemoteExecutor) Execute(url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error {
127+
func (d *DefaultRemoteExecutor) Execute(url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error {
128+
return d.ExecuteWithContext(context.Background(), url, config, stdin, stdout, stderr, tty, terminalSizeQueue)
129+
}
130+
131+
func (*DefaultRemoteExecutor) ExecuteWithContext(ctx context.Context, url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error {
124132
exec, err := createExecutor(url, config)
125133
if err != nil {
126134
return err
127135
}
128-
return exec.StreamWithContext(context.Background(), remotecommand.StreamOptions{
136+
return exec.StreamWithContext(ctx, remotecommand.StreamOptions{
129137
Stdin: stdin,
130138
Stdout: stdout,
131139
Stderr: stderr,

staging/src/k8s.io/kubectl/pkg/cmd/exec/exec_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package exec
1818

1919
import (
2020
"bytes"
21+
"context"
2122
"fmt"
2223
"io"
2324
"net/http"
@@ -45,6 +46,10 @@ type fakeRemoteExecutor struct {
4546
}
4647

4748
func (f *fakeRemoteExecutor) Execute(url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error {
49+
return f.ExecuteWithContext(context.Background(), url, config, stdin, stdout, stderr, tty, terminalSizeQueue)
50+
}
51+
52+
func (f *fakeRemoteExecutor) ExecuteWithContext(ctx context.Context, url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error {
4853
f.url = url
4954
return f.execErr
5055
}

0 commit comments

Comments
 (0)