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

Suspend k9s using CTRL-Z #2484

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Conversation

arajski
Copy link

@arajski arajski commented Jan 17, 2024

Description

Implements unix-like suspend functionality mentioned in #2013

At the moment, k9s supports Ctrl-C, which exits the application, however it's not possible to suspend the process and send it to the background. This PR implements that functionality, which is common among other tools, like vim.
User can now send the process to the background by pressing Ctrl-Z.
Once suspended, it can be resumed by either using fg or selecting a specific id from the jobs list (for example %1) in case there are multiple ones.

This behaviour can be enabled by setting allowSuspend to true.

Changes

  • remapped "Toggle Faults" action from Ctrl-Z to Ctrl-T (first one available that kind of made sense to me)
  • added suspend functionality to Ctrl-Z, which sends SIGTSTP signal to the current k9s process
  • added allowSuspend config to opt in to this behaviour
  • updated tests

TODO:

  • test on other platforms

Notes

This is my first contribution to this project, so happy to update things if there's anything missing/required. Thanks!

Copy link
Owner

@derailed derailed left a comment

Choose a reason for hiding this comment

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

@arajski Thanks for this update Artur!
Biggest concern with this behavior is to make sure we're cool all supported platforms.

@@ -13,6 +13,7 @@
"maxConnRetry": { "type": "integer" },
"readOnly": { "type": "boolean" },
"noExitOnCtrlC": { "type": "boolean" },
"noSuspendOnCtrlZ": { "type": "boolean" },
Copy link
Owner

Choose a reason for hiding this comment

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

Not keen on the negative naming here. Think folks may want to opt in just in case.
Perhaps allowSuspend? Ctrl-z is likely already understood from folks aware of this command.
To boot, it will default to false if left unset hence keeping current behavior.

Copy link
Author

Choose a reason for hiding this comment

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

agree on that one. I was looking at noExitOnCtrlC as a sort of naming convention here. Anyway - renamed to allowSuspend 👍

@@ -143,6 +144,7 @@ func (a *App) bindKeys() {
KeyColon: NewKeyAction("Cmd", a.activateCmd, false),
tcell.KeyCtrlR: NewKeyAction("Redraw", a.redrawCmd, false),
tcell.KeyCtrlC: NewKeyAction("Quit", a.quitCmd, false),
tcell.KeyCtrlZ: NewKeyAction("Suspend", a.suspendCmd, false),
Copy link
Owner

Choose a reason for hiding this comment

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

This should only be available if the option is set and likely we may not want to show it in the header all the time.

Copy link
Author

Choose a reason for hiding this comment

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

Moved it out of the initial set

return evt
}

if !a.Config.K9s.NoSuspendOnCtrlZ {
Copy link
Owner

Choose a reason for hiding this comment

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

We would not be needed the check here if we don't enable the action in the first place?

Copy link
Author

Choose a reason for hiding this comment

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

removed ;)

}

if !a.Config.K9s.NoSuspendOnCtrlZ {
a.Suspend(func() {
Copy link
Owner

Choose a reason for hiding this comment

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

This will require additional testing on various platforms to make sure we're cool on other non nix aka windows?

Copy link
Author

Choose a reason for hiding this comment

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

I've got Windows 10 as dual boot, so can give it a test. Is there are set of other recommended platforms/versions?
Some VMs/containers might be required for this.

@@ -40,7 +40,7 @@ func (r *Reference) Init(ctx context.Context) error {

func (r *Reference) bindKeys(aa ui.KeyActions) {
aa.Delete(ui.KeyShiftA, tcell.KeyCtrlS, tcell.KeyCtrlSpace, ui.KeySpace)
aa.Delete(tcell.KeyCtrlW, tcell.KeyCtrlL, tcell.KeyCtrlZ)
aa.Delete(tcell.KeyCtrlW, tcell.KeyCtrlL)
Copy link
Owner

Choose a reason for hiding this comment

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

This should now be KeyCtrlT since we renamed it ;(

Copy link
Author

Choose a reason for hiding this comment

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

Added KeyCtrlT back

@derailed derailed added enhancement New feature or request question Further information is requested needs-tlc Pr needs additional updates labels Jan 18, 2024
Copy link
Owner

@derailed derailed left a comment

Choose a reason for hiding this comment

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

@arajski Thank you for the updates Artur! LGTM
I think we need to test this out on linux, windows flavors, macOs.

@arajski
Copy link
Author

arajski commented Jan 24, 2024

@derailed seems like go doesn't support os signals on Windows without some hacky workarounds.
I was thinking about spawning a shell for anything other than linux and darwin OS.
From a user perspective it's going to look almost the same, except in order to return to k9s, you'll need to exit the shell instead of resuming the process from the background. Let me know if that makes sense.
Otherwise we can go with the subshell approach for everyone.

@derailed
Copy link
Owner

derailed commented Feb 3, 2024

@arajski Thank you for the research Artur! I think for non nix users likely Ctrtl-z does not make much sense. I don't use windows much these days but isn't Ctrl-z equivalent to undo?
If so might be best to detect the os and disable that functionality while in windows land?

@arajski
Copy link
Author

arajski commented Feb 6, 2024

Done 👍 Tested on macOS Sonoma and Debian 12

@arajski arajski requested a review from derailed February 6, 2024 13:38
Copy link
Owner

@derailed derailed left a comment

Choose a reason for hiding this comment

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

@arajski Thanks for the checks and updates Artur.

@@ -146,6 +148,12 @@ func (a *App) bindKeys() {
tcell.KeyCtrlU: NewSharedKeyAction("Clear Filter", a.clearCmd, false),
tcell.KeyCtrlQ: NewSharedKeyAction("Clear Filter", a.clearCmd, false),
}

if a.Config.K9s.AllowSuspend && runtime.GOOS != "windows" {
Copy link
Owner

Choose a reason for hiding this comment

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

I think it might be best to make a call on the config and keep this functionality encapsulated vs exposing on the call site? Perhaps something a.Config.K9s.IsSuspendable()?

Copy link
Author

Choose a reason for hiding this comment

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

yep makes sense - done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs-tlc Pr needs additional updates question Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants