diff --git a/examples/src/main/java/io/kubernetes/client/examples/CopyExample.java b/examples/src/main/java/io/kubernetes/client/examples/CopyExample.java index 7d80df7a09..b24043ff7c 100644 --- a/examples/src/main/java/io/kubernetes/client/examples/CopyExample.java +++ b/examples/src/main/java/io/kubernetes/client/examples/CopyExample.java @@ -18,6 +18,7 @@ import io.kubernetes.client.openapi.ApiException; import io.kubernetes.client.openapi.Configuration; import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.exception.CopyNotSupportedException; import java.io.IOException; import java.io.InputStream; import java.nio.file.Paths; @@ -31,7 +32,8 @@ *

From inside $REPO_DIR/examples */ public class CopyExample { - public static void main(String[] args) throws IOException, ApiException, InterruptedException { + public static void main(String[] args) + throws IOException, ApiException, InterruptedException, CopyNotSupportedException { String podName = "kube-addon-manager-minikube"; String namespace = "kube-system"; diff --git a/util/src/main/java/io/kubernetes/client/Copy.java b/util/src/main/java/io/kubernetes/client/Copy.java index b6a9648e6c..3d108a2634 100644 --- a/util/src/main/java/io/kubernetes/client/Copy.java +++ b/util/src/main/java/io/kubernetes/client/Copy.java @@ -17,6 +17,7 @@ import io.kubernetes.client.openapi.ApiException; import io.kubernetes.client.openapi.Configuration; import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.util.exception.CopyNotSupportedException; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; @@ -93,12 +94,12 @@ public void copyFileFromPod( } public void copyDirectoryFromPod(V1Pod pod, String srcPath, Path destination) - throws ApiException, IOException { + throws ApiException, IOException, CopyNotSupportedException { copyDirectoryFromPod(pod, null, srcPath, destination); } public void copyDirectoryFromPod(V1Pod pod, String container, String srcPath, Path destination) - throws ApiException, IOException { + throws ApiException, IOException, CopyNotSupportedException { copyDirectoryFromPod( pod.getMetadata().getNamespace(), pod.getMetadata().getName(), @@ -108,14 +109,17 @@ public void copyDirectoryFromPod(V1Pod pod, String container, String srcPath, Pa } public void copyDirectoryFromPod(String namespace, String pod, String srcPath, Path destination) - throws ApiException, IOException { + throws ApiException, IOException, CopyNotSupportedException { copyDirectoryFromPod(namespace, pod, null, srcPath, destination); } public void copyDirectoryFromPod( String namespace, String pod, String container, String srcPath, Path destination) - throws ApiException, IOException { - // TODO: Test that 'tar' is present in the container? + throws ApiException, IOException, CopyNotSupportedException { + // Test that 'tar' is present in the container? + if (!isTarPresentInContainer(namespace, pod, container)) { + throw new CopyNotSupportedException("Tar is not present in the target container"); + } final Process proc = this.exec( namespace, @@ -202,4 +206,20 @@ public void copyFileToPod( return; } + + private boolean isTarPresentInContainer(String namespace, String pod, String container) + throws ApiException, IOException { + final Process proc = + this.exec( + namespace, pod, new String[] {"sh", "-c", "tar --version"}, container, false, false); + // This will work for POSIX based operating systems + try { + int status = proc.waitFor(); + return status == 127 ? false : true; + } catch (InterruptedException ex) { + throw new IOException(ex); + } finally { + proc.destroy(); + } + } } diff --git a/util/src/main/java/io/kubernetes/client/util/exception/CopyNotSupportedException.java b/util/src/main/java/io/kubernetes/client/util/exception/CopyNotSupportedException.java new file mode 100644 index 0000000000..5b6739425d --- /dev/null +++ b/util/src/main/java/io/kubernetes/client/util/exception/CopyNotSupportedException.java @@ -0,0 +1,8 @@ +package io.kubernetes.client.util.exception; + +public class CopyNotSupportedException extends Exception { + + public CopyNotSupportedException(String errorMessage) { + super(errorMessage); + } +}