kubernetes-client / java

Official Java client library for kubernetes
http://kubernetes.io/
Apache License 2.0
3.57k stars 1.9k forks source link

How to set the terminal stty cols and rows through exec API ? #3641

Closed gohonsen closed 2 months ago

gohonsen commented 2 months ago

Describe the bug Hey, I want to enter the k8s pod container to run shell commands through web terminal, but I found that after the web terminal page size changed, I cann't synchronize set terminal size through exec API, and the page display is disordered. Is there any way to set the terminal stty cols and rows ? Thanks a lot

Client Version 18.0.1

Kubernetes Version v1.28.0

Java Version Java 8

brendandburns commented 2 months ago

This isn't currently supported. You can read resize messages from the stream via Exec.getResizeStream() but you can't write to that stream currently.

We need something like the following in the Javascript client:

https://github.com/kubernetes-client/javascript/blob/70f651ca7e5c8d3647c071ab4f6aeb89f4ccae14/src/exec.ts#L66

We'd be happy to take a PR for this support.

brendandburns commented 2 months ago

Alternately, you could use ExecProcess.getInputStream(4).write("{ 'width': foo, 'height': bar}") that should work.

https://github.com/kubernetes-client/java/blob/0e94811dbf8e3a0bfebb0c8a8cc9bf61a28e5e83/util/src/main/java/io/kubernetes/client/Exec.java#L610

gohonsen commented 2 months ago

@brendandburns I got it, the codes like this:

Exec.ExecutionBuilder executionBuilder = k8sExec.newExecutionBuilder(namespace, podName, new String[]{shellPath}); executionBuilder.setTty(tty); executionBuilder.setContainer(containerName); execProcess = (Exec.ExecProcess) executionBuilder.execute(); resizeOutStream = execProcess.getResizeStream(); outputStream = execProcess.getOutputStream(); inputStream = execProcess.getInputStream(); String resize = "{\"Width\":" + cols + ",\"Height\":" + rows + "}"; resizeOutStream.write(resize.getBytes());

and it works, thanks.

brendandburns commented 2 months ago

Glad it's working. Making this a little easier could be a good first issue, so I'll leave this open in case someone wants to take it on.

sriganeshres commented 2 months ago

@brendandburns i want to work on this issue. But the problem i cant setup the code properly can you send me the steps to setup the code.

sriganeshres commented 2 months ago

image i have done this till now.

thanks

sriganeshres commented 2 months ago

then i did this image next how should i proceed to do the required changes and test and contribute. It is some what tricky to a begginer to this code base. can you kindly please help me.

brendandburns commented 2 months ago

@sriganeshres the second command is to regenerate the code, you don't need to do that.

What you need to do is edit this file:

https://github.com/kubernetes-client/java/blob/65ade9471b18126e768475e510c288338f3d3d18/util/src/main/java/io/kubernetes/client/Exec.java#L4

To add a function like setTerminalSize(int widthColumns, int heightColumns) and which does code similar to https://github.com/kubernetes-client/java/issues/3641#issuecomment-2301637328.

If you need help with git or making a PR, please see instructions here: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request?tool=cli

sriganeshres commented 2 months ago

thank you @brendandburns

sriganeshres commented 2 months ago

@brendandburns I have added a PR #3657 for this. Thanks for the help and understanding.

brendandburns commented 2 months ago

@sriganeshres thank you. In the future, you can just push new commits to the same branch and this PR will update, you don't need to create a new PR. I will close this PR given the new one.