Skip to content

Commit d981b19

Browse files
committed
Add timeout cancellation to kubectl cp destination path check
1 parent 099a883 commit d981b19

File tree

1 file changed

+17
-1
lines changed
  • staging/src/k8s.io/kubectl/pkg/cmd/cp

1 file changed

+17
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ package cp
1919
import (
2020
"archive/tar"
2121
"bytes"
22+
"context"
2223
"errors"
2324
"fmt"
2425
"io"
2526
"os"
2627
"strings"
28+
"time"
2729

2830
"github.com/spf13/cobra"
2931

@@ -279,7 +281,21 @@ func (o *CopyOptions) checkDestinationIsDir(dest fileSpec) error {
279281
Executor: &exec.DefaultRemoteExecutor{},
280282
}
281283

282-
return o.execute(options)
284+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
285+
defer cancel()
286+
287+
done := make(chan error)
288+
289+
go func() {
290+
done <- o.execute(options)
291+
}()
292+
293+
select {
294+
case <-ctx.Done():
295+
return fmt.Errorf("timeout exceeded while checking the destination")
296+
case err := <-done:
297+
return err
298+
}
283299
}
284300

285301
func (o *CopyOptions) copyToPod(src, dest fileSpec, options *exec.ExecOptions) error {

0 commit comments

Comments
 (0)