Skip to content

Instantly share code, notes, and snippets.

@ntfrnzn
Created December 14, 2020 18:58
Show Gist options
  • Save ntfrnzn/461c9fbbc204d09afa45035d27e4edc0 to your computer and use it in GitHub Desktop.
Save ntfrnzn/461c9fbbc204d09afa45035d27e4edc0 to your computer and use it in GitHub Desktop.
#/usr/bin/env bash
function exit_message() {
echo "ERROR: $1" >&2
exit 1
}
# Usage info
function show_help() {
cat << EOF
Usage: ${0##*/} [OPTIONS]
Fetch and install the kubectl utility
Arguments:
-h (--help) display this help and exit
-v (--version) install a particular version of kubectl
EOF
}
# trap 'exit_message oops' ERR
while (( "$#" )); do
case "$1" in
-h|--help)
show_help && exit 0
;;
-v|--version)
VERSION=$2
shift 2
;;
*)
echo "Unknown option: <${1}>"
echo
show_help
exit 1
;;
esac
done
### installing kubectl
stable_version=$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)
version=${VERSION:-$stable_version}
DOWNLOAD="YES"
KUBECTL=$(which kubectl)
if [ "$KUBECTL" ]
then
current_version=$(kubectl version --short --client | awk '{print $NF}' )
if [ "$current_version" == "$version" ]
then
DOWNLOAD="NO"
fi
fi
# echo $DOWNLOAD "$current_version" ?= "$version"
if [ $DOWNLOAD == "YES" ]
then
arch=$(uname -s | tr '[:upper:]' '[:lower:]') # linux|darwin
url="https://storage.googleapis.com/kubernetes-release/release/${version}/bin/${arch}/amd64/kubectl"
echo "installing kubectl $version $arch to /usr/local/bin"
curl -s -O $url
chmod a+x kubectl
mv kubectl /usr/local/bin
KUBECTL=/usr/local/bin/kubectl
fi
echo "kubectl binary is located at $KUBECTL"
$KUBECTL version --client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment