Skip to content

Instantly share code, notes, and snippets.

@nmelnick
Created February 17, 2022 20:56
Show Gist options
  • Save nmelnick/a1c52e284c7c18c7c05610bba650ae55 to your computer and use it in GitHub Desktop.
Save nmelnick/a1c52e284c7c18c7c05610bba650ae55 to your computer and use it in GitHub Desktop.
Simple kubectl declarative version updater
#!/bin/bash
# Updates kubectl in the ~/bin directory. Provide a base version number, like
# "1.20", and this will find the latest patch release, download it, verify the
# hash, and overwrite the kubectl in ~/bin.
# Usage: update_kubectl <base version number>
update_kubectl() {
if [[ $# -eq 0 ]]; then
echo 'Missing base version, such as "1.20".'
else
mkdir -p ~/bin
K_REPO="https://api.github.com/repos/kubernetes/kubernetes/releases?per_page=100"
REQ_VER=$(curl --silent -H "Accept: application/vnd.github.v3+json" $K_REPO | jq -r '.[].tag_name' | grep -v alpha | grep -v beta | grep -v rc | grep "v$1" | head -n1)
echo "Found version $REQ_VER."
cd /tmp && \
curl --silent -LO "https://dl.k8s.io/release/$REQ_VER/bin/darwin/amd64/kubectl" && \
curl --silent -LO "https://dl.k8s.io/release/$REQ_VER/bin/darwin/amd64/kubectl.sha256" && \
echo "$(<kubectl.sha256) kubectl" | shasum -a 256 --check && \
chmod a+x kubectl && \
cp kubectl ~/bin/kubectl
kubectl version --client
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment