Skip to content

Commit

Permalink
Add support for terminal resize in ExecProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Dec 5, 2024
1 parent 858e905 commit 4b611d7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions util/src/main/java/io/kubernetes/client/Exec.java
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,13 @@ public OutputStream getResizeStream() {
return streamHandler.getOutputStream(4);
}

public void resize(int width, int height) throws IOException {
OutputStream resizeStream = getResizeStream();
String resize = "{ \"width\": " + width + ", \"height\": " + height + " }\n";
resizeStream.write(resize.getBytes("UTF-8"));
resizeStream.flush();
}

private synchronized InputStream getInputStream(int stream) {
if (!input.containsKey(stream)) {
input.put(stream, streamHandler.getInputStream(stream));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ private synchronized OutputStream getSocketInputOutputStream(int stream) {
return pipedOutput.get(stream);
}

// Only used for testing, has to be public because ExecTest is in a different package :(
public void injectOutputStream(int streamNum, OutputStream stream) {
output.put(streamNum, stream);
}

private class WebSocketOutputStream extends OutputStream {

private static final long MAX_QUEUE_SIZE = 16L * 1024 * 1024;
Expand Down
19 changes: 19 additions & 0 deletions util/src/test/java/io/kubernetes/client/ExecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ void execProcess() throws IOException, InterruptedException {
assertThat(process.exitValue()).isZero();
}

@Test
void terminalResize() throws IOException, InterruptedException {
final Throwable throwable = mock(Throwable.class);
final ExecProcess process = new ExecProcess(client);
ByteArrayOutputStream bos = new ByteArrayOutputStream();

System.out.println("Injecting output stream");
process.getHandler().injectOutputStream(4, bos);
System.out.println("Resizing output stream");
process.resize(100, 100);
System.out.println("Resizing output stream");
process.destroy();

System.out.println("Going to tests.");

String out = bos.toString("UTF-8");
assertThat(out).isEqualTo("{ \"width\": 100, \"height\": 100 }\n");
}

@Test
void defaultUnhandledError() throws IOException, InterruptedException {
final Throwable throwable = mock(Throwable.class);
Expand Down

0 comments on commit 4b611d7

Please sign in to comment.