From 4b611d7934f98d5415926fa654c7bcde392f4e3b Mon Sep 17 00:00:00 2001 From: Brendan Burns <5751682+brendandburns@users.noreply.github.com> Date: Wed, 27 Nov 2024 16:19:38 +0000 Subject: [PATCH] Add support for terminal resize in ExecProcess --- .../main/java/io/kubernetes/client/Exec.java | 7 +++++++ .../client/util/WebSocketStreamHandler.java | 5 +++++ .../java/io/kubernetes/client/ExecTest.java | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/util/src/main/java/io/kubernetes/client/Exec.java b/util/src/main/java/io/kubernetes/client/Exec.java index 1a94a31593..0e4e7618d9 100644 --- a/util/src/main/java/io/kubernetes/client/Exec.java +++ b/util/src/main/java/io/kubernetes/client/Exec.java @@ -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)); diff --git a/util/src/main/java/io/kubernetes/client/util/WebSocketStreamHandler.java b/util/src/main/java/io/kubernetes/client/util/WebSocketStreamHandler.java index e2fb2d6c07..6399b80caa 100644 --- a/util/src/main/java/io/kubernetes/client/util/WebSocketStreamHandler.java +++ b/util/src/main/java/io/kubernetes/client/util/WebSocketStreamHandler.java @@ -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; diff --git a/util/src/test/java/io/kubernetes/client/ExecTest.java b/util/src/test/java/io/kubernetes/client/ExecTest.java index 786de57d5d..bc905c076a 100644 --- a/util/src/test/java/io/kubernetes/client/ExecTest.java +++ b/util/src/test/java/io/kubernetes/client/ExecTest.java @@ -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);