Skip to content

Commit 4ece0e1

Browse files
authored
feat: add install_kubectl.sh ops script (#1319)
1 parent c88f9bb commit 4ece0e1

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

scripts/install_kubectl.sh

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/bin/bash
2+
3+
KUBECTL_DEST="/usr/local/bin/kubectl"
4+
ARCH=$(dpkg --print-architecture)
5+
DRY_RUN=false
6+
OWNER_UID="root"
7+
OWNER_GID="root"
8+
INSTALL_MODE="0755"
9+
KUBE_VERSION_OVERRIDE=""
10+
11+
# --- Argument Parsing ---
12+
if [[ "$1" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
13+
KUBE_VERSION_OVERRIDE="${1#v}"
14+
shift
15+
fi
16+
17+
if [[ "$1" == "--dry-run" ]] || [[ "$1" == "-n" ]]; then
18+
DRY_RUN=true
19+
echo "--- DRY-RUN MODE ACTIVATED ---"
20+
fi
21+
22+
case "${ARCH}" in
23+
"amd64")
24+
KUBERNETES_ARCH="amd64"
25+
;;
26+
"arm64")
27+
KUBERNETES_ARCH="arm64"
28+
;;
29+
*)
30+
echo "Error: Unsupported architecture ${ARCH}. This script supports amd64 and arm64."
31+
exit 1
32+
;;
33+
esac
34+
35+
echo "Detected system architecture: ${KUBERNETES_ARCH}"
36+
37+
get_kube_version() {
38+
if ! command -v kubectl &> /dev/null; then
39+
echo "Error: 'kubectl' command not found in PATH." >&2
40+
return 1
41+
fi
42+
43+
if $DRY_RUN && [ -z "${KUBE_VERSION_OVERRIDE}" ]; then
44+
echo "1.28.5 (SIMULATED)"
45+
return 0
46+
fi
47+
48+
SERVER_VERSION=$(kubectl version --client=false --output=json 2>/dev/null | jq -r '.serverVersion.gitVersion')
49+
50+
if [ -z "${SERVER_VERSION}" ] || [ "${SERVER_VERSION}" = "null" ]; then
51+
echo "Error: Could not determine Kubernetes server version. Is KUBECONFIG set and is the cluster reachable?" >&2
52+
return 1
53+
fi
54+
55+
echo "${SERVER_VERSION#v}"
56+
}
57+
58+
echo "Attempting to fetch Kubernetes server version..."
59+
60+
if [ -n "${KUBE_VERSION_OVERRIDE}" ]; then
61+
KUBE_VERSION="${KUBE_VERSION_OVERRIDE}"
62+
echo "Target Kubernetes version OVERRIDDEN by argument: v${KUBE_VERSION}"
63+
else
64+
KUBE_VERSION=$(get_kube_version)
65+
66+
if [ $? -ne 0 ]; then
67+
echo "FATAL: Auto-detection failed. When installing kubectl for the first time or when it's not in PATH, you MUST provide the target Kubernetes version as the first command-line argument (e.g., $0 1.29.1)." >&2
68+
exit 1
69+
fi
70+
echo "Target Kubernetes version detected: v${KUBE_VERSION}"
71+
fi
72+
73+
DOWNLOAD_URL="https://dl.k8s.io/release/v${KUBE_VERSION}/bin/linux/${KUBERNETES_ARCH}/kubectl"
74+
75+
if $DRY_RUN; then
76+
echo
77+
echo "--- Dry-Run Actions to be performed ---"
78+
79+
if [ -f "${KUBECTL_DEST}" ]; then
80+
echo "DRY-RUN: Existing binary found. Permissions/Ownership would be read and preserved. File would be removed and replaced."
81+
else
82+
echo "DRY-RUN: No existing kubectl binary found. File would be installed using default permissions (${INSTALL_MODE}) and ownership (root:root)."
83+
fi
84+
85+
echo "DRY-RUN: Binary v${KUBE_VERSION} would be downloaded from: ${DOWNLOAD_URL} to /tmp/kubectl"
86+
echo "DRY-RUN: The downloaded binary would be installed to ${KUBECTL_DEST}."
87+
echo "DRY-RUN: The temporary file /tmp/kubectl would be removed."
88+
89+
echo
90+
echo "Dry-Run complete. No changes were made."
91+
else
92+
if [ -f "${KUBECTL_DEST}" ]; then
93+
echo "Existing kubectl binary found. Reading and preserving permissions/ownership for ${KUBECTL_DEST}..."
94+
95+
OWNER_UID=$(sudo stat -c "%u" "${KUBECTL_DEST}" 2>/dev/null)
96+
OWNER_GID=$(sudo stat -c "%g" "${KUBECTL_DEST}" 2>/dev/null)
97+
OLD_MODE=$(sudo stat -c "%a" "${KUBECTL_DEST}" 2>/dev/null)
98+
99+
if [ -n "$OLD_MODE" ]; then
100+
INSTALL_MODE="$OLD_MODE"
101+
echo "Preserving permissions: ${INSTALL_MODE}"
102+
fi
103+
104+
if [ "$OWNER_UID" == "0" ]; then OWNER_UID="root"; fi
105+
if [ "$OWNER_GID" == "0" ]; then OWNER_GID="root"; fi
106+
107+
if [ -n "$OWNER_UID" ] && [ -n "$OWNER_GID" ]; then
108+
echo "Preserving ownership: Owner=${OWNER_UID}, Group=${OWNER_GID}"
109+
else
110+
echo "Could not read existing ownership/permissions, defaulting to root:root and 0755."
111+
OWNER_UID="root"
112+
OWNER_GID="root"
113+
fi
114+
115+
echo "Removing existing kubectl binary at ${KUBECTL_DEST}..."
116+
sudo rm -f "${KUBECTL_DEST}"
117+
else
118+
echo "No existing kubectl binary found at ${KUBECTL_DEST}. Installing new binary with default permissions."
119+
fi
120+
121+
echo "Downloading kubectl v${KUBE_VERSION} from: ${DOWNLOAD_URL}"
122+
curl -sSL "${DOWNLOAD_URL}" -o /tmp/kubectl
123+
124+
if [ ! -s "/tmp/kubectl" ]; then
125+
echo "Error: Download failed or the binary file is empty. Check the version number (v${KUBE_VERSION}) or URL." >&2
126+
rm -f /tmp/kubectl
127+
exit 1
128+
fi
129+
130+
echo "Installing kubectl to ${KUBECTL_DEST} with mode ${INSTALL_MODE} and ownership ${OWNER_UID}:${OWNER_GID}..."
131+
132+
sudo install -o "${OWNER_UID}" -g "${OWNER_GID}" -m "${INSTALL_MODE}" /tmp/kubectl "${KUBECTL_DEST}"
133+
rm -f /tmp/kubectl
134+
135+
echo "Installation complete!"
136+
echo "New kubectl version:"
137+
${KUBECTL_DEST} version --client
138+
139+
echo ""
140+
echo "Server version check:"
141+
${KUBECTL_DEST} version --client=false
142+
fi

0 commit comments

Comments
 (0)