-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Exec.java
617 lines (556 loc) · 19.9 KB
/
Exec.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client;
import static io.kubernetes.client.KubernetesConstants.*;
import com.google.common.io.CharStreams;
import com.google.gson.reflect.TypeToken;
import io.kubernetes.client.custom.IOTrio;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1Status;
import io.kubernetes.client.openapi.models.V1StatusCause;
import io.kubernetes.client.openapi.models.V1StatusDetails;
import io.kubernetes.client.util.WebSocketStreamHandler;
import io.kubernetes.client.util.WebSockets;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Exec {
private static final Logger log = LoggerFactory.getLogger(Exec.class);
private ApiClient apiClient;
/** Simple Exec API constructor, uses default configuration */
public Exec() {
this(Configuration.getDefaultApiClient());
}
/**
* Exec API Constructor
*
* @param apiClient The api client to use.
*/
public Exec(ApiClient apiClient) {
this.apiClient = apiClient;
}
/**
* Get the API client for these exec operations.
*
* @return The API client that will be used.
*/
public ApiClient getApiClient() {
return apiClient;
}
/**
* Set the API client for subsequent exec operations.
*
* @param apiClient The new API client to use.
*/
public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}
/**
* Setup a Builder for the given namespace, name and command
*
* @param namespace The namespace of the Pod
* @param name The name of the Pod
* @param command The command to run
*/
public ExecutionBuilder newExecutionBuilder(String namespace, String name, String[] command) {
return new ExecutionBuilder(namespace, name, command);
}
/**
* Execute a command in a container. If there are multiple containers in the pod, uses the first
* container in the Pod.
*
* @param namespace The namespace of the Pod
* @param name The name of the Pod
* @param command The command to run
* @param stdin If true, pass a stdin stream into the container
*/
public Process exec(String namespace, String name, String[] command, boolean stdin)
throws ApiException, IOException {
return exec(namespace, name, command, null, stdin, false);
}
/**
* Execute a command in a container. If there are multiple containers in the pod, uses the first
* container in the Pod.
*
* @param pod The pod where the command is run.
* @param command The command to run
* @param stdin If true, pass a stdin stream into the container
*/
public Process exec(V1Pod pod, String[] command, boolean stdin) throws ApiException, IOException {
return exec(pod, command, pod.getSpec().getContainers().get(0).getName(), stdin, false);
}
/**
* Execute a command in a container. If there are multiple containers in the pod, uses the first
* container in the Pod.
*
* @param namespace The namespace of the Pod
* @param name The name of the Pod
* @param command The command to run
* @param stdin If true, pass a stdin stream into the container
* @param tty If true, stdin is a tty.
*/
public Process exec(String namespace, String name, String[] command, boolean stdin, boolean tty)
throws ApiException, IOException {
return exec(namespace, name, command, null, stdin, tty);
}
/**
* Execute a command in a container. If there are multiple containers in the pod, uses the first
* container in the Pod.
*
* @param pod The pod where the command is run.
* @param command The command to run
* @param stdin If true, pass a stdin stream into the container
* @param tty If true, stdin is a tty.
*/
public Process exec(V1Pod pod, String[] command, boolean stdin, boolean tty)
throws ApiException, IOException {
return exec(pod, command, pod.getSpec().getContainers().get(0).getName(), stdin, tty);
}
/**
* Execute a command in a container. If there are multiple containers in the pod, uses the first
* container in the Pod.
*
* @param pod The pod where the command is run.
* @param command The command to run
* @param container The container in the Pod where the command is run.
* @param stdin If true, pass a stdin stream into the container.
* @param tty If true, stdin is a TTY (only applies if stdin is true)
*/
public Process exec(V1Pod pod, String[] command, String container, boolean stdin, boolean tty)
throws ApiException, IOException {
if (container == null) {
container = pod.getSpec().getContainers().get(0).getName();
}
return exec(
pod.getMetadata().getNamespace(),
pod.getMetadata().getName(),
command,
container,
stdin,
tty);
}
/**
* Execute a command in a container. If there are multiple containers in the pod, uses the first
* container in the Pod.
*
* @param namespace The namespace of the Pod
* @param name The name of the Pod
* @param command The command to run
* @param container The container in the Pod where the command is run.
* @param stdin If true, pass a stdin stream into the container.
* @param tty If true, stdin is a TTY (only applies if stdin is true)
*/
public Process exec(
String namespace, String name, String[] command, String container, boolean stdin, boolean tty)
throws ApiException, IOException {
return new ExecutionBuilder(namespace, name, command)
.setContainer(container)
.setStdin(stdin)
.setTty(tty)
.execute();
}
/**
* A convenience method. Executes a command remotely on a pod and monitors for events in that
* execution. The monitored events are: <br>
* - connection established (onOpen) <br>
* - connection closed (onClosed) <br>
* - execution error occurred (onError) <br>
* This method also allows to specify a MAX timeout for the execution and returns a future in
* order to monitor the execution flow. <br>
* onError and onClosed callbacks are invoked asynchronously, in a separate thread. <br>
*
* @param namespace a namespace the target pod "lives" in
* @param podName a name of the pod to exec the command on
* @param onOpen a callback invoked upon the connection established event.
* @param onClosed a callback invoked upon the process termination. Return code might not always
* be there. N.B. this callback is invoked before the returned {@link Future} is completed.
* @param onError a callback to handle k8s errors (NOT the command errors/stderr!)
* @param timeoutMs timeout in milliseconds for the execution. I.e. the execution will take this
* many ms or less. If the timeout command is running longer than the allowed timeout, the
* command will be "asked" to terminate gracefully. If the command is still running after the
* grace period, the sigkill will be issued. If null is passed, the timeout will not be used
* and will wait for process to exit itself.
* @param tty whether you need tty to pipe the data. TTY mode will trim some binary data in order
* to make it possible to show on screen (tty)
* @param command a tokenized command to run on the pod
* @return a {@link Future} promise representing this execution. Unless something goes south, the
* promise will contain the process return exit code. If the timeoutMs is non-null and the
* timeout expires before the process exits, promise will contain {@link Integer#MAX_VALUE}.
* @throws IOException
*/
public Future<Integer> exec(
String namespace,
String podName,
Consumer<IOTrio> onOpen,
BiConsumer<Integer, IOTrio> onClosed,
BiConsumer<Throwable, IOTrio> onError,
Long timeoutMs,
boolean tty,
String... command)
throws IOException {
CompletableFuture<Integer> future = new CompletableFuture<>();
IOTrio io = new IOTrio();
String cmdStr = Arrays.toString(command);
BiConsumer<Throwable, IOTrio> errHandler =
(err, errIO) -> {
if (onError != null) {
onError.accept(err, errIO);
}
};
try {
Process process = exec(namespace, podName, command, null, true, tty);
io.onClose(
(code, timeout) -> {
process.destroy();
waitForProcessToExit(process, timeout, cmdStr, err -> errHandler.accept(err, io));
// processWaitingThread will handle the rest
});
io.setStdin(process.getOutputStream());
io.setStdout(process.getInputStream());
io.setStderr(process.getErrorStream());
runAsync(
"Process-Waiting-Thread-" + command[0] + "-" + podName,
() -> {
Supplier<Integer> returnCode = process::exitValue;
try {
log.debug("Waiting for process to close in {} ms: {}", timeoutMs, cmdStr);
boolean beforeTimout =
waitForProcessToExit(
process, timeoutMs, cmdStr, err -> errHandler.accept(err, io));
if (!beforeTimout) {
returnCode = () -> Integer.MAX_VALUE;
}
} catch (Exception e) {
errHandler.accept(e, io);
}
log.debug("process.onExit({}): {}", returnCode.get(), cmdStr);
if (onClosed != null) {
onClosed.accept(returnCode.get(), io);
}
future.complete(returnCode.get());
});
if (onOpen != null) {
onOpen.accept(io);
}
} catch (ApiException e) {
errHandler.accept(e, io);
future.completeExceptionally(e);
}
return future;
}
protected void runAsync(String taskName, Runnable task) {
Thread thread = new Thread(task);
thread.setName(taskName);
thread.start();
}
private boolean waitForProcessToExit(
Process process, Long timeoutMs, String cmdStr, Consumer<Exception> onError) {
boolean beforeTimeout = true;
if (timeoutMs != null && timeoutMs >= 0) {
boolean exited = false;
try {
exited = process.waitFor(timeoutMs, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
onError.accept(e);
}
log.debug("Process closed={}: {}", exited, cmdStr);
if (!exited && process.isAlive()) {
beforeTimeout = false;
log.warn("Process timed out after {} ms. Shutting down: {}", timeoutMs, cmdStr);
process.destroy();
}
} else {
try {
process.waitFor();
} catch (InterruptedException e) {
onError.accept(e);
}
}
return beforeTimeout;
}
public final class ExecutionBuilder {
private final String namespace;
private final String name;
private final String[] command;
private String container;
private boolean stdin;
private boolean stdout;
private boolean stderr;
private boolean tty;
private ExecutionBuilder(String namespace, String name, String[] command) {
this.namespace = namespace;
this.name = name;
this.command = command;
this.stdin = true;
this.stdout = true;
this.stderr = true;
}
public String getName() {
return name;
}
public String getNamespace() {
return namespace;
}
public String[] getCommand() {
return command;
}
public String getContainer() {
return container;
}
public ExecutionBuilder setContainer(String container) {
this.container = container;
return this;
}
public boolean getStdin() {
return stdin;
}
public ExecutionBuilder setStdin(boolean stdin) {
this.stdin = stdin;
return this;
}
public boolean getStdout() {
return stdout;
}
public ExecutionBuilder setStdout(boolean stdout) {
this.stdout = stdout;
return this;
}
public boolean getStderr() {
return stderr;
}
public ExecutionBuilder setStderr(boolean stderr) {
this.stderr = stderr;
return this;
}
public boolean getTty() {
return tty;
}
public ExecutionBuilder setTty(boolean tty) {
this.tty = tty;
return this;
}
private String makePath() {
String[] encodedCommand = new String[command.length];
for (int i = 0; i < command.length; i++) {
try {
encodedCommand[i] = URLEncoder.encode(command[i], "UTF-8");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException("some thing wrong happend: " + ex.getMessage());
}
}
return "/api/v1/namespaces/"
+ namespace
+ "/pods/"
+ name
+ "/exec?"
+ "stdin="
+ stdin
+ "&stdout="
+ stdout
+ "&stderr="
+ stderr
+ "&tty="
+ tty
+ (container != null ? "&container=" + container : "")
+ "&command="
+ StringUtils.join(encodedCommand, "&command=");
}
public Process execute() throws ApiException, IOException {
if (container == null) {
CoreV1Api api = new CoreV1Api(apiClient);
V1Pod pod = api.readNamespacedPod(name, namespace, "false", null, null);
container = pod.getSpec().getContainers().get(0).getName();
}
ExecProcess exec = new ExecProcess(apiClient);
WebSocketStreamHandler handler = exec.getHandler();
WebSockets.stream(makePath(), "GET", apiClient, handler);
return exec;
}
}
static int parseExitCode(ApiClient client, InputStream inputStream) {
try {
Type returnType = new TypeToken<V1Status>() {}.getType();
String body;
try (final Reader reader = new InputStreamReader(inputStream)) {
body = CharStreams.toString(reader);
}
V1Status status = client.getJSON().deserialize(body, returnType);
if (status == null) {
return -1;
}
if (V1STATUS_SUCCESS.equals(status.getStatus())) return 0;
if (V1STATUS_REASON_NONZEROEXITCODE.equals(status.getReason())) {
V1StatusDetails details = status.getDetails();
if (details != null) {
List<V1StatusCause> causes = details.getCauses();
if (causes != null) {
for (V1StatusCause cause : causes) {
if (V1STATUS_CAUSE_REASON_EXITCODE.equals(cause.getReason())) {
try {
return Integer.parseInt(cause.getMessage());
} catch (NumberFormatException nfe) {
log.error("Error parsing exit code from status channel response", nfe);
}
}
}
}
}
}
} catch (Throwable t) {
log.error("Error parsing exit code from status channel response", t);
}
// Unable to parse the exit code from the content
return -1;
}
public static class ExecProcess extends Process {
private final WebSocketStreamHandler streamHandler;
private int statusCode = -1;
private boolean isAlive = true;
private final Map<Integer, InputStream> input = new HashMap<>();
private final CountDownLatch latch = new CountDownLatch(1);
public ExecProcess(final ApiClient apiClient) throws IOException {
this.streamHandler =
new WebSocketStreamHandler() {
@Override
protected void handleMessage(int stream, InputStream inStream) throws IOException {
if (stream == 3) {
int exitCode = parseExitCode(apiClient, inStream);
if (exitCode >= 0) {
// notify of process completion
synchronized (ExecProcess.this) {
statusCode = exitCode;
isAlive = false;
}
}
inStream.close();
// Stream ID of `3` delivers the status of exec connection from
// kubelet,
// closing the connection upon 0 exit-code.
this.close();
ExecProcess.this.latch.countDown();
} else super.handleMessage(stream, inStream);
}
@Override
public void failure(Throwable ex) {
super.failure(ex);
// TODO, it's possible we should suppress this error message, but
// currently there's
// no good place to surface the message, and without it, this will be
// really hard to
// debug.
ex.printStackTrace();
synchronized (ExecProcess.this) {
// Try for a pretty unique error code, so if someone searches
// they'll find this
// code.
statusCode = -1975219;
isAlive = false;
ExecProcess.this.latch.countDown();
}
}
@Override
public void close() {
// notify of process completion
synchronized (ExecProcess.this) {
if (isAlive) {
isAlive = false;
ExecProcess.this.latch.countDown();
}
}
super.close();
}
};
}
// Protected to facilitate unit testing.
protected WebSocketStreamHandler getHandler() {
return streamHandler;
}
@Override
public OutputStream getOutputStream() {
return streamHandler.getOutputStream(0);
}
@Override
public InputStream getInputStream() {
return getInputStream(1);
}
@Override
public InputStream getErrorStream() {
return getInputStream(2);
}
public InputStream getConnectionErrorStream() {
return getInputStream(3);
}
public OutputStream getResizeStream() {
return streamHandler.getOutputStream(4);
}
private synchronized InputStream getInputStream(int stream) {
if (!input.containsKey(stream)) {
input.put(stream, streamHandler.getInputStream(stream));
}
return input.get(stream);
}
@Override
public int waitFor() throws InterruptedException {
this.latch.await();
return statusCode;
}
@Override
public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException {
this.latch.await(timeout, unit);
return !isAlive();
}
@Override
public synchronized int exitValue() {
if (isAlive) throw new IllegalThreadStateException();
return statusCode;
}
@Override
public synchronized boolean isAlive() {
return isAlive;
}
@Override
public void destroy() {
streamHandler.close();
for (InputStream in : input.values()) {
try {
in.close();
} catch (IOException ex) {
log.error("Error on close", ex);
}
}
}
}
}