Skip to content
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

Add support for terminal size when executing command inside a container #983

Merged
merged 11 commits into from Nov 13, 2022

Conversation

armandpicard
Copy link
Contributor

Motivation

Adding support for terminal size when executing command in a container allow to reproduce "kubectl exec -it example -- sh" which can be needed when creating developers tools for example.

Solution

  • added the TerminalSize type and use the channel 4 to send terminal size.
  • changed the select in start_message_loop to select on more than 2 channels.
  • added the pod_exec_terminal_size example that reproduce "kubectl exec -it example -- sh", using complex terminal application like htop work fine in this example.

kazk
kazk previously requested changes Aug 12, 2022
Copy link
Member

@kazk kazk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign off your commits for DCO, and remove unused imports.

examples/pod_exec_terminal_size.rs Outdated Show resolved Hide resolved
examples/pod_exec_terminal_size.rs Outdated Show resolved Hide resolved
kube-client/src/api/remote_command.rs Show resolved Hide resolved
Signed-off-by: armandpicard <armandpicard71@gmail.com>
Signed-off-by: armandpicard <armandpicard71@gmail.com>
pod_exec_terminal size example

Signed-off-by: armandpicard <armandpicard71@gmail.com>
Signed-off-by: armandpicard <armandpicard71@gmail.com>
@kazk kazk self-requested a review September 7, 2022 04:51
@kazk kazk requested a review from a team November 1, 2022 02:53
@clux clux added the changelog-add changelog added category for prs label Nov 1, 2022
@clux clux modified the milestone: 0.77.0 Nov 1, 2022
Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks great. The example is very readable. The refactor is also nice despite the heavy change of the remote_command loop, and am slightly confused about some details there. It's the kind of change that makes me wish we have better tests for some of the nitty-gritty of the ws handling, but I am ultimately pro this change. The way it plugs into crossterm is really nice.

kube-client/src/api/remote_command.rs Show resolved Hide resolved
kube-client/src/api/remote_command.rs Show resolved Hide resolved
kube-client/src/api/remote_command.rs Outdated Show resolved Hide resolved
examples/pod_exec_terminal_size.rs Outdated Show resolved Hide resolved
examples/pod_exec_terminal_size.rs Outdated Show resolved Hide resolved
@clux
Copy link
Member

clux commented Nov 1, 2022

Also, sorry, I did not get to this sooner. Had missed it entirely :(

@clux clux linked an issue Nov 1, 2022 that may be closed by this pull request
Signed-off-by: armandpicard <armandpicard71@gmail.com>
armandpicard and others added 2 commits November 1, 2022 22:39
Co-authored-by: kazk <kazk.dev@gmail.com>
Signed-off-by: armand picard <33733022+armandpicard@users.noreply.github.com>
Signed-off-by: armandpicard <armandpicard71@gmail.com>
@clux
Copy link
Member

clux commented Nov 4, 2022

I played around a bit with this now, and it does seem to work, but it's an incredibly frustrating example atm because every 3rd keystroke is ignored if it happens at a certain time. not quite sure what is causing it, but something in the example is not plumbed up correctly.

but crucially it does a much better job than the pod_shell example at being user friendly (backspace, up arrow, etc all do the right thing now) 👍

@@ -0,0 +1,134 @@
use futures::{channel::mpsc::Sender, SinkExt, StreamExt};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should rename this to pod_shell_crossterm to match the other example and highlight functionality

@armandpicard
Copy link
Contributor Author

The keystroke issue is related to crossterm. It looks likes crossterm is also reading stdin and get the keystroke and send it on the EventStream, that why we don't get it in stdin. I'll try to dig more into it.

Signed-off-by: armandpicard <armandpicard71@gmail.com>
@armandpicard
Copy link
Contributor Author

I can't find any way to avoid the EventStream from reading on stdin so I use just use signal stream on SIGWINCH. This work on unix I don't know if I should also make it work in Windows ?

@clux
Copy link
Member

clux commented Nov 11, 2022

Thanks a lot, that feels a lot more usable now!

[..] SIGWINCH. This work on unix I don't know if I should also make it work in Windows ?

I think SIGWINCH is the right thing for us to do; minimal and keeps the code focused on what WE do, plus it serves the majority of users on mac/linux.

Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am happy to merge this as is, but will want to rename the example to pod_shell_crossterm unless you have objections to that.

Signed-off-by: armandpicard <armandpicard71@gmail.com>
@clux
Copy link
Member

clux commented Nov 12, 2022

Thanks! I'm going to update the branch for CI and start the job so it's all good to go before merging.

@codecov-commenter
Copy link

codecov-commenter commented Nov 12, 2022

Codecov Report

Merging #983 (ea0d2ec) into main (f6ca7df) will increase coverage by 0.13%.
The diff coverage is 44.89%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #983      +/-   ##
==========================================
+ Coverage   72.34%   72.48%   +0.13%     
==========================================
  Files          65       65              
  Lines        4715     4757      +42     
==========================================
+ Hits         3411     3448      +37     
- Misses       1304     1309       +5     
Impacted Files Coverage Δ
kube-client/src/api/mod.rs 68.08% <ø> (ø)
kube-client/src/api/remote_command.rs 60.00% <44.89%> (-7.75%) ⬇️
kube-runtime/src/reflector/mod.rs 100.00% <0.00%> (ø)
kube-runtime/src/utils/stream_backoff.rs 87.05% <0.00%> (ø)
kube-client/src/lib.rs 93.79% <0.00%> (+0.04%) ⬆️
kube-runtime/src/scheduler.rs 96.81% <0.00%> (+0.08%) ⬆️
kube-runtime/src/controller/runner.rs 95.31% <0.00%> (+0.31%) ⬆️
kube-client/src/client/middleware/mod.rs 93.54% <0.00%> (+0.44%) ⬆️
kube-runtime/src/controller/mod.rs 37.83% <0.00%> (+0.56%) ⬆️
kube/src/lib.rs 88.57% <0.00%> (+0.71%) ⬆️
... and 5 more

@clux
Copy link
Member

clux commented Nov 12, 2022

Ah, ok so two issues:

  • rustfmt (needs the nightly rust and run just fmt)
  • the example fails to build on windows so just wrap most of the example in an if_cfg linux type thing (can also be done in the example's Cargo.toml to disallow the example on windows i think)
  • ignore the cargo deny failure (we will fix that separately, and it's not caused by this PR)

Just force push over my change if necessary.

…+ run fmt

Signed-off-by: armandpicard <armandpicard71@gmail.com>
@armandpicard
Copy link
Contributor Author

I've been kind with Windows user, I've made the example work on Windows but without handling the terminal size change, I just send the size when the program start.

Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yeah, that is nicer. partial windows support at least!

@clux clux merged commit d574fbc into kube-rs:main Nov 13, 2022
@clux clux added this to the 0.77.0 milestone Nov 13, 2022
@clux
Copy link
Member

clux commented Nov 13, 2022

Thank you so much!

aviramha pushed a commit to metalbear-co/kube that referenced this pull request Nov 19, 2022
…er (kube-rs#983)

* Add support for terminal size when executing command inside a container

Signed-off-by: armandpicard <armandpicard71@gmail.com>

* remove unused import + fix example

Signed-off-by: armandpicard <armandpicard71@gmail.com>

* Use crossterm::event::EventStream to get terminal change in
pod_exec_terminal size example

Signed-off-by: armandpicard <armandpicard71@gmail.com>

* Fix error when terminal_resize_tx is None

Signed-off-by: armandpicard <armandpicard71@gmail.com>

* Add some comments

Signed-off-by: armandpicard <armandpicard71@gmail.com>

* Fix typos

Co-authored-by: kazk <kazk.dev@gmail.com>
Signed-off-by: armand picard <33733022+armandpicard@users.noreply.github.com>

* Fix: uniformize message send

Signed-off-by: armandpicard <armandpicard71@gmail.com>

* Remove crossterm event stream

Signed-off-by: armandpicard <armandpicard71@gmail.com>

* Rename example pod_shell_crossterm

Signed-off-by: armandpicard <armandpicard71@gmail.com>

* Make example run on window but without handling terminal size change + run fmt

Signed-off-by: armandpicard <armandpicard71@gmail.com>

Signed-off-by: armandpicard <armandpicard71@gmail.com>
Signed-off-by: armand picard <33733022+armandpicard@users.noreply.github.com>
Co-authored-by: kazk <kazk.dev@gmail.com>
Co-authored-by: Eirik A <sszynrae@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-add changelog added category for prs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

exec sh (shell) with tty not processing escape chars
4 participants