Skip to content

Commit 17fb01a

Browse files
committed
Add some initial utilities for copying files.
1 parent f1819ee commit 17fb01a

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2017, 2018 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.examples;
14+
15+
import com.google.common.io.ByteStreams;
16+
import io.kubernetes.client.ApiClient;
17+
import io.kubernetes.client.ApiException;
18+
import io.kubernetes.client.Configuration;
19+
import io.kubernetes.client.Copy;
20+
import io.kubernetes.client.util.Config;
21+
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
25+
/**
26+
* A simple example of how to use the Java API
27+
*
28+
* <p>Easiest way to run this: mvn exec:java
29+
* -Dexec.mainClass="io.kubernetes.client.examples.CopyExample"
30+
*
31+
* <p>From inside $REPO_DIR/examples
32+
*/
33+
public class CopyExample {
34+
public static void main(String[] args) throws IOException, ApiException, InterruptedException {
35+
String podName = "kube-addon-manager-minikube";
36+
String namespace = "kube-system";
37+
38+
ApiClient client = Config.defaultClient();
39+
Configuration.setDefaultApiClient(client);
40+
41+
Copy copy = new Copy();
42+
InputStream dataStream = copy.copyFile(namespace, podName, "/etc/motd");
43+
44+
ByteStreams.copy(dataStream, System.out);
45+
}
46+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2017, 2018 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client;
14+
15+
import com.google.common.io.ByteStreams;
16+
import io.kubernetes.client.models.V1Pod;
17+
import java.io.ByteArrayInputStream;
18+
import java.io.ByteArrayOutputStream;
19+
import java.io.FileOutputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.nio.file.Path;
23+
import org.apache.commons.codec.binary.Base64;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
public class Copy extends Exec {
28+
private static final Logger log = LoggerFactory.getLogger(Copy.class);
29+
30+
/** Simple Copy constructor, uses default configuration */
31+
public Copy() {
32+
super(Configuration.getDefaultApiClient());
33+
}
34+
35+
/**
36+
* Copy Constructor
37+
*
38+
* @param apiClient The api client to use.
39+
*/
40+
public Copy(ApiClient apiClient) {
41+
super(apiClient);
42+
}
43+
44+
public InputStream copyFile(String namespace, String pod, String srcPath)
45+
throws ApiException, IOException {
46+
return copyFile(namespace, pod, null, srcPath);
47+
}
48+
49+
public InputStream copyFile(V1Pod pod, String srcPath) throws ApiException, IOException {
50+
return copyFile(pod, null, srcPath);
51+
}
52+
53+
public InputStream copyFile(V1Pod pod, String container, String srcPath)
54+
throws ApiException, IOException {
55+
return copyFile(
56+
pod.getMetadata().getNamespace(), pod.getMetadata().getName(), container, srcPath);
57+
}
58+
59+
public InputStream copyFile(String namespace, String pod, String container, String srcPath)
60+
throws ApiException, IOException {
61+
Process proc =
62+
this.exec(
63+
namespace,
64+
pod,
65+
new String[] {"sh", "-c", "cat " + srcPath + " | base64"},
66+
container,
67+
false,
68+
false);
69+
try {
70+
proc.waitFor();
71+
} catch (InterruptedException ex) {
72+
log.warn("Interrupted waiting for copy to complete", ex);
73+
}
74+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
75+
ByteStreams.copy(proc.getInputStream(), bos);
76+
77+
return new ByteArrayInputStream(Base64.decodeBase64(bos.toByteArray()));
78+
}
79+
80+
public static void copyFile(String namespace, String pod, String srcPath, Path dest)
81+
throws ApiException, IOException {
82+
Copy c = new Copy();
83+
InputStream is = c.copyFile(namespace, pod, null, srcPath);
84+
FileOutputStream os = new FileOutputStream(dest.toFile());
85+
ByteStreams.copy(is, os);
86+
os.flush();
87+
os.close();
88+
}
89+
}

0 commit comments

Comments
 (0)