-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathk
More file actions
executable file
·54 lines (45 loc) · 1.31 KB
/
k
File metadata and controls
executable file
·54 lines (45 loc) · 1.31 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
#!/usr/bin/env bash
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 [ ! -z "$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 [ ! -z "$KUBECONFIG" ]; then
echo "$TARGET_VERSION" > "$VERSION_CACHE_FILE"
fi
TARGET=$KX_PATH/kubectl-$TARGET_VERSION
if [ ! -f $TARGET ]; then
cd "$KX_PATH"
echo "Downloading kubectl $TARGET_VERSION..."
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 "$@"