Skip to content

Commit 63fc77b

Browse files
authored
Merge pull request #3801 from kubernetes-client/terminalSize
Add support for terminal resize in ExecProcess
2 parents 1763d26 + e55215c commit 63fc77b

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,13 @@ public OutputStream getResizeStream() {
611611
return streamHandler.getOutputStream(4);
612612
}
613613

614+
public void resize(int width, int height) throws IOException {
615+
OutputStream resizeStream = getResizeStream();
616+
String resize = "{ \"width\": " + width + ", \"height\": " + height + " }\n";
617+
resizeStream.write(resize.getBytes("UTF-8"));
618+
resizeStream.flush();
619+
}
620+
614621
private synchronized InputStream getInputStream(int stream) {
615622
if (!input.containsKey(stream)) {
616623
input.put(stream, streamHandler.getInputStream(stream));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ private synchronized OutputStream getSocketInputOutputStream(int stream) {
218218
return pipedOutput.get(stream);
219219
}
220220

221+
// Only used for testing, has to be public because ExecTest is in a different package :(
222+
public void injectOutputStream(int streamNum, OutputStream stream) {
223+
output.put(streamNum, stream);
224+
}
225+
221226
public boolean supportsClose() {
222227
return this.protocol.equals("v5.channel.k8s.io");
223228
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,25 @@ void execProcess() throws IOException, InterruptedException {
139139
assertThat(process.exitValue()).isZero();
140140
}
141141

142+
@Test
143+
void terminalResize() throws IOException, InterruptedException {
144+
final Throwable throwable = mock(Throwable.class);
145+
final ExecProcess process = new ExecProcess(client);
146+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
147+
148+
System.out.println("Injecting output stream");
149+
process.getHandler().injectOutputStream(4, bos);
150+
System.out.println("Resizing output stream");
151+
process.resize(100, 100);
152+
System.out.println("Resizing output stream");
153+
process.destroy();
154+
155+
System.out.println("Going to tests.");
156+
157+
String out = bos.toString("UTF-8");
158+
assertThat(out).isEqualTo("{ \"width\": 100, \"height\": 100 }\n");
159+
}
160+
142161
@Test
143162
void defaultUnhandledError() throws IOException, InterruptedException {
144163
final Throwable throwable = mock(Throwable.class);

0 commit comments

Comments
 (0)