mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
[kubectl] Fail when local source file doesn't exist
When trying to upload an unexisting file to a pod, kubectl currently doesn't print any error message and terminates with a zero exit code. This adds a check and fails explicitly in such cases.
This commit is contained in:
parent
5a4aa2026c
commit
3fa5b50442
@ -203,10 +203,7 @@ func (o *CopyOptions) Run(args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(srcSpec.PodName) != 0 && len(destSpec.PodName) != 0 {
|
if len(srcSpec.PodName) != 0 && len(destSpec.PodName) != 0 {
|
||||||
if _, err := os.Stat(args[0]); err == nil {
|
return fmt.Errorf("one of src or dest must be a local file specification")
|
||||||
return o.copyToPod(fileSpec{File: args[0]}, destSpec, &exec.ExecOptions{})
|
|
||||||
}
|
|
||||||
return fmt.Errorf("src doesn't exist in local filesystem")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(srcSpec.PodName) != 0 {
|
if len(srcSpec.PodName) != 0 {
|
||||||
@ -245,6 +242,9 @@ func (o *CopyOptions) copyToPod(src, dest fileSpec, options *exec.ExecOptions) e
|
|||||||
if len(src.File) == 0 || len(dest.File) == 0 {
|
if len(src.File) == 0 || len(dest.File) == 0 {
|
||||||
return errFileCannotBeEmpty
|
return errFileCannotBeEmpty
|
||||||
}
|
}
|
||||||
|
if _, err := os.Stat(src.File); err != nil {
|
||||||
|
return fmt.Errorf("%s doesn't exist in local filesystem", src.File)
|
||||||
|
}
|
||||||
reader, writer := io.Pipe()
|
reader, writer := io.Pipe()
|
||||||
|
|
||||||
// strip trailing slash (if any)
|
// strip trailing slash (if any)
|
||||||
|
@ -576,27 +576,36 @@ func TestCopyToPod(t *testing.T) {
|
|||||||
defer os.RemoveAll(srcFile)
|
defer os.RemoveAll(srcFile)
|
||||||
|
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
|
src string
|
||||||
dest string
|
dest string
|
||||||
expectedErr bool
|
expectedErr bool
|
||||||
}{
|
}{
|
||||||
"copy to directory": {
|
"copy to directory": {
|
||||||
|
src: srcFile,
|
||||||
dest: "/tmp/",
|
dest: "/tmp/",
|
||||||
expectedErr: false,
|
expectedErr: false,
|
||||||
},
|
},
|
||||||
"copy to root": {
|
"copy to root": {
|
||||||
|
src: srcFile,
|
||||||
dest: "/",
|
dest: "/",
|
||||||
expectedErr: false,
|
expectedErr: false,
|
||||||
},
|
},
|
||||||
"copy to empty file name": {
|
"copy to empty file name": {
|
||||||
|
src: srcFile,
|
||||||
dest: "",
|
dest: "",
|
||||||
expectedErr: true,
|
expectedErr: true,
|
||||||
},
|
},
|
||||||
|
"copy unexisting file": {
|
||||||
|
src: path.Join(srcFile, "nope"),
|
||||||
|
dest: "/tmp",
|
||||||
|
expectedErr: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
opts := NewCopyOptions(ioStreams)
|
opts := NewCopyOptions(ioStreams)
|
||||||
src := fileSpec{
|
src := fileSpec{
|
||||||
File: srcFile,
|
File: test.src,
|
||||||
}
|
}
|
||||||
dest := fileSpec{
|
dest := fileSpec{
|
||||||
PodNamespace: "pod-ns",
|
PodNamespace: "pod-ns",
|
||||||
|
Loading…
Reference in New Issue
Block a user