Skip to content

Commit 008e30d

Browse files
fix(utils): make install-kubectl.sh work on macOS (#976)
install-kubectl.sh always downloaded the linux/amd64 kubectl, so on macOS it installed an unexecutable Linux ELF (later kubectl calls fail with an exec format error). Detect OS (uname -s) and arch (uname -m) and download the matching bin/{linux,darwin}/{amd64,arm64}/kubectl, mirroring the OS/arch detection added to install-minikube-cluster.sh in #970. Also: treat an existing kubectl as installed only if it actually runs (`kubectl version --client`), so a stale Linux binary left on PATH on macOS is replaced rather than skipped; and use `curl -f` so HTTP errors abort instead of saving an error page as the binary. Follow-up to the review on #970; relates to #931. Signed-off-by: HumphreySun98 <humphreysun98@gmail.com> Co-authored-by: Rui Zhang <51696593+ruizhang0101@users.noreply.github.com>
1 parent 0329ef2 commit 008e30d

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

utils/install-kubectl.sh

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,30 @@ set -e
55
KUBECTL_DIR="$HOME/.local/bin"
66
KUBECTL_PATH="$KUBECTL_DIR/kubectl"
77

8+
# Detect host OS and architecture so we download the matching kubectl build.
9+
# Without this the script always pulled the linux/amd64 binary, which on macOS
10+
# installs an unexecutable Linux ELF (exec format error on later kubectl calls).
11+
OS_KERNEL="$(uname -s)"
12+
case "$OS_KERNEL" in
13+
Linux) HOST_OS=linux ;;
14+
Darwin) HOST_OS=darwin ;;
15+
*) echo "ERROR: unsupported OS: $OS_KERNEL" >&2; exit 1 ;;
16+
esac
17+
18+
OS_ARCH="$(uname -m)"
19+
case "$OS_ARCH" in
20+
x86_64 | amd64) HOST_ARCH=amd64 ;;
21+
arm64 | aarch64) HOST_ARCH=arm64 ;;
22+
*) echo "ERROR: unsupported architecture: $OS_ARCH" >&2; exit 1 ;;
23+
esac
24+
25+
# A kubectl on PATH only counts as installed if it actually runs on this host
26+
# (a stale Linux binary on macOS is on PATH but cannot execute).
827
kubectl_exists() {
9-
command -v kubectl >/dev/null 2>&1
28+
command -v kubectl >/dev/null 2>&1 && kubectl version --client >/dev/null 2>&1
1029
}
1130

12-
# If kubectl is already installed, exit
31+
# If a working kubectl is already installed, exit
1332
if kubectl_exists; then
1433
echo "kubectl is already installed"
1534
exit 0
@@ -18,8 +37,9 @@ fi
1837
# Ensure the target directory exists
1938
mkdir -p "$KUBECTL_DIR"
2039

21-
# Install kubectl
22-
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
40+
# Install kubectl for the detected platform. -f makes curl fail on HTTP errors
41+
# instead of saving an error page as the binary.
42+
curl -fLO "https://dl.k8s.io/release/$(curl -fL -s https://dl.k8s.io/release/stable.txt)/bin/${HOST_OS}/${HOST_ARCH}/kubectl"
2343
chmod +x kubectl
2444
mv kubectl "$KUBECTL_PATH"
2545

0 commit comments

Comments
 (0)