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 Nov 27, 2024
1 parent ee15c48 commit 800b9d3
Show file tree
Hide file tree
Showing 3 changed files with 25 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
13 changes: 13 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,19 @@ 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();

process.getHandler().injectOutputStream(3, bos);
process.resize(100, 100);

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

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

0 comments on commit 800b9d3

Please sign in to comment.