mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Merge pull request #126652 from ardaguclu/add-timeout-cp-dest-check
Add timeout cancellation to kubectl cp destination path check
This commit is contained in:
commit
c999f9d828
@ -18,12 +18,13 @@ package cp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"bytes"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
@ -267,8 +268,8 @@ func (o *CopyOptions) checkDestinationIsDir(dest fileSpec) error {
|
|||||||
options := &exec.ExecOptions{
|
options := &exec.ExecOptions{
|
||||||
StreamOptions: exec.StreamOptions{
|
StreamOptions: exec.StreamOptions{
|
||||||
IOStreams: genericiooptions.IOStreams{
|
IOStreams: genericiooptions.IOStreams{
|
||||||
Out: bytes.NewBuffer([]byte{}),
|
Out: io.Discard,
|
||||||
ErrOut: bytes.NewBuffer([]byte{}),
|
ErrOut: io.Discard,
|
||||||
},
|
},
|
||||||
|
|
||||||
Namespace: dest.PodNamespace,
|
Namespace: dest.PodNamespace,
|
||||||
@ -279,7 +280,21 @@ func (o *CopyOptions) checkDestinationIsDir(dest fileSpec) error {
|
|||||||
Executor: &exec.DefaultRemoteExecutor{},
|
Executor: &exec.DefaultRemoteExecutor{},
|
||||||
}
|
}
|
||||||
|
|
||||||
return o.execute(options)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
done := make(chan error)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
done <- o.execute(options)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
case err := <-done:
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *CopyOptions) copyToPod(src, dest fileSpec, options *exec.ExecOptions) error {
|
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
|
|||||||
// If no error, dest.File was found to be a directory.
|
// If no error, dest.File was found to be a directory.
|
||||||
// Copy specified src into it
|
// Copy specified src into it
|
||||||
destFile = destFile.Join(srcFile.Base())
|
destFile = destFile.Join(srcFile.Base())
|
||||||
|
} else if errors.Is(err, context.DeadlineExceeded) {
|
||||||
|
// we haven't decided destination is directory or not because context timeout is exceeded.
|
||||||
|
// That's why, we should shortcut the process in here.
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
go func(src localPath, dest remotePath, writer io.WriteCloser) {
|
go func(src localPath, dest remotePath, writer io.WriteCloser) {
|
||||||
|
Loading…
Reference in New Issue
Block a user