@@ -95,7 +95,11 @@ var runtimeExecCommand = &cli.Command{
95
95
}
96
96
return nil
97
97
}
98
- err = Exec (runtimeClient , opts )
98
+
99
+ ctx , cancel := context .WithCancel (c .Context )
100
+ defer cancel ()
101
+
102
+ err = Exec (ctx , runtimeClient , opts )
99
103
if err != nil {
100
104
return fmt .Errorf ("execing command in container: %w" , err )
101
105
}
@@ -124,7 +128,7 @@ func ExecSync(client internalapi.RuntimeService, opts execOptions) (int, error)
124
128
}
125
129
126
130
// 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 {
128
132
request := & pb.ExecRequest {
129
133
ContainerId : opts .id ,
130
134
Cmd : opts .cmd ,
@@ -135,7 +139,7 @@ func Exec(client internalapi.RuntimeService, opts execOptions) error {
135
139
}
136
140
137
141
logrus .Debugf ("ExecRequest: %v" , request )
138
- r , err := client .Exec (context . TODO () , request )
142
+ r , err := client .Exec (ctx , request )
139
143
logrus .Debugf ("ExecResponse: %v" , r )
140
144
if err != nil {
141
145
return err
@@ -156,10 +160,10 @@ func Exec(client internalapi.RuntimeService, opts execOptions) error {
156
160
}
157
161
158
162
logrus .Debugf ("Exec URL: %v" , URL )
159
- return stream (opts .stdin , opts .tty , URL )
163
+ return stream (ctx , opts .stdin , opts .tty , URL )
160
164
}
161
165
162
- func stream (in , tty bool , url * url.URL ) error {
166
+ func stream (ctx context. Context , in , tty bool , url * url.URL ) error {
163
167
executor , err := remoteclient .NewSPDYExecutor (& restclient.Config {TLSClientConfig : restclient.TLSClientConfig {Insecure : true }}, "POST" , url )
164
168
if err != nil {
165
169
return err
@@ -176,7 +180,7 @@ func stream(in, tty bool, url *url.URL) error {
176
180
}
177
181
logrus .Debugf ("StreamOptions: %v" , streamOptions )
178
182
if ! tty {
179
- return executor .Stream ( streamOptions )
183
+ return executor .StreamWithContext ( ctx , streamOptions )
180
184
} else {
181
185
var detachKeys []byte
182
186
detachKeys , err = mobyterm .ToBytes (detachSequence )
@@ -198,5 +202,5 @@ func stream(in, tty bool, url *url.URL) error {
198
202
return fmt .Errorf ("input is not a terminal" )
199
203
}
200
204
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 ) })
202
206
}
0 commit comments