-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkubectl-shell_ctx
More file actions
executable file
·184 lines (151 loc) · 5.14 KB
/
kubectl-shell_ctx
File metadata and controls
executable file
·184 lines (151 loc) · 5.14 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
# Kubernetes shell independent context switching
# Version: 1.0.14
set -eou pipefail
if [[ "$(basename "$0")" == kubectl-* ]]; then # we are invoked as plugin
SELF="kubectl shell-ctx"
else
SELF="shell-ctx"
fi
err() {
echo "Error, $*" 1>&2
exit 1
}
usage() {
cat <<-EOF
Usage:
$SELF : Select new context
$SELF clone : Clone context from another shell
$SELF hook <SHELL> : Hook for <SHELL>
$SELF -h,--help : Show this message
Setup:
$(do_help_hook)
EOF
}
do_help_hook() {
cat <<-EOF
Setup is required for $SELF to function correctly.
#################################################################################
# IMPORTANT: #
# If you are setting a custom KUBECONFIG #
# you need to do that BEFORE loading the shell hook! #
#################################################################################
BASH: append the following to your ~/.bashrc:
eval "\$($(basename "$0") hook bash)"
ZSH: append the following to your ~/.zshrc:
eval "\$($(basename "$0") hook zsh)"
FISH: append the following to your ~/.config/fish/config.fish:
$(basename "$0") hook fish | source
EOF
}
do_hook() {
# Setup tmp KUBECONFIG for this shell
if [ -z ${KUBECONFIG+x} ]; then
KUBECONFIG=""
fi
if [ -z ${KUBECTL_SHELL_CTX_DIR+x} ]; then
KUBECTL_SHELL_CTX_DIR="/tmp"
fi
local TMP_KUBECONFIG
TMP_KUBECONFIG=$(mktemp -q "${KUBECTL_SHELL_CTX_DIR}"/kubectl-shell-ctx.XXXXXX || exit 1)
if [[ "$1" == "bash" ]] || [[ "$1" == "zsh" ]]; then
echo "export KUBECONFIG=$TMP_KUBECONFIG"
echo "export KUBECTL_SHELL_CTX_HOOK=$1"
echo "trap \"rm -f -- $TMP_KUBECONFIG\" EXIT"
elif [[ "$1" == "fish" ]]; then
echo "set -x KUBECONFIG $TMP_KUBECONFIG"
echo "set -x KUBECTL_SHELL_CTX_HOOK $1"
echo "trap \"rm -f -- $TMP_KUBECONFIG\" EXIT"
else
err "Unknown shell $1"
fi
# Write current context to new temp context
(
local NEW_KUBECONFIG_CONTENT
NEW_KUBECONFIG_CONTENT=$($KUBECTL config view --raw)
# Create new kubeconfig for this shell
echo "$NEW_KUBECONFIG_CONTENT" >"$TMP_KUBECONFIG"
chmod 600 "$TMP_KUBECONFIG"
) &
}
do_clone() {
if [ -z ${KUBECTL_SHELL_CTX_DIR+x} ]; then
KUBECTL_SHELL_CTX_DIR="/tmp"
fi
# Find other shell-ctx instances
SHELL_CTXS=$(find "${KUBECTL_SHELL_CTX_DIR}"/ -type f -name "kubectl-shell-ctx*" -not -name "$(basename "$KUBECONFIG")" 2>/dev/null || true)
[[ -z "$SHELL_CTXS" ]] && err "no other shell-ctx instances found"
for SHELL_CTX in $SHELL_CTXS; do
local TMP_CTX_CONTEXT
TMP_CTX_CONTEXT=$(KUBECONFIG=$SHELL_CTX $KUBECTL config current-context || echo "No current context")
TMP_CTX+="$SHELL_CTX $TMP_CTX_CONTEXT\n"
done
SELECTED_CTX=$(echo -e "$TMP_CTX" | fzf -e --no-sort --border=top --border-label='Select context to clone' $FZF_STYLE_ARG --layout=reverse --pointer='▶' --marker='✓' --info=inline --cycle --height=~80% --prompt='Search: ' --color='label:cyan,prompt:cyan' | awk '{print $1}')
cp -rp "$SELECTED_CTX" "$KUBECONFIG"
}
main() {
if [[ -z "${KUBECTL:-}" ]]; then
if hash kubectl 2>/dev/null; then
KUBECTL=kubectl
elif hash kubectl.exe 2>/dev/null; then
KUBECTL=kubectl.exe
else
err "Error, kubectl is not found"
fi
fi
if [[ "$#" -eq 2 ]] && [[ "$1" == 'hook' ]]; then
do_hook "$2"
elif [[ "$#" -eq 0 ]]; then
# Check we have the hook setup
if [[ -z "${KUBECTL_SHELL_CTX_HOOK:-}" ]]; then
do_help_hook
else
checkRequirements
if command -v yq &>/dev/null; then
PREVIEW_CMD="$KUBECTL --context {} config view --minify | yq -C"
else
PREVIEW_CMD="$KUBECTL --context {} config view --minify"
fi
SELECTED_CTX="$($KUBECTL config get-contexts --no-headers=true -o name | sed 's/^\*/ /g' | fzf -e --no-sort --border=top --border-label='Select context' $FZF_STYLE_ARG --layout=reverse --preview="$PREVIEW_CMD" --preview-window=right,60%,border-rounded,wrap --pointer='▶' --marker='✓' --info=inline --cycle --height=~80% --prompt='Search: ' --color='label:cyan,prompt:cyan')"
$KUBECTL config use-context "$SELECTED_CTX"
fi
elif [[ "$#" -eq 1 ]] && [[ "$1" == "clone" ]]; then
checkRequirements
do_clone
elif [[ "$#" -eq 1 ]] && [[ "$1" == '-h' ]] || [[ "$1" == '--help' ]]; then
usage
elif [[ "$#" -eq 1 ]]; then
# Use context supplied on command line
$KUBECTL config use-context "$1"
else
echo "Error, unknown commandline arguments"
usage
exit 1
fi
}
checkRequirements() {
# Test for requirements
REQUIREMENTS="mktemp awk sed fzf kubectl"
for REQUIREMENT in $REQUIREMENTS; do
command -v "$REQUIREMENT" 1>/dev/null || {
err "requirement missing: $REQUIREMENT"
}
done
# Check fzf version (--style=full requires 0.58+)
checkFzfVersion
}
checkFzfVersion() {
local FZF_VERSION
FZF_VERSION=$(fzf --version | awk '{print $1}' | cut -d. -f1-2)
local MAJOR
local MINOR
MAJOR=$(echo "$FZF_VERSION" | cut -d. -f1)
MINOR=$(echo "$FZF_VERSION" | cut -d. -f2)
# --style=full requires fzf 0.58+
if [[ $MAJOR -eq 0 ]] && [[ $MINOR -lt 58 ]]; then
FZF_STYLE_ARG=""
else
FZF_STYLE_ARG="--style=full"
fi
}
main "$@"