This repository has been archived by the owner on May 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
kbenv.sh
211 lines (175 loc) · 5.5 KB
/
kbenv.sh
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env bash
function _kbenv_test_requirements {
if [[ ! "$(command -v curl)" ]]
then
echo "You must install curl"
exit 1
elif [[ ! "$(command -v jq)" ]]
then
echo "You must install jq"
exit 1
fi
}
function _kbenv_get_so_and_arch(){
local _uname
local _arch
_uname="$(uname -s)"
_arch="$(uname -m)"
case "${_uname}" in
Linux) machine=linux;;
Darwin) machine=darwin;;
*) machine="UNKNOWN:${_uname}"
esac
case "${_arch}" in
arm) architecture="arm";;
arm64) architecture="arm64";;
x86_64) architecture="amd64";;
*) architecture="UNKNOWN:${_arch}"
esac
if [[ "$machine" == "darwin" ]]
then
echo "$machine/amd64"
return 0
fi
echo "$machine/$architecture"
}
function kbenv_list_remote () {
echo "Fetching versions..."
# TODO Paginate over this url
versions_url="https://api.github.com/repos/kubernetes/kubernetes/releases?per_page=100"
versions="$(curl -s "$versions_url" | jq -r ".[].tag_name" | grep -v "rc\\|beta\\|alpha" | sort --version-sort)"
echo "$versions"
return 0
}
function kbenv_install () {
VERSION="$1"
if [[ -z "$VERSION" ]] && [[ -t 1 && -z ${KBENV_IGNORE_FZF:-} && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
VERSION=$(kbenv_list_remote | fzf)
fi
if [[ -z "$VERSION" ]]
then
echo "You must specify a version!"
return 1
fi
if [[ -e "$KUBECTL_BINARY_PATH/kubectl-$VERSION" ]]
then
echo "The version $VERSION is already installed!"
return 0
fi
os_arch=$(_kbenv_get_so_and_arch)
if [[ "$os_arch" = *"UNKNOWN"* ]]
then
echo "The architecture and/or the SO is not supported: $os_arch"
return 1
fi
url="https://storage.googleapis.com/kubernetes-release/release/$VERSION/bin/$os_arch/kubectl"
echo "Downloading binary..."
curl -s -L -o "/tmp/kubectl-$VERSION" "$url"
filetype="$(file -b "/tmp/kubectl-$VERSION")"
if [[ "$filetype" != *"executable"* ]]
then
echo "There was a problem downloading the file! You probably typed the version incorrectly, but it may be something else."
return 1
fi
chmod +x "/tmp/kubectl-$VERSION"
mv "/tmp/kubectl-$VERSION" "$KUBECTL_BINARY_PATH/kubectl-$VERSION"
if [[ -L "$KUBECTL_BINARY_PATH/kubectl" ]]
then
actual_version="$(basename "$(readlink -f "$KUBECTL_BINARY_PATH/kubectl")")"
echo "kubectl is pointing to the ${actual_version//kubectl-} version"
echo "Do you want to overwrite it? (y/n)"
read -r overwrite
if [[ "$overwrite" == "y" ]]
then
kbenv_use "$VERSION"
else
echo "Nothing done, kubectl still points to the ${actual_version//kubectl-} version"
fi
else
kbenv_use "$VERSION"
fi
}
function kbenv_uninstall(){
VERSION="$1"
if [[ -z "$VERSION" ]] && [[ -t 1 && -z ${KBENV_IGNORE_FZF:-} && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
VERSION=$(kbenv_list | fzf)
fi
if [[ -z "$VERSION" ]]
then
echo "You must specify a version!"
return 1
fi
if [[ -e "$KUBECTL_BINARY_PATH/kubectl-$VERSION" ]]
then
rm "$KUBECTL_BINARY_PATH/kubectl-$VERSION"
echo "The version $VERSION is uninstalled!"
else
echo "Nothing done, the version $VERSION is not installed!"
fi
}
function kbenv_list(){
installed_versions="$(find "${KUBECTL_BINARY_PATH}"/ -name '*kubectl*' -printf '%f\n' | grep -Eo 'v([0-9]\.?)+$' | sed '/^$/d' | sort --version-sort)"
echo "$installed_versions"
}
function kbenv_use(){
VERSION="$1"
if [[ -z "$VERSION" ]] && [[ -t 1 && -z ${KBENV_IGNORE_FZF:-} && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
VERSION=$(kbenv_list | fzf)
fi
if [[ -z "$VERSION" ]]
then
echo "You must specify a version!"
return 1
fi
installed="$(find "$KUBECTL_BINARY_PATH"/ -name "*$VERSION*")"
if [[ -z "$installed" ]]
then
echo "The $VERSION version is not installed!"
return 1
fi
actual_link="$(readlink -f "$KUBECTL_BINARY_PATH/kubectl")"
if [[ "$actual_link" =~ $VERSION ]]
then
echo "kubectl was already pointing to the $VERSION version!"
else
ln -sf "$KUBECTL_BINARY_PATH/kubectl-$VERSION" "$KUBECTL_BINARY_PATH/kubectl"
echo "Done! Now kubectl points to the $VERSION version"
export KUBECTL_HOME="$HOME/.kubectl/${VERSION}"
fi
}
function kbenv_help() {
echo "Usage: kbenv <command> [<options>]"
echo "Commands:"
echo " list-remote List all installable versions"
echo " list List all installed versions"
echo " install Install a specific version"
echo " use Switch to specific version"
echo " uninstall Uninstall a specific version"
}
function kbenv_init () {
KUBECTL_BINARY_PATH="$HOME/.bin"
if [[ ! -e "$KUBECTL_BINARY_PATH" ]]
then
mkdir "$KUBECTL_BINARY_PATH"
fi
}
function kbenv() {
ACTION="$1"
ACTION_PARAMETER="$2"
_kbenv_test_requirements
case "${ACTION}" in
"list-remote")
kbenv_list_remote;;
"list")
kbenv_list;;
"install")
kbenv_install "$ACTION_PARAMETER";;
"uninstall")
kbenv_uninstall "$ACTION_PARAMETER";;
"use")
kbenv_use "$ACTION_PARAMETER";;
*)
kbenv_help
esac
}
kbenv_init