-
Notifications
You must be signed in to change notification settings - Fork 10
/
kubectl-sh-wrapper
executable file
·60 lines (56 loc) · 1.89 KB
/
kubectl-sh-wrapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# kubectl-sh-wrapper (part of ossobv/vcutil) // wdoekes/2019 // Public Domain
#
# Wrapper to call custom kubectl shell wrapper scripts. By also calling it
# kubectl, we get a certain amount of tab-completion support.
#
# IMPORTANT: To make this work, you'll need to manually symlink this.
#
# ln -s /usr/bin/kubectl-sh-wrapper /usr/local/bin/kubectl
#
# And the real kubectl needs to be available as /usr/bin/kubectl.
#
# Provides access to:
# --sh-bash: calls kubectl-sh-bash, sets shell $LINES/$COLUMNS, execs bash
# --sh-secret: calls kubectl-sh-secret, helps with opaque secrets
#
# Examples:
#
# kubectl --help # regular behaviour
#
# kubectl exec --sh-bash -it PODNAME # goes through kubectl-sh-bash
#
test -z "${BASH_VERSION}" && echo "$0: requires bash" >&2 && exit 1
# Get first kubectl command from $PATH that is not us; in a bash-y fashion
# without spawning too many subprocesses. Note that we might call $KUBECTL in
# the wrapper scripts directly.
while IFS='' read -d $'\n' l; do
test "$l" != "${BASH_SOURCE[0]}" && KUBECTL=$l && break
done < <(which -a kubectl)
# Look through args and find --sh-* shell plugins.
script=
declare -a ARGS
for var in "$@"; do
# Catch custom commands:
if [[ "$var" == --sh-bash ]]; then
script=kubectl-sh-bash
elif [[ "$var" == --sh-secret ]]; then
script=kubectl-sh-secret
elif [[ "$var" == --sh-* ]]; then
echo "Usage: $0 [...] --sh-* [...]" >&2
echo "Where --sh-* is one of:" >&2
echo " --sh-bash: sets LINES/COLUMNS for kubectl exec" >&2
echo " --sh-secret: helps with opaque secret handling" >&2
exit 1
else
ARGS[${#ARGS[@]}]="$var"
fi
done
# Found a script? Source it; they're bash commands too.
if test -n "$script"; then
set -- "${ARGS[@]}" # update argv
source "$(command -v "$script")"
exit $?
fi
# Call the real kubectl.
exec "$KUBECTL" "${ARGS[@]}"