Skip to content

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

Closed
@yaojiqunaer

Description

@yaojiqunaer

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?

Activity

rohanKanojia

rohanKanojia commented on Jan 6, 2022

@rohanKanojia
Member

@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?

self-assigned this
on Jan 6, 2022
added a commit that references this issue on Jan 6, 2022

Fix fabric8io#3568: Pod upload file fails if file is directly in root…

756de1e
rohanKanojia

rohanKanojia commented on Jan 7, 2022

@rohanKanojia
Member

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

added a commit that references this issue on Jan 10, 2022

Fix fabric8io#3568: Pod upload file fails if file is directly in root…

f08cad0
added a commit that references this issue on Jan 11, 2022

Fix fabric8io#3568: Pod upload file fails if file is directly in root…

52edaf4
added this to the 5.12.0 milestone on Jan 11, 2022
added a commit that references this issue on Jan 11, 2022

Fix #3568: Pod upload file fails if file is directly in root path

2499f93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @manusa@rohanKanojia@yaojiqunaer

    Issue actions

      upload file failed if the path is "/" · Issue #3568 · fabric8io/kubernetes-client