From 76596c8f50f2e28c8753567c10a660cfdbce17ed Mon Sep 17 00:00:00 2001 From: Florian Hussonnois Date: Tue, 6 Aug 2024 12:18:46 +0200 Subject: [PATCH] fix(client): improve reliability of file upload to pod (#6212) Fix: #6212 Signed-off-by: Florian Hussonnois (cherry picked from commit f3ec2cdc80ca686f5d7c302466d3e83c54f5a17c) --- CHANGELOG.md | 1 + .../client/dsl/internal/uploadable/PodUpload.java | 2 +- .../client/dsl/internal/uploadable/PodUploadTest.java | 9 +++++---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24086e950cb..134080327bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Fix #6066: Added support for missing `v1.APIVersions` in KubernetesClient * Fix #6110: VolumeSource (and other file mode fields) in Octal are correctly interpreted * Fix #6215: Suppressing rejected execution exception for port forwarder +* Fix #6212: Improved reliability of file upload to Pod ### 6.13.1 (2024-07-02) diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUpload.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUpload.java index 2c08c064e2f..c105023c736 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUpload.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUpload.java @@ -184,7 +184,7 @@ private static void addFileToTar(String fileName, File file, TarArchiveOutputStr static String createExecCommandForUpload(String file) { return String.format( - "mkdir -p %s && cat - > %s", shellQuote(getDirectoryFromFile(file)), shellQuote(file)); + "mkdir -p %s && cat - > %s && echo $?", shellQuote(getDirectoryFromFile(file)), shellQuote(file)); } private static String ensureEndsWithSlash(String path) { diff --git a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUploadTest.java b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUploadTest.java index 0e3ad7716d4..9509c2e2880 100644 --- a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUploadTest.java +++ b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUploadTest.java @@ -49,7 +49,7 @@ void withFileInRootPath_shouldCreateValidExecCommandForUpload() { // When String result = PodUpload.createExecCommandForUpload("/cp.log"); // Then - assertThat(result).isEqualTo("mkdir -p '/' && cat - > '/cp.log'"); + assertThat(result).isEqualTo("mkdir -p '/' && cat - > '/cp.log' && echo $?"); } @Test @@ -57,7 +57,7 @@ void withNormalFile_shouldCreateValidExecCommandForUpload() { // When String result = PodUpload.createExecCommandForUpload("/tmp/foo/cp.log"); // Then - assertThat(result).isEqualTo("mkdir -p '/tmp/foo/' && cat - > '/tmp/foo/cp.log'"); + assertThat(result).isEqualTo("mkdir -p '/tmp/foo/' && cat - > '/tmp/foo/cp.log' && echo $?"); } // @@ -66,7 +66,7 @@ void withSingleQuoteInPath() { // When String result = PodUpload.createExecCommandForUpload("/tmp/fo'o/cp.log"); // Then - assertThat(result).isEqualTo("mkdir -p '/tmp/fo\'\\'\'o/' && cat - > '/tmp/fo\'\\'\'o/cp.log'"); + assertThat(result).isEqualTo("mkdir -p '/tmp/fo\'\\'\'o/' && cat - > '/tmp/fo\'\\'\'o/cp.log' && echo $?"); } @Test @@ -74,7 +74,8 @@ void withMultipleSingleQuotesInPath() { // When String result = PodUpload.createExecCommandForUpload("/tmp/f'o'o/c'p.log"); // Then - assertThat(result).isEqualTo("mkdir -p '/tmp/f\'\\'\'o\'\\'\'o/' && cat - > '/tmp/f\'\\'\'o\'\\'\'o/c\'\\'\'p.log'"); + assertThat(result) + .isEqualTo("mkdir -p '/tmp/f\'\\'\'o\'\\'\'o/' && cat - > '/tmp/f\'\\'\'o\'\\'\'o/c\'\\'\'p.log' && echo $?"); } }