forked from Azure/aks-engine-azurestack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk
executable file
·57 lines (46 loc) · 1.41 KB
/
k
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
#!/usr/bin/env bash
# Downloads and runs the correct version of kubectl just in time.
# This script is modified from the original source at https://github.com/jakepearson/k
set +x
KX_PATH="$HOME/.kx"
mkdir -p "$KX_PATH/cache"
case "$OSTYPE" in
*arwin*)
OS="darwin"
;;
*in32* | *indows*)
OS="windows"
;;
*)
OS="linux"
esac
# Attempt to load the server version from cache
if [ -n "$KUBECONFIG" ]; then
if [ "$OS" == "darwin" ]; then
KUBECONFIG_HASH=$(echo "$KUBECONFIG" | shasum -a 256 | cut -c1-5)
else
KUBECONFIG_HASH=$(echo "$KUBECONFIG" | sha256sum | cut -c1-5)
fi
VERSION_CACHE_FILE="$KX_PATH/cache/$KUBECONFIG_HASH"
TARGET_VERSION=$(cat "$VERSION_CACHE_FILE" 2> /dev/null)
fi
# Get the server version from the server if not cached
if [ -z "$TARGET_VERSION" ]; then
TARGET_VERSION=$(kubectl version -o json 2>/dev/null | jq -r '.serverVersion.gitVersion')
fi
# Default to kubectl v1.10.12 if TARGET_VERSION was unset and `kubectl version` failed
if [ "$TARGET_VERSION" == "null" ]; then
TARGET_VERSION=v1.10.12
fi
# Fill the cache if possible
if [ -n "$KUBECONFIG" ]; then
echo "$TARGET_VERSION" > "$VERSION_CACHE_FILE"
fi
TARGET=$KX_PATH/kubectl-$TARGET_VERSION
if [ ! -f $TARGET ]; then
cd "$KX_PATH"
curl -sLO "https://storage.googleapis.com/kubernetes-release/release/$TARGET_VERSION/bin/$OS/amd64/kubectl"
mv $KX_PATH/kubectl $TARGET
chmod +x $TARGET
fi
$TARGET "$@"