Skip to content

Commit 4cd1cbd

Browse files
Adding a check to test if Tar utility is present in the target container (#974)
* Adding a check to test if Tar utility is present in the target container * Cleaning up the logic * Creating a checked exception and flipping the if check * Adding CopyNotSupportedException to example file * Changed the code to avoid InterruptedException
1 parent c4b72e2 commit 4cd1cbd

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

examples/src/main/java/io/kubernetes/client/examples/CopyExample.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.kubernetes.client.openapi.ApiException;
1919
import io.kubernetes.client.openapi.Configuration;
2020
import io.kubernetes.client.util.Config;
21+
import io.kubernetes.client.util.exception.CopyNotSupportedException;
2122
import java.io.IOException;
2223
import java.io.InputStream;
2324
import java.nio.file.Paths;
@@ -31,7 +32,8 @@
3132
* <p>From inside $REPO_DIR/examples
3233
*/
3334
public class CopyExample {
34-
public static void main(String[] args) throws IOException, ApiException, InterruptedException {
35+
public static void main(String[] args)
36+
throws IOException, ApiException, InterruptedException, CopyNotSupportedException {
3537
String podName = "kube-addon-manager-minikube";
3638
String namespace = "kube-system";
3739

util/src/main/java/io/kubernetes/client/Copy.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.kubernetes.client.openapi.ApiException;
1818
import io.kubernetes.client.openapi.Configuration;
1919
import io.kubernetes.client.openapi.models.V1Pod;
20+
import io.kubernetes.client.util.exception.CopyNotSupportedException;
2021
import java.io.BufferedInputStream;
2122
import java.io.File;
2223
import java.io.FileInputStream;
@@ -93,12 +94,12 @@ public void copyFileFromPod(
9394
}
9495

9596
public void copyDirectoryFromPod(V1Pod pod, String srcPath, Path destination)
96-
throws ApiException, IOException {
97+
throws ApiException, IOException, CopyNotSupportedException {
9798
copyDirectoryFromPod(pod, null, srcPath, destination);
9899
}
99100

100101
public void copyDirectoryFromPod(V1Pod pod, String container, String srcPath, Path destination)
101-
throws ApiException, IOException {
102+
throws ApiException, IOException, CopyNotSupportedException {
102103
copyDirectoryFromPod(
103104
pod.getMetadata().getNamespace(),
104105
pod.getMetadata().getName(),
@@ -108,14 +109,17 @@ public void copyDirectoryFromPod(V1Pod pod, String container, String srcPath, Pa
108109
}
109110

110111
public void copyDirectoryFromPod(String namespace, String pod, String srcPath, Path destination)
111-
throws ApiException, IOException {
112+
throws ApiException, IOException, CopyNotSupportedException {
112113
copyDirectoryFromPod(namespace, pod, null, srcPath, destination);
113114
}
114115

115116
public void copyDirectoryFromPod(
116117
String namespace, String pod, String container, String srcPath, Path destination)
117-
throws ApiException, IOException {
118-
// TODO: Test that 'tar' is present in the container?
118+
throws ApiException, IOException, CopyNotSupportedException {
119+
// Test that 'tar' is present in the container?
120+
if (!isTarPresentInContainer(namespace, pod, container)) {
121+
throw new CopyNotSupportedException("Tar is not present in the target container");
122+
}
119123
final Process proc =
120124
this.exec(
121125
namespace,
@@ -202,4 +206,20 @@ public void copyFileToPod(
202206

203207
return;
204208
}
209+
210+
private boolean isTarPresentInContainer(String namespace, String pod, String container)
211+
throws ApiException, IOException {
212+
final Process proc =
213+
this.exec(
214+
namespace, pod, new String[] {"sh", "-c", "tar --version"}, container, false, false);
215+
// This will work for POSIX based operating systems
216+
try {
217+
int status = proc.waitFor();
218+
return status == 127 ? false : true;
219+
} catch (InterruptedException ex) {
220+
throw new IOException(ex);
221+
} finally {
222+
proc.destroy();
223+
}
224+
}
205225
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.kubernetes.client.util.exception;
2+
3+
public class CopyNotSupportedException extends Exception {
4+
5+
public CopyNotSupportedException(String errorMessage) {
6+
super(errorMessage);
7+
}
8+
}

0 commit comments

Comments
 (0)