Files
aiops/scripts/install-kubectl.sh
T

47 lines
1.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# 按目标 OS/ARCH 下载 kubectl(Docker 构建 / 本地脚本共用)// Logan.Li
set -eu
KUBECTL_VERSION="${KUBECTL_VERSION:-v1.32.2}"
TARGET_OS="${TARGET_OS:-linux}"
TARGET_ARCH="${TARGET_ARCH:-amd64}"
DEST="${1:-/usr/local/bin/kubectl}"
case "${TARGET_ARCH}" in
amd64|x86_64) KARCH=amd64 ;;
arm64|aarch64) KARCH=arm64 ;;
*)
echo "[install-kubectl] 不支持的架构: ${TARGET_ARCH}" >&2
exit 1
;;
esac
# 容器内固定 linux;本地 Mac 调试可 TARGET_OS=darwin
case "${TARGET_OS}" in
linux|darwin) ;;
*)
echo "[install-kubectl] 不支持的平台: ${TARGET_OS}" >&2
exit 1
;;
esac
PRIMARY="https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/${TARGET_OS}/${KARCH}/kubectl"
FALLBACK="https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/${TARGET_OS}/${KARCH}/kubectl"
MIRROR_CN="https://mirrors.aliyun.com/kubernetes/release/${KUBECTL_VERSION}/bin/${TARGET_OS}/${KARCH}/kubectl"
echo "[install-kubectl] ${TARGET_OS}/${KARCH} ${KUBECTL_VERSION} -> ${DEST}"
# --max-time 120 限制单次下载总时长,--connect-timeout 20 限制连接阶段 // Logan.Li
if curl -fsSL --retry 3 --retry-delay 2 --connect-timeout 20 --max-time 120 "${PRIMARY}" -o "${DEST}"; then
:
elif curl -fsSL --retry 3 --retry-delay 2 --connect-timeout 20 --max-time 120 "${MIRROR_CN}" -o "${DEST}"; then
echo "[install-kubectl] 使用阿里云镜像: ${MIRROR_CN}"
elif curl -fsSL --retry 3 --retry-delay 2 --connect-timeout 20 --max-time 120 "${FALLBACK}" -o "${DEST}"; then
echo "[install-kubectl] 使用备用源: ${FALLBACK}"
else
echo "[install-kubectl] 下载失败: ${PRIMARY}" >&2
exit 1
fi
chmod +x "${DEST}"
"${DEST}" version --client=true 2>/dev/null || true