Advertisement
Saichovsky

k9s_updater_installer.sh

Feb 7th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.61 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. if command -v k9s >/dev/null; then
  41.   C_VER=$(k9s version -s | awk '/^Version/ {print $NF}')
  42. fi
  43.  
  44. # Fetch the latest release version and platform
  45. L_VER=$(curl -s https://api.github.com/repos/derailed/k9s/releases/latest | jq -r .tag_name)
  46. PLATFORM=$(uname -m | sed 's/x86_64/amd64/; s/aarch64/arm64/')
  47.  
  48. if update_available $C_VER $L_VER; then
  49.   ARCHIVE="k9s_$(uname)_${PLATFORM}.tar.gz"
  50.   cd /tmp
  51.   echo "Downloading and installing the newer k9s ${L_VER}..."
  52.   curl -LJO --fail https://github.com/derailed/k9s/releases/download/${L_VER}/${ARCHIVE} &&
  53.     tar xzf $ARCHIVE k9s && sudo install -o root -g root -m 0755 k9s /usr/local/bin/k9s
  54.   rm -fr k9s*
  55.   echo "Done."
  56. else
  57.   echo "You have the latest version of k9s [$C_VER]"
  58. fi
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement