@@ -18,12 +18,13 @@ package cp
18
18
19
19
import (
20
20
"archive/tar"
21
- "bytes "
21
+ "context "
22
22
"errors"
23
23
"fmt"
24
24
"io"
25
25
"os"
26
26
"strings"
27
+ "time"
27
28
28
29
"github.com/spf13/cobra"
29
30
@@ -267,8 +268,8 @@ func (o *CopyOptions) checkDestinationIsDir(dest fileSpec) error {
267
268
options := & exec.ExecOptions {
268
269
StreamOptions : exec.StreamOptions {
269
270
IOStreams : genericiooptions.IOStreams {
270
- Out : bytes . NewBuffer ([] byte {}) ,
271
- ErrOut : bytes . NewBuffer ([] byte {}) ,
271
+ Out : io . Discard ,
272
+ ErrOut : io . Discard ,
272
273
},
273
274
274
275
Namespace : dest .PodNamespace ,
@@ -279,7 +280,21 @@ func (o *CopyOptions) checkDestinationIsDir(dest fileSpec) error {
279
280
Executor : & exec.DefaultRemoteExecutor {},
280
281
}
281
282
282
- return o .execute (options )
283
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
284
+ defer cancel ()
285
+
286
+ done := make (chan error )
287
+
288
+ go func () {
289
+ done <- o .execute (options )
290
+ }()
291
+
292
+ select {
293
+ case <- ctx .Done ():
294
+ return ctx .Err ()
295
+ case err := <- done :
296
+ return err
297
+ }
283
298
}
284
299
285
300
func (o * CopyOptions ) copyToPod (src , dest fileSpec , options * exec.ExecOptions ) error {
@@ -295,6 +310,10 @@ func (o *CopyOptions) copyToPod(src, dest fileSpec, options *exec.ExecOptions) e
295
310
// If no error, dest.File was found to be a directory.
296
311
// Copy specified src into it
297
312
destFile = destFile .Join (srcFile .Base ())
313
+ } else if errors .Is (err , context .DeadlineExceeded ) {
314
+ // we haven't decided destination is directory or not because context timeout is exceeded.
315
+ // That's why, we should shortcut the process in here.
316
+ return err
298
317
}
299
318
300
319
go func (src localPath , dest remotePath , writer io.WriteCloser ) {
0 commit comments