|
| 1 | +package io.kubernetes.client; |
| 2 | + |
| 3 | +import io.kubernetes.client.Configuration; |
| 4 | +import io.kubernetes.client.models.V1Pod; |
| 5 | +import io.kubernetes.client.util.WebSockets; |
| 6 | + |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.Closeable; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.OutputStream; |
| 12 | +import java.io.PipedInputStream; |
| 13 | +import java.io.PipedOutputStream; |
| 14 | +import java.io.Reader; |
| 15 | + |
| 16 | +import org.apache.commons.lang.StringUtils; |
| 17 | + |
| 18 | +public class Exec { |
| 19 | + private ApiClient apiClient; |
| 20 | + |
| 21 | + /** |
| 22 | + * Simple Exec API constructor, uses default configuration |
| 23 | + */ |
| 24 | + public Exec() { |
| 25 | + this(Configuration.getDefaultApiClient()); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Exec API Constructor |
| 30 | + * @param apiClient The api client to use. |
| 31 | + */ |
| 32 | + public Exec(ApiClient apiClient) { |
| 33 | + this.apiClient = apiClient; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Get the API client for these exec operations. |
| 38 | + * @returns The API client that will be used. |
| 39 | + */ |
| 40 | + public ApiClient getApiClient() { |
| 41 | + return apiClient; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Set the API client for subsequent exec operations. |
| 46 | + * @param apiClient The new API client to use. |
| 47 | + */ |
| 48 | + public void setApiClient(ApiClient apiClient) { |
| 49 | + this.apiClient = apiClient; |
| 50 | + } |
| 51 | + |
| 52 | + private String makePath(String namespace, String name, String[] command, String container, boolean stdin, boolean tty) { |
| 53 | + String path = "/api/v1/namespaces/" + |
| 54 | + namespace + |
| 55 | + "/pods/" + |
| 56 | + name + |
| 57 | + "/exec?" + |
| 58 | + "stdin=" + stdin + |
| 59 | + "&tty=" + tty + |
| 60 | + (container != null ? "&container=" + container : "") + |
| 61 | + "&command=" + StringUtils.join(command, "&command="); |
| 62 | + return path; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Execute a command in a container. If there are multiple containers in the pod, uses |
| 67 | + * the first container in the Pod. |
| 68 | + * |
| 69 | + * @param namespace The namespace of the Pod |
| 70 | + * @param name The name of the Pod |
| 71 | + * @param command The command to run |
| 72 | + * @param stdin If true, pass a stdin stream into the container |
| 73 | + */ |
| 74 | + public Process exec(String namespace, String name, String[] command, boolean stdin) throws ApiException, IOException { |
| 75 | + return exec(namespace, name, command, null, stdin, false); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Execute a command in a container. If there are multiple containers in the pod, uses |
| 80 | + * the first container in the Pod. |
| 81 | + * |
| 82 | + * @param pod The pod where the command is run. |
| 83 | + * @param command The command to run |
| 84 | + * @param stdin If true, pass a stdin stream into the container |
| 85 | + */ |
| 86 | + public Process exec(V1Pod pod, String[] command, boolean stdin) throws ApiException, IOException { |
| 87 | + return exec(pod, command, null, stdin, false); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + /** |
| 92 | + * Execute a command in a container. If there are multiple containers in the pod, uses |
| 93 | + * the first container in the Pod. |
| 94 | + * |
| 95 | + * @param pod The pod where the command is run. |
| 96 | + * @param command The command to run |
| 97 | + * @param stdin If true, pass a stdin stream into the container |
| 98 | + * @param tty If true, stdin is a tty. |
| 99 | + */ |
| 100 | + public Process exec(V1Pod pod, String[] command, boolean stdin, boolean tty) throws ApiException, IOException { |
| 101 | + return exec(pod, command, null, stdin, tty); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Execute a command in a container. If there are multiple containers in the pod, uses |
| 106 | + * the first container in the Pod. |
| 107 | + * |
| 108 | + * @param pod The pod where the command is run. |
| 109 | + * @param command The command to run |
| 110 | + * @param container The container in the Pod where the command is run. |
| 111 | + * @param stdin If true, pass a stdin stream into the container. |
| 112 | + * @param tty If true, stdin is a TTY (only applies if stdin is true) |
| 113 | + */ |
| 114 | + public Process exec(V1Pod pod, String[] command, String container, boolean stdin, boolean tty) throws ApiException, IOException { |
| 115 | + return exec(pod.getMetadata().getNamespace(), pod.getMetadata().getName(), command, container, stdin, tty); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Execute a command in a container. If there are multiple containers in the pod, uses |
| 120 | + * the first container in the Pod. |
| 121 | + * |
| 122 | + * @param namespace The namespace of the Pod |
| 123 | + * @param name The name of the Pod |
| 124 | + * @param command The command to run |
| 125 | + * @param container The container in the Pod where the command is run. |
| 126 | + * @param stdin If true, pass a stdin stream into the container. |
| 127 | + * @param tty If true, stdin is a TTY (only applies if stdin is true) |
| 128 | + */ |
| 129 | + public Process exec(String namespace, String name, String[] command, String container, boolean stdin, boolean tty) throws ApiException, IOException { |
| 130 | + String path = makePath(namespace, name, command, container, stdin, tty); |
| 131 | + |
| 132 | + ExecProcess exec = new ExecProcess(); |
| 133 | + WebSockets.stream(path, "GET", apiClient, exec); |
| 134 | + |
| 135 | + return exec; |
| 136 | + } |
| 137 | + |
| 138 | + private static class ExecProcess extends Process implements WebSockets.SocketListener { |
| 139 | + private PipedInputStream pipeIn; |
| 140 | + private PipedOutputStream pipeOut; |
| 141 | + private PipedOutputStream pipeErr; |
| 142 | + private InputStream err; |
| 143 | + private OutputStream output; |
| 144 | + private InputStream input; |
| 145 | + private int statusCode; |
| 146 | + |
| 147 | + public ExecProcess() throws IOException { |
| 148 | + this.pipeIn = new PipedInputStream(); |
| 149 | + this.pipeOut = new PipedOutputStream(); |
| 150 | + this.pipeErr = new PipedOutputStream(); |
| 151 | + this.input = new PipedInputStream(pipeOut); |
| 152 | + this.output = new PipedOutputStream(pipeIn); |
| 153 | + this.err = new PipedInputStream(pipeErr); |
| 154 | + this.statusCode = -1; |
| 155 | + } |
| 156 | + |
| 157 | + public OutputStream getOutputStream() { |
| 158 | + return output; |
| 159 | + } |
| 160 | + |
| 161 | + public InputStream getInputStream() { |
| 162 | + return input; |
| 163 | + } |
| 164 | + |
| 165 | + public InputStream getErrorStream() { |
| 166 | + return err; |
| 167 | + } |
| 168 | + |
| 169 | + public int waitFor() throws InterruptedException { |
| 170 | + synchronized(this) { |
| 171 | + this.wait(); |
| 172 | + } |
| 173 | + return statusCode; |
| 174 | + } |
| 175 | + |
| 176 | + public int exitValue() { |
| 177 | + return statusCode; |
| 178 | + } |
| 179 | + |
| 180 | + public void destroy() { |
| 181 | + // TODO implementation here |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public void open(String protocol, Closeable closer) {} |
| 186 | + |
| 187 | + private OutputStream findOutputStream(int val) { |
| 188 | + if (val == 1) { |
| 189 | + return pipeOut; |
| 190 | + } |
| 191 | + if (val == 2) { |
| 192 | + return pipeErr; |
| 193 | + } |
| 194 | + System.err.println("Unknown stream: " + val); |
| 195 | + return pipeOut; |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public void bytesMessage(InputStream in) { |
| 200 | + try { |
| 201 | + int val = in.read(); |
| 202 | + OutputStream out = findOutputStream(val); |
| 203 | + |
| 204 | + byte[] buffer = new byte[4096]; |
| 205 | + for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { |
| 206 | + out.write(buffer, 0, read); |
| 207 | + } |
| 208 | + out.flush(); |
| 209 | + } catch (IOException ex) { |
| 210 | + // TODO use a logger here. |
| 211 | + ex.printStackTrace(); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public void textMessage(Reader in) { |
| 217 | + try { |
| 218 | + int val = in.read(); |
| 219 | + OutputStream out = findOutputStream(val); |
| 220 | + // TODO: there has to be a better way to do this... |
| 221 | + char[] buffer = new char[4096]; |
| 222 | + for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { |
| 223 | + String data = new String(buffer, 0, read); |
| 224 | + out.write(data.getBytes("UTF-8")); |
| 225 | + } |
| 226 | + out.flush(); |
| 227 | + } catch (IOException ex) { |
| 228 | + // TODO use a logger here |
| 229 | + ex.printStackTrace(); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public void close() { |
| 235 | + try { |
| 236 | + pipeIn.close(); |
| 237 | + pipeOut.close(); |
| 238 | + output.close(); |
| 239 | + } catch (IOException ex) { |
| 240 | + // TODO use a logger here |
| 241 | + ex.printStackTrace(); |
| 242 | + } |
| 243 | + // TODO: get status code here |
| 244 | + synchronized(this) { |
| 245 | + this.notifyAll(); |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments