Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +32,8 @@
* <p>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";

Expand Down
30 changes: 25 additions & 5 deletions util/src/main/java/io/kubernetes/client/Copy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.kubernetes.client.util.exception;

public class CopyNotSupportedException extends Exception {

public CopyNotSupportedException(String errorMessage) {
super(errorMessage);
}
}