Skip to content

Commit c999f9d

Browse files
authored
Merge pull request kubernetes#126652 from ardaguclu/add-timeout-cp-dest-check
Add timeout cancellation to kubectl cp destination path check
2 parents 114900a + ca2c9c6 commit c999f9d

File tree

1 file changed

+23
-4
lines changed
  • staging/src/k8s.io/kubectl/pkg/cmd/cp

1 file changed

+23
-4
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ package cp
1818

1919
import (
2020
"archive/tar"
21-
"bytes"
21+
"context"
2222
"errors"
2323
"fmt"
2424
"io"
2525
"os"
2626
"strings"
27+
"time"
2728

2829
"github.com/spf13/cobra"
2930

@@ -267,8 +268,8 @@ func (o *CopyOptions) checkDestinationIsDir(dest fileSpec) error {
267268
options := &exec.ExecOptions{
268269
StreamOptions: exec.StreamOptions{
269270
IOStreams: genericiooptions.IOStreams{
270-
Out: bytes.NewBuffer([]byte{}),
271-
ErrOut: bytes.NewBuffer([]byte{}),
271+
Out: io.Discard,
272+
ErrOut: io.Discard,
272273
},
273274

274275
Namespace: dest.PodNamespace,
@@ -279,7 +280,21 @@ func (o *CopyOptions) checkDestinationIsDir(dest fileSpec) error {
279280
Executor: &exec.DefaultRemoteExecutor{},
280281
}
281282

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+
}
283298
}
284299

285300
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
295310
// If no error, dest.File was found to be a directory.
296311
// Copy specified src into it
297312
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
298317
}
299318

300319
go func(src localPath, dest remotePath, writer io.WriteCloser) {

0 commit comments

Comments
 (0)