From d981b19ad30c3396e8a4d197bedc346e68e0270b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Tue, 13 Aug 2024 11:02:04 +0300 Subject: [PATCH] Add timeout cancellation to kubectl cp destination path check --- staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go b/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go index 5f07d51df3d..b4d3748c387 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go @@ -19,11 +19,13 @@ package cp import ( "archive/tar" "bytes" + "context" "errors" "fmt" "io" "os" "strings" + "time" "github.com/spf13/cobra" @@ -279,7 +281,21 @@ func (o *CopyOptions) checkDestinationIsDir(dest fileSpec) error { Executor: &exec.DefaultRemoteExecutor{}, } - return o.execute(options) + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + done := make(chan error) + + go func() { + done <- o.execute(options) + }() + + select { + case <-ctx.Done(): + return fmt.Errorf("timeout exceeded while checking the destination") + case err := <-done: + return err + } } func (o *CopyOptions) copyToPod(src, dest fileSpec, options *exec.ExecOptions) error {