#!/bin/bash # ============================================================================== # kubectl-install.sh # # Installs or updates kubectl to the latest stable release. # # Usage: # curl -fsSL https://curl-pipe-bash.watzinger.biz/kubectl-install.sh | sudo bash # # What it does: # - Resolves the latest stable release via https://dl.k8s.io/release/stable.txt # - Skips download if that version is already installed (idempotent) # - Verifies the SHA-256 checksum of the downloaded binary # - Installs to /usr/local/bin/kubectl (already on PATH) # - Adds the alias `k` for kubectl for all users: /etc/profile.d/kubectl.sh # (login shells), sourced from /etc/bash.bashrc if present (interactive # non-login shells on Debian/Ubuntu; /etc/bashrc elsewhere already does it) # - Cleans up temp files on exit, even on failure # # Requirements: curl, sha256sum, sudo # ============================================================================== set -euo pipefail # ── Configuration ────────────────────────────────────────────────────────────── INSTALL_PATH="/usr/local/bin/kubectl" PROFILE_SCRIPT="/etc/profile.d/kubectl.sh" SYSTEM_BASHRC="/etc/bash.bashrc" BASE_URL="https://dl.k8s.io/release" OS="$(uname -s | tr '[:upper:]' '[:lower:]')" ARCH="$(uname -m)" TMP_DIR="$(mktemp -d)" case "${ARCH}" in x86_64) ARCH="amd64" ;; aarch64|arm64) ARCH="arm64" ;; armv7l) ARCH="arm" ;; *) echo "[ERROR] Unsupported architecture: ${ARCH}"; exit 1 ;; esac # ── Cleanup on exit ──────────────────────────────────────────────────────────── cleanup() { rm -rf "$TMP_DIR" } trap cleanup EXIT # ── Helpers ──────────────────────────────────────────────────────────────────── err() { echo "[ERROR] $*" >&2; } info() { echo "[INFO] $*"; } # ── Dependency check ─────────────────────────────────────────────────────────── check_dependencies() { local missing=() for cmd in curl sha256sum; do command -v "$cmd" &>/dev/null || missing+=("$cmd") done if [ ${#missing[@]} -gt 0 ]; then err "Missing required tools: ${missing[*]}" err "Install them and re-run this script." exit 1 fi } # ── Version of the installed kubectl, empty if none ─────────────────────────── get_installed_version() { [[ -x "${INSTALL_PATH}" ]] || return 0 "${INSTALL_PATH}" version --client 2>/dev/null \ | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' \ | head -n1 } # ── Install or update the kubectl binary ────────────────────────────────────── install_kubectl() { local version version="$(curl -fsSL "${BASE_URL}/stable.txt")" if [[ ! "${version}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then err "Could not resolve latest stable kubectl version (got '${version}')." exit 1 fi info "Latest stable is ${version}" local installed installed="$(get_installed_version)" if [[ "${installed}" == "${version}" ]]; then info "kubectl ${version} already installed at ${INSTALL_PATH} - skipping download." return fi [[ -n "${installed}" ]] && info "Updating from ${installed}" local url="${BASE_URL}/${version}/bin/${OS}/${ARCH}/kubectl" info "Downloading ${version}..." curl -fsSL --progress-bar "${url}" -o "${TMP_DIR}/kubectl" info "Verifying checksum..." local expected_hash expected_hash="$(curl -fsSL "${url}.sha256")" echo "${expected_hash} ${TMP_DIR}/kubectl" | sha256sum --check --status info "Checksum verified OK." sudo install -m 0755 "${TMP_DIR}/kubectl" "${INSTALL_PATH}" info "Installed: $("${INSTALL_PATH}" version --client | head -n1)" } # ── Write the `k` alias for all users ───────────────────────────────────────── write_alias() { info "Writing alias definition to ${PROFILE_SCRIPT}..." sudo tee "${PROFILE_SCRIPT}" > /dev/null <<'EOF' # kubectl alias - managed by kubectl-install.sh alias k='kubectl' EOF sudo chmod 644 "${PROFILE_SCRIPT}" [[ -f "${SYSTEM_BASHRC}" ]] || return 0 local marker="# kubectl-install.sh" if grep -q "${marker}" "${SYSTEM_BASHRC}"; then info "${SYSTEM_BASHRC} already sources ${PROFILE_SCRIPT}." else info "Adding source line to ${SYSTEM_BASHRC} for non-login shells..." sudo tee -a "${SYSTEM_BASHRC}" > /dev/null <