Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions utils/install-kubectl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@ set -e
KUBECTL_DIR="$HOME/.local/bin"
KUBECTL_PATH="$KUBECTL_DIR/kubectl"

# Detect host OS and architecture so we download the matching kubectl build.
# Without this the script always pulled the linux/amd64 binary, which on macOS
# installs an unexecutable Linux ELF (exec format error on later kubectl calls).
OS_KERNEL="$(uname -s)"
case "$OS_KERNEL" in
Linux) HOST_OS=linux ;;
Darwin) HOST_OS=darwin ;;
*) echo "ERROR: unsupported OS: $OS_KERNEL" >&2; exit 1 ;;
esac

OS_ARCH="$(uname -m)"
case "$OS_ARCH" in
x86_64 | amd64) HOST_ARCH=amd64 ;;
arm64 | aarch64) HOST_ARCH=arm64 ;;
*) echo "ERROR: unsupported architecture: $OS_ARCH" >&2; exit 1 ;;
esac

# A kubectl on PATH only counts as installed if it actually runs on this host
# (a stale Linux binary on macOS is on PATH but cannot execute).
kubectl_exists() {
command -v kubectl >/dev/null 2>&1
command -v kubectl >/dev/null 2>&1 && kubectl version --client >/dev/null 2>&1
}

# If kubectl is already installed, exit
# If a working kubectl is already installed, exit
if kubectl_exists; then
echo "kubectl is already installed"
exit 0
Expand All @@ -18,8 +37,9 @@ fi
# Ensure the target directory exists
mkdir -p "$KUBECTL_DIR"

# Install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
# Install kubectl for the detected platform. -f makes curl fail on HTTP errors
# instead of saving an error page as the binary.
curl -fLO "https://dl.k8s.io/release/$(curl -fL -s https://dl.k8s.io/release/stable.txt)/bin/${HOST_OS}/${HOST_ARCH}/kubectl"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Nesting the curl command to fetch the stable version inside the main download URL makes error handling and debugging difficult if the network request fails.

If the stable version lookup fails (e.g., due to a temporary network issue or DNS failure), the inner curl will fail silently (due to -s), resulting in an empty version string and a cryptic 404 error from the outer curl.

Consider extracting the version lookup into its own step with explicit error handling. This improves robustness and provides a clear error message to the user.

Suggested change
curl -fLO "https://dl.k8s.io/release/$(curl -fL -s https://dl.k8s.io/release/stable.txt)/bin/${HOST_OS}/${HOST_ARCH}/kubectl"
if ! KUBECTL_VERSION="$(curl -fL -s https://dl.k8s.io/release/stable.txt)" || [ -z "$KUBECTL_VERSION" ]; then
echo "ERROR: Failed to fetch the latest stable kubectl version" >&2
exit 1
fi
curl -fLO "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/${HOST_OS}/${HOST_ARCH}/kubectl"

chmod +x kubectl
mv kubectl "$KUBECTL_PATH"

Expand Down
Loading