Skip to content

Commit a413c35

Browse files
authored
Merge pull request #2 from predatorray/0.0.x
Options `-it` are deprecated & Pod names can be read from a file using `-f`
2 parents b0775e1 + 29e5ba3 commit a413c35

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
NAME=kubectl-tmux-exec
2-
VERSION=0.0.2
2+
VERSION=0.0.3
33

44
OUTPUT_DIR=output
55
RELEASE_FILE_NAME=$(NAME)-$(VERSION).tar.gz

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# kubectl-tmux-exec
22

3-
![homebrew](https://img.shields.io/badge/homebrew-0.0.2-orange)
3+
![homebrew](https://img.shields.io/badge/homebrew-0.0.3-orange)
44
![license](https://img.shields.io/badge/license-MIT-green)
55

66
A kubectl plugin that uses [Tmux](https://github.com/tmux/tmux) to multiplex commands to pods.
@@ -9,12 +9,12 @@ A kubectl plugin that uses [Tmux](https://github.com/tmux/tmux) to multiplex com
99

1010
It is to `kubectl exec` as `csshX` or `pssh` is to `ssh`.
1111

12-
Instead of `exec bash` into multiple pod's containers one-at-a-time, like `kubectl exec -it pod{N} /bin/bash`.
12+
Instead of `exec bash` into multiple pod's containers one-at-a-time, like `kubectl exec pod{N} /bin/bash`.
1313

1414
You can now use
1515

1616
```sh
17-
kubectl tmux-exec -it -l app=nginx /bin/bash
17+
kubectl tmux-exec -l app=nginx /bin/bash
1818
```
1919

2020
# Installation via Homebrew
@@ -68,11 +68,9 @@ The `tmux-exec` is similar to `exec`, except that it requires label selectors wh
6868
To `bash` into all pod containers that share some common labels, `foo=bar` for instance.
6969

7070
```sh
71-
kubectl tmux-exec -it -l foo=bar /bin/bash
71+
kubectl tmux-exec -l foo=bar /bin/bash
7272
```
7373

74-
It should be noted that the `-i` / `--stdin` and `-t` / `--tty` options must both be turned on when you are trying to initiate an interactive session. If not, there will not be any errors. Instead, the `tmux` process simply exits because the `exec`-ed command exits due to no inputs.
75-
7674
After you have successfully `bash`-ed into your selected containers, a Tmux window is opened actually, where each pane displays the execution result of each pod's container. Your keyboard inputs will be synchronized to all those panes.
7775

7876
If you are not familar with Tmux, you can have a look at tmux's man page or online tutorials. Or you can see the cheatsheet below, which will be sufficient I think.

bin/kubectl-tmux_exec

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,24 @@ Examples:
6464
${PROG_NAME} -l app=nginx -- tail -f /var/log/nginx/access.log
6565
6666
# Open bash terminals that attach to 'nginx' containers of all nginx pods
67-
${PROG_NAME} -l app=nginx -c nginx -it /bin/bash
67+
${PROG_NAME} -l app=nginx -c nginx /bin/bash
68+
69+
# Read pod names from a file instead of using a selector
70+
${PROG_NAME} -f pods.txt /bin/bash
71+
${PROG_NAME} -f - /bin/bash # read from stdin
6872
6973
Options:
7074
-c, --container='': Container name. If omitted, the first container in the pod will be chosen
71-
-i, --stdin=false: Pass stdin to the container
72-
-t, --tty=false: Stdin is a TTY
75+
-i, --stdin=true: Pass stdin to the container (deprecated, since it's enabled by default)
76+
-t, --tty=true: Stdin is a TTY (deprecated, since it's enabled by default)
7377
-l, --selector: Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
78+
-f, --file: Read pod names line-by-line from a file
7479
--remain-on-exit=false: Remain Tmux window on exit
75-
--select-layout=tiled: one of the five Tmux preset layouts: even-horizontal, even-vertical, main-horizontal,
80+
--select-layout=tiled: One of the five Tmux preset layouts: even-horizontal, even-vertical, main-horizontal,
7681
main-vertical, or tiled.
7782
7883
Usage:
79-
${PROG_NAME} -l label [-c CONTAINER] [flags] -- COMMAND [args...]
84+
${PROG_NAME} { -l label | -f pod-list-file } [-c CONTAINER] [flags] -- COMMAND [args...]
8085
8186
Use "kubectl options" for a list of global command-line options (applies to all commands).
8287
EOF
@@ -91,6 +96,10 @@ function check_required_executables() {
9196
done
9297
}
9398

99+
function warn() {
100+
echo >&2 'warn:' "$@"
101+
}
102+
94103
function error_and_exit() {
95104
echo >&2 'error:' "$@"
96105
echo >&2 "Run '${PROG_NAME} --help' for more information on the command."
@@ -127,17 +136,16 @@ function array_contains() {
127136

128137
function main() {
129138
local opts
130-
opts=$(ggetopt -o hitc:l:"$(printf '%s:' "${KUBECTL_SHORT_OPTS[@]}")" --long \
131-
help,stdin,tty,container:,selector:,remain-on-exit,select-layout:,"$(printf '%s:,' "${KUBECTL_LONG_OPTS[@]}")","$(printf '%s,' "${KUBECTL_NOARG_LONG_OPTS[@]}")" -- "$@")
139+
opts=$(ggetopt -o hitc:l:f:"$(printf '%s:' "${KUBECTL_SHORT_OPTS[@]}")" --long \
140+
help,stdin,tty,container:,selector:,remain-on-exit,select-layout:,file:,"$(printf '%s:,' "${KUBECTL_LONG_OPTS[@]}")","$(printf '%s,' "${KUBECTL_NOARG_LONG_OPTS[@]}")" -- "$@")
132141
eval set -- $opts
133142

134143
local selector
135144
local container_name
136-
local opt_stdin=0
137-
local opt_tty=0
138145
local kubectl_opts=()
139146
local remain_on_exit=0
140147
local tmux_layout='tiled'
148+
local pod_list_file
141149
while [[ $# -gt 0 ]]; do
142150
local opt="$1"
143151
case "${opt}" in
@@ -150,10 +158,10 @@ function main() {
150158
container_name="$1"
151159
;;
152160
-i|--stdin)
153-
opt_stdin=1
161+
warn "The option -i / --stdin is deprecated."
154162
;;
155163
-t|--tty)
156-
opt_tty=1
164+
warn "The option -t / --tty is deprecated."
157165
;;
158166
-l|--selector)
159167
shift
@@ -166,6 +174,10 @@ function main() {
166174
shift
167175
tmux_layout="$1"
168176
;;
177+
-f|--file)
178+
shift
179+
pod_list_file="$1"
180+
;;
169181
--)
170182
shift
171183
break
@@ -204,8 +216,9 @@ function main() {
204216
error_and_exit 'you must specify at least one command for the container'
205217
fi
206218

207-
if [[ -z "${selector:-}" ]]; then
208-
error_and_exit 'The label selector option is required: -l'
219+
if ( [[ -z "${selector:-}" ]] && [[ -z "${pod_list_file:-}" ]] ) ||
220+
( [[ ! -z "${selector:-}" ]] && [[ ! -z "${pod_list_file:-}" ]] ); then
221+
error_and_exit 'you must either specify option -l or -f'
209222
fi
210223

211224
if [[ -z "${tmux_layout}" ]] || ! array_contains "${tmux_layout}" "${TMUX_LAYOUTS[@]}"; then
@@ -214,13 +227,7 @@ function main() {
214227

215228
local commands=("$@")
216229

217-
local kubectl_exec_opts=''
218-
if [[ "${opt_stdin}" -eq 1 ]]; then
219-
kubectl_exec_opts="${kubectl_exec_opts} -i"
220-
fi
221-
if [[ "${opt_tty}" -eq 1 ]]; then
222-
kubectl_exec_opts="${kubectl_exec_opts} -t"
223-
fi
230+
local kubectl_exec_opts='-i -t'
224231
if [[ ! -z "${container_name:-}" ]]; then
225232
kubectl_exec_opts="${kubectl_exec_opts} -c ${container_name}"
226233
fi
@@ -234,11 +241,22 @@ function main() {
234241
fi
235242
done
236243

244+
if [[ ! -z "${pod_list_file:-}" ]] && [[ "${pod_list_file}" != '-' ]] && [[ ! -f "${pod_list_file}" ]]; then
245+
error_and_exit "No such file or directory: ${pod_list_file}"
246+
fi
247+
237248
local pods=()
238249
while IFS='' read -r pod_name; do
250+
if [[ -z "${pod_name}" ]]; then
251+
continue
252+
fi
239253
pods+=("${pod_name}")
240254
done < <(
241-
kubectl ${kubectl_opts[@]:-} get pods -l "${selector}" -o custom-columns=':metadata.name' --no-headers
255+
if [[ -z "${selector:-}" ]]; then
256+
cat "${pod_list_file}"
257+
else
258+
kubectl ${kubectl_opts[@]:-} get pods -l "${selector}" -o custom-columns=':metadata.name' --no-headers
259+
fi
242260
)
243261

244262
if [[ "${#pods[@]}" -eq 0 ]]; then

0 commit comments

Comments
 (0)