Skip to content

Commit e34487b

Browse files
committed
Attach: Add builder-style mechanism to configure attachment
1 parent a4384e8 commit e34487b

File tree

2 files changed

+128
-17
lines changed

2 files changed

+128
-17
lines changed

util/src/main/java/io/kubernetes/client/Attach.java

Lines changed: 107 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,14 @@ public void setApiClient(ApiClient apiClient) {
5757
this.apiClient = apiClient;
5858
}
5959

60-
private String makePath(
61-
String namespace, String name, String container, boolean stdin, boolean tty) {
62-
return "/api/v1/namespaces/"
63-
+ namespace
64-
+ "/pods/"
65-
+ name
66-
+ "/attach?"
67-
+ "stdin="
68-
+ stdin
69-
+ "&tty="
70-
+ tty
71-
+ (container != null ? "&container=" + container : "");
60+
/**
61+
* Setup a Builder for the given namespace and name
62+
*
63+
* @param namespace The namespace of the Pod
64+
* @param name The name of the Pod
65+
*/
66+
public ConnectionBuilder newConnectionBuilder(String namespace, String name) {
67+
return new ConnectionBuilder(namespace, name);
7268
}
7369

7470
/**
@@ -136,13 +132,107 @@ public AttachResult attach(V1Pod pod, String container, boolean stdin, boolean t
136132
public AttachResult attach(
137133
String namespace, String name, String container, boolean stdin, boolean tty)
138134
throws ApiException, IOException {
139-
String path = makePath(namespace, name, container, stdin, tty);
135+
return newConnectionBuilder(namespace, name)
136+
.setContainer(container)
137+
.setStdin(stdin)
138+
.setTty(tty)
139+
.connect();
140+
}
141+
142+
public final class ConnectionBuilder {
143+
private final String namespace;
144+
private final String name;
145+
146+
private String container;
147+
148+
private boolean stdin;
149+
private boolean stdout;
150+
private boolean stderr;
151+
private boolean tty;
152+
153+
private ConnectionBuilder(String namespace, String name) {
154+
this.namespace = namespace;
155+
this.name = name;
156+
this.stdin = true;
157+
}
158+
159+
public String getName() {
160+
return name;
161+
}
162+
163+
public String getNamespace() {
164+
return namespace;
165+
}
166+
167+
public String getContainer() {
168+
return container;
169+
}
170+
171+
public ConnectionBuilder setContainer(String container) {
172+
this.container = container;
173+
return this;
174+
}
140175

141-
WebSocketStreamHandler handler = new WebSocketStreamHandler();
142-
AttachResult result = new AttachResult(handler);
143-
WebSockets.stream(path, "GET", apiClient, handler);
176+
public boolean getStdin() {
177+
return stdin;
178+
}
179+
180+
public ConnectionBuilder setStdin(boolean stdin) {
181+
this.stdin = stdin;
182+
return this;
183+
}
184+
185+
public boolean getStdout() {
186+
return stdout;
187+
}
144188

145-
return result;
189+
public ConnectionBuilder setStdout(boolean stdout) {
190+
this.stdout = stdout;
191+
return this;
192+
}
193+
194+
public boolean getStderr() {
195+
return stderr;
196+
}
197+
198+
public ConnectionBuilder setStderr(boolean stderr) {
199+
this.stderr = stderr;
200+
return this;
201+
}
202+
203+
public boolean getTty() {
204+
return tty;
205+
}
206+
207+
public ConnectionBuilder setTty(boolean tty) {
208+
this.tty = tty;
209+
return this;
210+
}
211+
212+
private String makePath() {
213+
return "/api/v1/namespaces/"
214+
+ namespace
215+
+ "/pods/"
216+
+ name
217+
+ "/attach?"
218+
+ "stdin="
219+
+ stdin
220+
+ "&stdout="
221+
+ stdout
222+
+ "&stderr="
223+
+ stderr
224+
+ "&tty="
225+
+ tty
226+
+ (container != null ? "&container=" + container : "");
227+
}
228+
229+
public AttachResult connect() throws ApiException, IOException {
230+
WebSocketStreamHandler handler = new WebSocketStreamHandler();
231+
AttachResult result = new AttachResult(handler);
232+
WebSockets.stream(makePath(), "GET", apiClient, handler);
233+
234+
return result;
235+
}
146236
}
147237

148238
/**

util/src/test/java/io/kubernetes/client/AttachTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,46 @@ public void testUrl() throws IOException, ApiException, InterruptedException {
6464

6565
AttachResult res1 = attach.attach(namespace, podName, container, false, false);
6666
AttachResult res2 = attach.attach(namespace, podName, true);
67+
AttachResult res3 =
68+
attach
69+
.newConnectionBuilder(namespace, podName)
70+
.setContainer(container)
71+
.setStdin(false)
72+
.setStdout(true)
73+
.connect();
6774

6875
// TODO: Kill this sleep, the trouble is that the test tries to validate before the connection
6976
// event has happened
7077
Thread.sleep(2000);
7178

7279
res1.close();
7380
res2.close();
81+
res3.close();
7482

7583
verify(
7684
getRequestedFor(
7785
urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/attach"))
7886
.withQueryParam("stdin", equalTo("false"))
87+
.withQueryParam("stdout", equalTo("false"))
88+
.withQueryParam("stderr", equalTo("false"))
7989
.withQueryParam("tty", equalTo("false"))
8090
.withQueryParam("container", equalTo(container)));
8191

8292
verify(
8393
getRequestedFor(
8494
urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/attach"))
8595
.withQueryParam("stdin", equalTo("true"))
96+
.withQueryParam("stdout", equalTo("false"))
97+
.withQueryParam("stderr", equalTo("false"))
8698
.withQueryParam("tty", equalTo("false")));
99+
100+
verify(
101+
getRequestedFor(
102+
urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/attach"))
103+
.withQueryParam("stdin", equalTo("false"))
104+
.withQueryParam("stdout", equalTo("true"))
105+
.withQueryParam("stderr", equalTo("false"))
106+
.withQueryParam("tty", equalTo("false"))
107+
.withQueryParam("container", equalTo(container)));
87108
}
88109
}

0 commit comments

Comments
 (0)