Skip to content

Add support for terminal resize in ExecProcess #3801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -218,6 +218,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);
}

public boolean supportsClose() {
return this.protocol.equals("v5.channel.k8s.io");
}
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
Loading