Skip to content

Instantly share code, notes, and snippets.

@craigforr
Last active September 19, 2020 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigforr/f2af21e6dca74bd71c2e29c6b1e2640a to your computer and use it in GitHub Desktop.
Save craigforr/f2af21e6dca74bd71c2e29c6b1e2640a to your computer and use it in GitHub Desktop.
Installs or upgrades kubectl on Linux, including WSL2
#!/usr/bin/env bash
# Installs or upgrades kubectl on Linux
# Chuck for root privileges
if [[ "$EUID" != "0" ]] ; then
echo "Please execute script with sudo or as root."
exit 1
fi
# Check Dependencies
if ! curl --version &>/dev/null; then
echo "curl must be installed and in \$PATH"
exit 1
fi
INSTALLED_KUBECTL_PATH=$(which kubectl 2>/dev/null || echo NONE)
INSTALLED_KUBECTL_VERSION="$($INSTALLED_KUBECTL version --client --short | cut -f3 -d' ')"
LATEST_KUBECTL_VERSION=$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)
if [ $INSTALLED_KUBECTL_VERSION != $LATEST_KUBECTL_VERSION ] ; then
curl -#LO "https://storage.googleapis.com/kubernetes-release/release/${LATEST_KUBECTL_VERSION}/bin/linux/amd64/kubectl" \
&& chmod ug+x ./kubectl
if [ -x ./kubectl ] ; then
DOWNLOADED_KUBECTL_VERSION="$(./kubectl version --client --short | cut -f3 -d' ')"
fi
else
echo "The latest version is already installed."
fi
if [ $DOWNLOADED_KUBECTL_VERSION != $LATEST_KUBECTL_VERSION ] ; then
echo "ERROR: Something went wrong with the download."
else
mv ./kubectl $INSTALLED_KUBECTL_PATH
echo "Installation and/or upgrade complete."
fi
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment