|
| 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