kubectl-iexec icon indicating copy to clipboard operation
kubectl-iexec copied to clipboard

Should detect when piping to disable formatting

Open weltonrodrigo opened this issue 10 months ago • 6 comments

Current behavior

When doing something like

kubectl iexec api printenv | sort > .env

.env will have formatting markers meant to the terminal:


[2K
[34m?[0m Select Pod: 
[2K
[34m?[0m Select Pod: 
[2K
  Namespace: [34mweb[0m | Pod: [1m▸[0m [36m-5979b66dff-92snq[0m
[2K
  Namespace: [34mweb[0m | Pod: [1m▸[0m [36mapi-5979b66dff-92snq[0m
[2K
  Namespace: [34mweb[0m | Pod: [35mapi-5979b66dff-qwcnp[0m
[2K
  Namespace: [34mweb[0m | Pod: [35mapi-5979b66dff-qwcnp[0m
[?25l[2K
[2mUse the arrow keys to navigate:[0m [2m↓[0m [2m↑[0m [2m→[0m [2m←[0m 
[J[2K
 [J[2K
 [J[2K
[1A[2K
[1A[2K
[1A[2K
[1A[2K
[2K
[2mUse the arrow keys to navigate:[0m [2m↓[0m [2m↑[0m [2m→[0m [2m←[0m 
 [1A[1A[1A[1A[2K
Namespace: [34mweb[0m | Pod: [32m✔[0m [36mapi-5979b66dff-92snq[0m[1B[2K
[1B[2K
[1B[2K
[1B[?25h[J[2K
PATH=/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MYKEYA=myvaluea
MYKEYB=myvalueb

Suggested behavior

kubectl iexec should provide an option to select any pod (to skip the pod selection) and detect if writing to a pipe or file so that it can disable terminal formmating.

Thanks for this. I use it A LOT LOT LOT.

weltonrodrigo avatar Jan 17 '25 18:01 weltonrodrigo

Hey @weltonrodrigo thanks for raising the issue, this is an interesting use case. Are you trying to pipe the command inside the remote container or in your current shell? I'll explore both options to see if there are any limiting factors.

gabeduke avatar Jan 17 '25 22:01 gabeduke

In my case I’m extracting data from the container so piping into the current shellEm 17 de jan. de 2025, à(s) 19:18, Gabriel Duke @.***> escreveu: Hey @weltonrodrigo thanks for raising the issue, this is an interesting use case. Are you trying to pipe the command inside the remote container or in your current shell? I'll explore both options to see if there are any limiting factors.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

weltonrodrigo avatar Jan 18 '25 00:01 weltonrodrigo

Can you try the build on this release: https://github.com/gabeduke/kubectl-iexec/releases/tag/v1.19.15-alphav1

gabeduke avatar Jan 18 '25 02:01 gabeduke

Can you try the build on this release: https://github.com/gabeduke/kubectl-iexec/releases/tag/v1.19.15-alphav1

Please see the screencapture: https://asciinema.org/a/M5adgTVXILFwAtrrdumICNoEp

Maybe one of those two behaviors:

A)

  • have --choose-first argument with default value of false. If true, just run the command on the first pod matching the filter.
  • have --naked disable all formatting and coloring
  • if printing to the terminal, act as always
  • if printing to a pipe, make --choose-first=true and naked=true

B)

  • if printing to the terminal, act as always
  • if printing to a pipe, render the ui on stderr instead of stdout.

On the code I tried, redirecting to a pipe rendered my terminal broken.

thanks.

weltonrodrigo avatar Jan 21 '25 16:01 weltonrodrigo

If we choose the first pod and container it defeats the purpose of having an interactive selection, the core purpose of the tool. Why wouldn't we just use the kubectl exec tool in this case?

The build I shared prints the interactive menu directly to /dev/tty which should bypass the pipe since it is not in stdout. If that doesn't work for you I would need to come up with a different solution. What operating system, terminal and shell are you using?

gabeduke avatar Jan 24 '25 12:01 gabeduke

Why wouldn't we just use the kubectl exec tool in this case?

The killer feature of iexec for was the ability to have a repeatable command in my history waiting for a CTRL-R when I need to repeat it. Doing kubectl exec myapp-845d5bd77-9x2dj curl http://aserviceinthecluster:8080/api/users/id/10 wouldn't work because when i need to run the command again I would type CTRL-R api/users 10 <ENTER><ENTER><ENTER> (one enter to select the entry from the fzf history list, other to run it, other to select the pod from the list. If i uses kubectl exec, the pod name is not a stable reference to the "thing" I want to run the command against, which is any pod in the deployment myapp.

Also, when running for the first time, typing kubectl exec myapp<TAB> would start autocomplete, but then I need to type kubectl exec myapp-<TAB>8<TAB>j curl otherurl<ENTER> (the j is to select the first pod from the pod list) which is very cumbersome when compared with kubectl iexec myapp curl otherurl<ENTER><ENTER> (the second enter is to select the first pod in the list).

But your answer made me revisit kubectl exec and discover that it now allows kubectl exec deploy/myapp -- curl http://aserviceinthecluster:8080/api/users/id/10 which now perfectly fits my use case.

If you still want to pursue this, though, my os is Sonoma 14.6.1, my zsh is zsh 5.9 (x86_64-apple-darwin23.0) (from homebrew) with kubectl is Client Version: v1.30.0.

thanks again for your time.

weltonrodrigo avatar Jan 24 '25 14:01 weltonrodrigo