Skip to content

Commit 72de537

Browse files
committed
testing
1 parent 878726a commit 72de537

File tree

1 file changed

+235
-0
lines changed

1 file changed

+235
-0
lines changed
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
/*
2+
Copyright 2017 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 io.kubernetes.client.models.V1Pod;
16+
import io.kubernetes.client.util.WebSocketStreamHandler;
17+
import io.kubernetes.client.util.WebSockets;
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.io.OutputStream;
21+
import java.util.concurrent.TimeUnit;
22+
23+
import org.apache.commons.lang3.StringUtils;
24+
25+
public class Exec {
26+
private ApiClient apiClient;
27+
28+
/** Simple Exec API constructor, uses default configuration */
29+
public Exec() {
30+
this(Configuration.getDefaultApiClient());
31+
}
32+
33+
/**
34+
* Exec API Constructor
35+
*
36+
* @param apiClient The api client to use.
37+
*/
38+
public Exec(ApiClient apiClient) {
39+
this.apiClient = apiClient;
40+
}
41+
42+
/**
43+
* Get the API client for these exec operations.
44+
*
45+
* @return The API client that will be used.
46+
*/
47+
public ApiClient getApiClient() {
48+
return apiClient;
49+
}
50+
51+
/**
52+
* Set the API client for subsequent exec operations.
53+
*
54+
* @param apiClient The new API client to use.
55+
*/
56+
public void setApiClient(ApiClient apiClient) {
57+
this.apiClient = apiClient;
58+
}
59+
60+
private String makePath(
61+
String namespace,
62+
String name,
63+
String[] command,
64+
String container,
65+
boolean stdin,
66+
boolean tty) {
67+
String path =
68+
"/api/v1/namespaces/"
69+
+ namespace
70+
+ "/pods/"
71+
+ name
72+
+ "/exec?"
73+
+ "stdin="
74+
+ stdin
75+
+ "&tty="
76+
+ tty
77+
+ (container != null ? "&container=" + container : "")
78+
+ "&command="
79+
+ StringUtils.join(command, "&command=");
80+
return path;
81+
}
82+
83+
/**
84+
* Execute a command in a container. If there are multiple containers in the pod, uses the first
85+
* container in the Pod.
86+
*
87+
* @param namespace The namespace of the Pod
88+
* @param name The name of the Pod
89+
* @param command The command to run
90+
* @param stdin If true, pass a stdin stream into the container
91+
*/
92+
public Process exec(String namespace, String name, String[] command, boolean stdin)
93+
throws ApiException, IOException {
94+
return exec(namespace, name, command, null, stdin, false);
95+
}
96+
97+
/**
98+
* Execute a command in a container. If there are multiple containers in the pod, uses the first
99+
* container in the Pod.
100+
*
101+
* @param pod The pod where the command is run.
102+
* @param command The command to run
103+
* @param stdin If true, pass a stdin stream into the container
104+
*/
105+
public Process exec(V1Pod pod, String[] command, boolean stdin) throws ApiException, IOException {
106+
return exec(pod, command, null, stdin, false);
107+
}
108+
109+
/**
110+
* Execute a command in a container. If there are multiple containers in the pod, uses the first
111+
* container in the Pod.
112+
*
113+
* @param namespace The namespace of the Pod
114+
* @param name The name of the Pod
115+
* @param command The command to run
116+
* @param stdin If true, pass a stdin stream into the container
117+
* @param tty If true, stdin is a tty.
118+
*/
119+
public Process exec(String namespace, String name, String[] command, boolean stdin, boolean tty)
120+
throws ApiException, IOException {
121+
return exec(namespace, name, command, null, stdin, tty);
122+
}
123+
124+
/**
125+
* Execute a command in a container. If there are multiple containers in the pod, uses the first
126+
* container in the Pod.
127+
*
128+
* @param pod The pod where the command is run.
129+
* @param command The command to run
130+
* @param stdin If true, pass a stdin stream into the container
131+
* @param tty If true, stdin is a tty.
132+
*/
133+
public Process exec(V1Pod pod, String[] command, boolean stdin, boolean tty)
134+
throws ApiException, IOException {
135+
return exec(pod, command, null, stdin, tty);
136+
}
137+
138+
/**
139+
* Execute a command in a container. If there are multiple containers in the pod, uses the first
140+
* container in the Pod.
141+
*
142+
* @param pod The pod where the command is run.
143+
* @param command The command to run
144+
* @param container The container in the Pod where the command is run.
145+
* @param stdin If true, pass a stdin stream into the container.
146+
* @param tty If true, stdin is a TTY (only applies if stdin is true)
147+
*/
148+
public Process exec(V1Pod pod, String[] command, String container, boolean stdin, boolean tty)
149+
throws ApiException, IOException {
150+
return exec(
151+
pod.getMetadata().getNamespace(),
152+
pod.getMetadata().getName(),
153+
command,
154+
container,
155+
stdin,
156+
tty);
157+
}
158+
159+
/**
160+
* Execute a command in a container. If there are multiple containers in the pod, uses the first
161+
* container in the Pod.
162+
*
163+
* @param namespace The namespace of the Pod
164+
* @param name The name of the Pod
165+
* @param command The command to run
166+
* @param container The container in the Pod where the command is run.
167+
* @param stdin If true, pass a stdin stream into the container.
168+
* @param tty If true, stdin is a TTY (only applies if stdin is true)
169+
*/
170+
public Process exec(
171+
String namespace, String name, String[] command, String container, boolean stdin, boolean tty)
172+
throws ApiException, IOException {
173+
String path = makePath(namespace, name, command, container, stdin, tty);
174+
175+
WebSocketStreamHandler handler = new WebSocketStreamHandler();
176+
ExecProcess exec = new ExecProcess(handler);
177+
WebSockets.stream(path, "GET", apiClient, handler);
178+
179+
return exec;
180+
}
181+
182+
private static class ExecProcess extends Process {
183+
WebSocketStreamHandler streamHandler;
184+
private volatile int statusCode;
185+
186+
public ExecProcess(WebSocketStreamHandler handler) throws IOException {
187+
this.streamHandler = handler;
188+
this.statusCode = -1;
189+
}
190+
191+
@Override
192+
public OutputStream getOutputStream() {
193+
return streamHandler.getOutputStream(0);
194+
}
195+
196+
@Override
197+
public InputStream getInputStream() {
198+
return streamHandler.getInputStream(1);
199+
}
200+
201+
@Override
202+
public InputStream getErrorStream() {
203+
return streamHandler.getInputStream(2);
204+
}
205+
206+
@Override
207+
public int waitFor() throws InterruptedException {
208+
synchronized (this) {
209+
this.wait();
210+
}
211+
return statusCode;
212+
}
213+
214+
@Override
215+
public boolean waitFor(long timeout, TimeUnit unit)
216+
throws InterruptedException {
217+
synchronized (this) {
218+
this.wait(TimeUnit.MILLISECONDS.convert(timeout, unit));
219+
}
220+
return !isAlive();
221+
}
222+
223+
@Override
224+
public int exitValue() {
225+
if (statusCode == -1)
226+
throw new IllegalThreadStateException();
227+
return statusCode;
228+
}
229+
230+
@Override
231+
public void destroy() {
232+
streamHandler.close();
233+
}
234+
}
235+
}

0 commit comments

Comments
 (0)