Skip to content

Commit 06bb4c9

Browse files
committed
Use executor.StreamWithContext instead of deprecated executor.Stream
1 parent 092eddc commit 06bb4c9

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

cmd/crictl/attach.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ var runtimeAttachCommand = &cli.Command{
5959
return err
6060
}
6161

62+
ctx, cancel := context.WithCancel(c.Context)
63+
defer cancel()
64+
6265
var opts = attachOptions{
6366
id: id,
6467
tty: c.Bool("tty"),
6568
stdin: c.Bool("stdin"),
6669
}
67-
if err = Attach(runtimeClient, opts); err != nil {
70+
if err = Attach(ctx, runtimeClient, opts); err != nil {
6871
return fmt.Errorf("attaching running container failed: %w", err)
6972

7073
}
@@ -73,7 +76,7 @@ var runtimeAttachCommand = &cli.Command{
7376
}
7477

7578
// Attach sends an AttachRequest to server, and parses the returned AttachResponse
76-
func Attach(client internalapi.RuntimeService, opts attachOptions) error {
79+
func Attach(ctx context.Context, client internalapi.RuntimeService, opts attachOptions) error {
7780
if opts.id == "" {
7881
return fmt.Errorf("ID cannot be empty")
7982

@@ -86,7 +89,7 @@ func Attach(client internalapi.RuntimeService, opts attachOptions) error {
8689
Stderr: !opts.tty,
8790
}
8891
logrus.Debugf("AttachRequest: %v", request)
89-
r, err := client.Attach(context.TODO(), request)
92+
r, err := client.Attach(ctx, request)
9093
logrus.Debugf("AttachResponse: %v", r)
9194
if err != nil {
9295
return err
@@ -106,5 +109,5 @@ func Attach(client internalapi.RuntimeService, opts attachOptions) error {
106109
}
107110

108111
logrus.Debugf("Attach URL: %v", URL)
109-
return stream(opts.stdin, opts.tty, URL)
112+
return stream(ctx, opts.stdin, opts.tty, URL)
110113
}

cmd/crictl/exec.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ var runtimeExecCommand = &cli.Command{
9595
}
9696
return nil
9797
}
98-
err = Exec(runtimeClient, opts)
98+
99+
ctx, cancel := context.WithCancel(c.Context)
100+
defer cancel()
101+
102+
err = Exec(ctx, runtimeClient, opts)
99103
if err != nil {
100104
return fmt.Errorf("execing command in container: %w", err)
101105
}
@@ -124,7 +128,7 @@ func ExecSync(client internalapi.RuntimeService, opts execOptions) (int, error)
124128
}
125129

126130
// Exec sends an ExecRequest to server, and parses the returned ExecResponse
127-
func Exec(client internalapi.RuntimeService, opts execOptions) error {
131+
func Exec(ctx context.Context, client internalapi.RuntimeService, opts execOptions) error {
128132
request := &pb.ExecRequest{
129133
ContainerId: opts.id,
130134
Cmd: opts.cmd,
@@ -135,7 +139,7 @@ func Exec(client internalapi.RuntimeService, opts execOptions) error {
135139
}
136140

137141
logrus.Debugf("ExecRequest: %v", request)
138-
r, err := client.Exec(context.TODO(), request)
142+
r, err := client.Exec(ctx, request)
139143
logrus.Debugf("ExecResponse: %v", r)
140144
if err != nil {
141145
return err
@@ -156,10 +160,10 @@ func Exec(client internalapi.RuntimeService, opts execOptions) error {
156160
}
157161

158162
logrus.Debugf("Exec URL: %v", URL)
159-
return stream(opts.stdin, opts.tty, URL)
163+
return stream(ctx, opts.stdin, opts.tty, URL)
160164
}
161165

162-
func stream(in, tty bool, url *url.URL) error {
166+
func stream(ctx context.Context, in, tty bool, url *url.URL) error {
163167
executor, err := remoteclient.NewSPDYExecutor(&restclient.Config{TLSClientConfig: restclient.TLSClientConfig{Insecure: true}}, "POST", url)
164168
if err != nil {
165169
return err
@@ -176,7 +180,7 @@ func stream(in, tty bool, url *url.URL) error {
176180
}
177181
logrus.Debugf("StreamOptions: %v", streamOptions)
178182
if !tty {
179-
return executor.Stream(streamOptions)
183+
return executor.StreamWithContext(ctx, streamOptions)
180184
} else {
181185
var detachKeys []byte
182186
detachKeys, err = mobyterm.ToBytes(detachSequence)
@@ -198,5 +202,5 @@ func stream(in, tty bool, url *url.URL) error {
198202
return fmt.Errorf("input is not a terminal")
199203
}
200204
streamOptions.TerminalSizeQueue = t.MonitorSize(t.GetSize())
201-
return t.Safe(func() error { return executor.Stream(streamOptions) })
205+
return t.Safe(func() error { return executor.StreamWithContext(ctx, streamOptions) })
202206
}

pkg/validate/streaming.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ func checkExec(c internalapi.RuntimeService, execServerURL, stdout string, stdou
198198
streamOptions.Stdin = localInRead
199199
streamOptions.Tty = true
200200
}
201-
err = e.Stream(streamOptions)
201+
202+
ctx, cancel := context.WithCancel(context.Background())
203+
defer cancel()
204+
205+
err = e.StreamWithContext(ctx, streamOptions)
202206
framework.ExpectNoError(err, "failed to open streamer for %q", execServerURL)
203207

204208
if stdoutExactMatch {
@@ -296,7 +300,10 @@ func checkAttach(c internalapi.RuntimeService, attachServerURL string) {
296300
e, err := remoteclient.NewSPDYExecutor(&rest.Config{TLSClientConfig: rest.TLSClientConfig{Insecure: true}}, "POST", url)
297301
framework.ExpectNoError(err, "failed to create executor for %q", attachServerURL)
298302

299-
err = e.Stream(remoteclient.StreamOptions{
303+
ctx, cancel := context.WithCancel(context.Background())
304+
defer cancel()
305+
306+
err = e.StreamWithContext(ctx, remoteclient.StreamOptions{
300307
Stdin: reader,
301308
Stdout: localOut,
302309
Stderr: localErr,

0 commit comments

Comments
 (0)