Advertisement
Saichovsky

k9s_updater_installer.sh

Feb 7th, 2025 (edited)
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.73 KB | None | 0 0
  1. #!/bin/bash
  2. # Installs or updates the k9s Kubernetes client tool on your host
  3.  
  4. set -eu
  5.  
  6. update_available() {
  7.   # Extract version components using parameter expansion
  8.   local version1=${1#v}
  9.   local version2=${2#v}
  10.  
  11.   IFS='.' read -r major1 minor1 patch1 <<<"$version1"
  12.   IFS='.' read -r major2 minor2 patch2 <<<"$version2"
  13.  
  14.   # Compare major versions
  15.   if ((major1 < major2)); then
  16.     return 0 # true
  17.   elif ((major1 > major2)); then
  18.     return 1 # false
  19.   fi
  20.  
  21.   # Compare minor versions
  22.   if ((minor1 < minor2)); then
  23.     return 0 # true
  24.   elif ((minor1 > minor2)); then
  25.     return 1 # false
  26.   fi
  27.  
  28.   # Compare patch versions
  29.   if ((patch1 < patch2)); then
  30.     return 0 # true
  31.   elif ((patch1 > patch2)); then
  32.     return 1 # false
  33.   fi
  34.  
  35.   return 1 # false (versions are equal)
  36. }
  37.  
  38. # Use this if k9s is not installed, otherwise get the current version
  39. C_VER=0.0.1
  40. BINFILE=/usr/local/bin/k9s
  41. if command -v k9s >/dev/null; then
  42.   C_VER=$(k9s version -s | awk '/^Version/ {print $NF}')
  43.   BINFILE=$(command -v k9s)
  44. fi
  45.  
  46. # Fetch the latest release version and platform
  47. L_VER=$(curl -s https://api.github.com/repos/derailed/k9s/releases/latest | jq -r .tag_name)
  48. PLATFORM=$(uname -m | sed 's/x86_64/amd64/; s/aarch64/arm64/')
  49.  
  50. if update_available $C_VER $L_VER; then
  51.   ARCHIVE="k9s_$(uname)_${PLATFORM}.tar.gz"
  52.   cd /tmp
  53.   [[ "$C_VER" == "0.0.1" ]] && UPDATE_MSG="" || UPDATE_MSG="the newer "
  54.   echo "Downloading and installing ${UPDATE_MSG}k9s ${L_VER}..."
  55.   curl -LJO --fail https://github.com/derailed/k9s/releases/download/${L_VER}/${ARCHIVE} &&
  56.     tar xzf $ARCHIVE k9s && sudo install -o root -g root -m 0755 k9s $BINFILE
  57.   rm -fr k9s*
  58.   echo "Done."
  59. else
  60.   echo "You have the latest version of k9s [$C_VER]"
  61. fi
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement