├── .gitignore ├── README.md └── k /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Automatically run the correct version of kubectl every time 2 | 3 | This script will read the version of the Kubernetes server you are running. It will then download a new version of kubectl if needed and run your command with new kubectl. I named it `k` since most everyone appears to alias `kubectl` to `k` anyway. 4 | 5 | ![So Simple](https://media.giphy.com/media/3o6Zt16nOfEI0C9sPu/giphy.gif) 6 | 7 | ## Getting Started 8 | 9 | Download the file to somewhere in your path. Any time you would call `kubectl ...`, call `k ...` instead. 10 | 11 | `curl -OL https://raw.githubusercontent.com/jakepearson/k/master/k && chmod +x k` 12 | -------------------------------------------------------------------------------- /k: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set +xe 3 | 4 | # DEBUG 5 | # true => verbose (`set -xe`) 6 | # => nothing 7 | DEBUG=${DEBUG:-false} 8 | 9 | if [[ "$DEBUG" == "true" ]]; 10 | then 11 | set -xe 12 | echo "Enabled verbose logging." 13 | fi 14 | 15 | 16 | KX_PATH="${KX_PATH:-$HOME/.kx}" 17 | mkdir -p "$KX_PATH/cache" 18 | 19 | case "$OSTYPE" in 20 | *arwin*) 21 | OS="darwin" 22 | ;; 23 | *in32* | *indows*) 24 | OS="windows" 25 | ;; 26 | *) 27 | OS="linux" 28 | esac 29 | 30 | # Attempt to load the server version from cache 31 | if [ -n "$KUBECONFIG" ]; then 32 | if [ "$OS" == "darwin" ]; then 33 | KUBECONFIG_HASH=$(echo "$KUBECONFIG" | shasum -a 256 | cut -c1-5) 34 | else 35 | KUBECONFIG_HASH=$(echo "$KUBECONFIG" | sha256sum | cut -c1-5) 36 | fi 37 | VERSION_CACHE_FILE="$KX_PATH/cache/$KUBECONFIG_HASH" 38 | TARGET_VERSION=$(cat "$VERSION_CACHE_FILE" 2> /dev/null) 39 | fi 40 | 41 | # Use Correct CPU Architecture 42 | CPU_ARCHITECTURE=$(uname -m) 43 | case $CPU_ARCHITECTURE in 44 | armv5*) CPU_ARCHITECTURE="armv5";; 45 | armv6*) CPU_ARCHITECTURE="armv6";; 46 | armv7*) CPU_ARCHITECTURE="arm";; 47 | aarch64) CPU_ARCHITECTURE="arm64";; 48 | x86) CPU_ARCHITECTURE="386";; 49 | x86_64) CPU_ARCHITECTURE="amd64";; 50 | i686) CPU_ARCHITECTURE="386";; 51 | i386) CPU_ARCHITECTURE="386";; 52 | esac 53 | 54 | # Ensure we have at least 1 kubectl version 55 | # 56 | # This is used to make it easier to get the server version, which 57 | # is an API that rarely changes... at least that is the hope 58 | KNOWN_VERSION="$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)" 59 | LOCAL_KUBECTL=$KX_PATH/kubectl-$KNOWN_VERSION 60 | if [ ! -f "$LOCAL_KUBECTL" ]; then 61 | curl -L -o "$LOCAL_KUBECTL" "https://storage.googleapis.com/kubernetes-release/release/$KNOWN_VERSION/bin/$OS/$CPU_ARCHITECTURE/kubectl" > /dev/null 2>&1 62 | chmod +x "$LOCAL_KUBECTL" 63 | fi 64 | 65 | # Get the server version from the server if not cached 66 | if [ -z "$TARGET_VERSION" ]; then 67 | TARGET_VERSION=$($LOCAL_KUBECTL version -o json | jq -r '.serverVersion.gitVersion') 68 | if [ -z "$TARGET_VERSION" ] || [ "$TARGET_VERSION" == "null" ]; then 69 | echo "Unable to get version information from cluster" 70 | exit 1 71 | fi 72 | # watch out for different non-stable release versions 73 | if echo "$TARGET_VERSION" | grep -E "alpha|beta|rc" > /dev/null 2>&1 74 | then 75 | TARGET_VERSION=$(echo $TARGET_VERSION | cut -d'.' -f1-4) 76 | else 77 | TARGET_VERSION=$(echo $TARGET_VERSION | cut -d'.' -f1-3) 78 | fi 79 | 80 | # watch out for the custom EKS version 81 | if echo "$TARGET_VERSION" | grep -E "eks" > /dev/null 2>&1 82 | then 83 | TARGET_VERSION=$(echo $TARGET_VERSION | cut -d'-' -f1) 84 | fi 85 | fi 86 | 87 | # Fill the cache if possible 88 | if [ -n "$KUBECONFIG" ]; then 89 | echo "$TARGET_VERSION" > "$VERSION_CACHE_FILE" 90 | fi 91 | 92 | TARGET=$KX_PATH/kubectl-$TARGET_VERSION 93 | 94 | if [ ! -f "$TARGET" ]; then 95 | curl -L -o "$TARGET" "https://storage.googleapis.com/kubernetes-release/release/$TARGET_VERSION/bin/$OS/$CPU_ARCHITECTURE/kubectl" > /dev/null 2>&1 96 | chmod +x "$TARGET" 97 | fi 98 | 99 | $TARGET "$@" 100 | --------------------------------------------------------------------------------