Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload file failed if the path is "/" #3568

Closed
yaojiqunaer opened this issue Nov 4, 2021 · 2 comments · Fixed by #3719
Closed

upload file failed if the path is "/" #3568

yaojiqunaer opened this issue Nov 4, 2021 · 2 comments · Fixed by #3719
Assignees
Milestone

Comments

@yaojiqunaer
Copy link

yaojiqunaer commented Nov 4, 2021

Description

If my file is uploaded to the root path of the container like "/" it will give an error, like "/var" it will not

Dependency

<dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>kubernetes-client</artifactId>
    <version>5.9.0</version>
</dependency>

My code

    @Test
    public void upToRootTest() {
        try (final KubernetesClient k8s = client) {
            final File source = new File("/tmp/cp.log");
            //"/var/cp.log" is true
            //final String destFile="/var/cp.log";
            //"/cp.log" will be error
            final String destFile="/cp.log";
            Boolean upload = k8s.pods().inNamespace("namespace")
                    .withName("name")
                    .inContainer("spring-boot")
                    .file(destFile)             // <- Target location of copied file inside Pod
                    .upload(source.toPath());  // <- Path of local file
            System.out.println(upload);
        } catch (KubernetesClientException kce) {
            log.error("upload to / failed", kce);
        }
    }

Exception

19:01:46.785 [main] ERROR com.xxx.xxx.console.k8s.exec.ContainerFileTest - upload to / failed
io.fabric8.kubernetes.client.KubernetesClientException: [size=215 text={"metadata":{},"status":"Failure","message":"command terminated …]
	at io.fabric8.kubernetes.client.dsl.internal.uploadable.PodUploadWebSocketListener.checkError(PodUploadWebSocketListener.java:91)
	at io.fabric8.kubernetes.client.dsl.internal.uploadable.PodUploadWebSocketListener.send(PodUploadWebSocketListener.java:115)
	at io.fabric8.kubernetes.client.dsl.internal.uploadable.PodUpload.copy(PodUpload.java:142)
	at io.fabric8.kubernetes.client.dsl.internal.uploadable.PodUpload.uploadFile(PodUpload.java:86)
	at io.fabric8.kubernetes.client.dsl.internal.uploadable.PodUpload.upload(PodUpload.java:64)
	at io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl.lambda$upload$0(PodOperationsImpl.java:391)
	at io.fabric8.kubernetes.client.utils.OptionalDependencyWrapper.wrapRunWithOptionalDependency(OptionalDependencyWrapper.java:39)
	at io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl.upload(PodOperationsImpl.java:389)
	at io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl.upload(PodOperationsImpl.java:84)
	at com.trs.devops.console.k8s.exec.ContainerFileTest.upToRootTest(ContainerFileTest.java:58)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Reason maybe here

mehtod in io.fabric8.kubernetes.client.dsl.internal.uploadable.PodUpload#uploadFile
image
the substing reture "";
final String directory = file.substring(0, file.lastIndexOf('/'));
the directory is "" when file is "/xxx.xxx"

Other requirements

Upload method only support java.nio.file.Path as a local file path, if i use the org.springframework.web.multipart.MultipartFile uploaded files, are unable to get the file(getFile methord is forbit),so i must write multiPartFIle inputStrem to local disk and then read file as Path . Why not support both Path and InputStream?

@rohanKanojia
Copy link
Member

rohanKanojia commented Jan 6, 2022

@yaojiqunaer : Thanks for reporting this. I can reproduce this issue. This issue can be fixed if we just assume directory to be / in case it's empty as you rightly suggested:

-    final String directory = file.substring(0, file.lastIndexOf('/'));
+    final String directory = file.substring(0, file.lastIndexOf('/')).isEmpty() ?
+      "/" :
+      file.substring(0, file.lastIndexOf('/'));

I see you have an additional requirement for having an additional upload(InputStream inputStream) method for uploading files. Shall we do it as part of this issue or create a separate issue for this enhancement?

@rohanKanojia rohanKanojia self-assigned this Jan 6, 2022
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue Jan 6, 2022
… path

Pod file upload seems be be creating invalid exec command for upload if
the file is directly in root path e.g. `/cp.log`. We create parent
directory of file by creating a substring based on last index of `/`.
This returns an empty string for a string like `/cp.log`.

Assume directory path to be `/` when directory is returned as empty string.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
@rohanKanojia
Copy link
Member

@yaojiqunaer : I've created #3721 for the enhancement you've requested

rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue Jan 10, 2022
… path

Pod file upload seems be be creating invalid exec command for upload if
the file is directly in root path e.g. `/cp.log`. We create parent
directory of file by creating a substring based on last index of `/`.
This returns an empty string for a string like `/cp.log`.

Assume directory path to be `/` when directory is returned as empty string.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
manusa pushed a commit to rohanKanojia/kubernetes-client that referenced this issue Jan 11, 2022
… path

Pod file upload seems be be creating invalid exec command for upload if
the file is directly in root path e.g. `/cp.log`. We create parent
directory of file by creating a substring based on last index of `/`.
This returns an empty string for a string like `/cp.log`.

Assume directory path to be `/` when directory is returned as empty string.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
@manusa manusa added this to the 5.12.0 milestone Jan 11, 2022
manusa pushed a commit that referenced this issue Jan 11, 2022
Pod file upload seems be be creating invalid exec command for upload if
the file is directly in root path e.g. `/cp.log`. We create parent
directory of file by creating a substring based on last index of `/`.
This returns an empty string for a string like `/cp.log`.

Assume directory path to be `/` when directory is returned as empty string.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants