-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubectl-cnf
executable file
·138 lines (110 loc) · 3.57 KB
/
kubectl-cnf
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
#!/usr/bin/env bash
[[ -n "${DEBUG}" ]] && set -x
set -eo pipefail
KCNF_VERSION="v0.0.8-dev"
KCNF_DIR="${KCNF_DIR:-${HOME}/.kube/configs}"
KCNF_HEIGHT="${KCNF_HEIGHT:-40%}"
KCNF_NO_VERBOSE="${KCNF_NO_VERBOSE:-0}"
KCNF_NO_SHELL="${KCNF_NO_SHELL:-0}"
KCNF_COPY_CLIP="${KCNF_COPY_CLIP:-0}"
KCNF_SYMLINK="${KCNF_SYMLINK:-0}"
DEPENDENCIES=(fzf bat)
### show plugin version and exit
# arg: none
show_version() {
echo "kubectl cnf version ${KCNF_VERSION}"
exit
}
### show help message and exit
# arg: none
show_help() {
cat << EOF | bat --style=plain --language=man
kubectl cnf helps switch between current-contexts in multiple kubeconfigs
Usage:
kubectl cnf [-h] [<string>]
Flags:
-h, --help show this message
-v, --version show plugin version
Dependencies:
fzf - https://github.com/junegunn/fzf
bat - https://github.com/sharkdp/bat
Prerequisites:
directory '${KCNF_DIR}' populated with kubeconfig files
EOF
exit
}
### ensure that dependencies are installed
# arg: none
check_requirements() {
local dep
for dep in "${DEPENDENCIES[@]}"; do
hash "${dep}" 2>/dev/null || err "missing core dependency: ${dep}"
done
}
### show error message and exit
# arg: $1 - text
err() {
echo "🮲 error: $1" >&2
exit 1
}
### copy kubeconfig export command to clipboard
# arg: none
copy_to_clipboard() {
local platform clipboard_cmd
platform=$(uname)
case "${platform}" in
Linux) case "${XDG_SESSION_TYPE:-x11}" in
x11) clipboard_cmd=("xsel" "--clipboard") ;;
wayland) clipboard_cmd=("wl-copy") ;;
*) err "clipboard copy is not supported on session: ${XDG_SESSION_TYPE}" ;;
esac
;;
Darwin) clipboard_cmd=("pbcopy")
;;
*) err "clipboard copy is not supported on platform: ${platform}"
;;
esac
hash "${clipboard_cmd[0]}" 2>/dev/null || err "missing clipboard dependency: ${clipboard_cmd[0]}"
echo -n "${EXPORT_COMMAND}" | "${clipboard_cmd[@]}"
}
main() {
local kubeconfigs selected_kubeconfig
check_requirements
[[ " $* " =~ ( -h | --help ) ]] && show_help
[[ " $* " =~ ( -v | --version ) ]] && show_version
[[ -d "${KCNF_DIR}" ]] || err "directory does not exist: ${KCNF_DIR}"
kubeconfigs=$(find "${KCNF_DIR}" \( -type f -o -type l \) -exec awk '/^current-context:/ {print FILENAME, $2}' {} +)
[[ -n "${kubeconfigs}" ]] || err "no valid kubeconfigs found in directory: ${KCNF_DIR}"
selected_kubeconfig=$(sort --key=2 <<< "${kubeconfigs}" | fzf \
--height="${KCNF_HEIGHT}" \
--layout=reverse \
--with-nth=2 \
--query="${*:-}" \
--bind="tab:toggle-preview" \
--preview="{ echo '# {1}'; kubectl config view --kubeconfig {1}; } | bat --style=plain --color=always --language=yaml" \
--preview-window="hidden,wrap,75%")
export KUBECONFIG="${selected_kubeconfig%% *}"
export KUBECONTEXT="${selected_kubeconfig##* }"
if (( KCNF_NO_SHELL )) || (( KCNF_COPY_CLIP )) || (( KCNF_SYMLINK )); then
(( KCNF_NO_VERBOSE )) || echo "⮺ ${KUBECONTEXT}"
fi
if (( KCNF_NO_SHELL )) || (( KCNF_COPY_CLIP )); then
EXPORT_COMMAND="export KUBECONFIG='${KUBECONFIG}'"
fi
if (( KCNF_NO_SHELL )); then
echo "${EXPORT_COMMAND}"
return
fi
if (( KCNF_COPY_CLIP )); then
copy_to_clipboard
return
fi
if (( KCNF_SYMLINK )); then
ln --symbolic --force "${KUBECONFIG}" ~/.kube/config
return
fi
(( KCNF_NO_VERBOSE )) || echo "⇲ ${KUBECONTEXT}"
"${SHELL}" || true
(( KCNF_NO_VERBOSE )) || echo "⇱ ${KUBECONTEXT}"
}
main "$@"