Skip to content

Commit 20e880c

Browse files
fix(create-kubernetes-binaries-iso-with-cilium.sh): Improvements suggested by Copilot.
cks: harden ISO build script with pipefail, --fail, and strict mode: - Replace 'set -e' with 'set -o errexit', 'set -o nounset', 'set -o pipefail'; - Add TRACE env var support for debug with 'set -o xtrace'; - Add --fail (-f) flag to curl commands in pipelines to prevent silent; - Add --fail and tar.gz integrity validation for etcd download; - Fix ARCH validation error message to list all accepted values (x86_64, amd64, aarch64, arm64). cks: fix semver comparison using sort -V instead of awk: - Replace awk numeric comparison with a sort -V based version_lt() function. The previous approach treated '1.9.0' as 1.9 and '1.18.0' as 1.18, making 1.9 > 1.18 and selecting the wrong source for kubelet.service and 10-kubeadm.conf. cks: vendor kubelet.service and 10-kubeadm.conf to reduce supply-chain risk: - Vendor kubelet.service and 10-kubeadm.conf from shapeblue/cloudstack-nonoss into the repository instead of fetching them at build time from a mutable remote branch. These files are executed with root privileges as systemd units, and pinning to a mutable branch without checksum verification posed a supply-chain risk. cks: only add Cilium Helm repo if not already configured: - Replace 'helm repo add ... || true' with an explicit check via 'helm repo list'. The previous approach suppressed all failures (including network/DNS errors), which could cause a later, less-clear failure during 'helm template'. cks: move Cilium ISO script to scripts/util/cks/ and add download progress: - Move create-kubernetes-binaries-iso-with-cilium.sh to scripts/util/cks/ alongside vendored kubelet.service and 10-kubeadm.conf; - Download kubeadm, kubelet, kubectl individually with --progress-bar for visibility during long downloads.
1 parent 9362ba8 commit 20e880c

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

scripts/util/cks/10-kubeadm.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
[Service]
3+
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
4+
Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
5+
# This is a file that "kubeadm init" and "kubeadm join" generate at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
6+
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
7+
# This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
8+
# the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
9+
EnvironmentFile=-/etc/default/kubelet
10+
ExecStart=
11+
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS

scripts/util/create-kubernetes-binaries-iso-with-cilium.sh renamed to scripts/util/cks/create-kubernetes-binaries-iso-with-cilium.sh

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
23
# Licensed to the Apache Software Foundation (ASF) under one
34
# or more contributor license agreements. See the NOTICE file
45
# distributed with this work for additional information
@@ -16,7 +17,13 @@
1617
# specific language governing permissions and limitations
1718
# under the License.
1819

19-
set -e
20+
set -o errexit
21+
set -o nounset
22+
set -o pipefail
23+
24+
if [[ "${TRACE-0}" == "1" ]]; then
25+
set -o xtrace
26+
fi
2027

2128
if [ $# -lt 8 ]; then
2229
echo "============================================================================================================"
@@ -56,12 +63,19 @@ elif [ "${6}" = "aarch64" ] || [ "${6}" = "arm64" ]; then
5663
ARCH="arm64"
5764
ARCH_SUFFIX="aarch64"
5865
else
59-
echo "ERROR: ARCH must be 'x86_64' or 'aarch64'."
66+
echo "ERROR: ARCH must be one of: x86_64, amd64, aarch64, or arm64."
6067
exit 1
6168
fi
6269

6370
RELEASE="v${2}"
64-
VAL="1.18.0"
71+
MIN_UPSTREAM_VERSION="1.18.0"
72+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
73+
74+
# version_lt returns 0 (true) if $1 < $2 using semver-aware comparison
75+
version_lt() {
76+
[ "$(printf '%s\n%s' "$1" "$2" | sort -V | head -n1)" = "$1" ] && [ "$1" != "$2" ]
77+
}
78+
6579
output_dir="${1}"
6680
start_dir="$PWD"
6781
iso_dir=$(mktemp -d)
@@ -95,27 +109,30 @@ echo "Downloading Kubernetes tools ${RELEASE}..."
95109
k8s_dir="${working_dir}/k8s"
96110
mkdir -p "${k8s_dir}"
97111
cd "${k8s_dir}"
98-
curl -sS -L --remote-name-all https://dl.k8s.io/release/"${RELEASE}"/bin/linux/${ARCH}/{kubeadm,kubelet,kubectl}
112+
for binary in kubeadm kubelet kubectl; do
113+
echo " Downloading ${binary}..."
114+
curl --progress-bar -fL "https://dl.k8s.io/release/${RELEASE}/bin/linux/${ARCH}/${binary}" -o "${binary}"
115+
done
99116
kubeadm_file_permissions=$(stat --format '%a' kubeadm)
100117
chmod +x kubeadm
101118

102119
echo "Downloading kubelet.service ${RELEASE}..."
103120
cd "${start_dir}"
104121
kubelet_service_file="${working_dir}/kubelet.service"
105122
touch "${kubelet_service_file}"
106-
if [[ $(echo "${2} $VAL" | awk '{print ($1 < $2)}') == 1 ]]; then
107-
curl -sSL "https://raw.githubusercontent.com/kubernetes/kubernetes/${RELEASE}/build/debs/kubelet.service" | sed "s:/usr/bin:/opt/bin:g" > "${kubelet_service_file}"
123+
if version_lt "${2}" "${MIN_UPSTREAM_VERSION}"; then
124+
curl -sSfL "https://raw.githubusercontent.com/kubernetes/kubernetes/${RELEASE}/build/debs/kubelet.service" | sed "s:/usr/bin:/opt/bin:g" > "${kubelet_service_file}"
108125
else
109-
curl -sSL "https://raw.githubusercontent.com/shapeblue/cloudstack-nonoss/main/cks/kubelet.service" | sed "s:/usr/bin:/opt/bin:g" > "${kubelet_service_file}"
126+
sed "s:/usr/bin:/opt/bin:g" "${SCRIPT_DIR}/kubelet.service" > "${kubelet_service_file}"
110127
fi
111128

112129
echo "Downloading 10-kubeadm.conf ${RELEASE}..."
113130
kubeadm_conf_file="${working_dir}/10-kubeadm.conf"
114131
touch "${kubeadm_conf_file}"
115-
if [[ $(echo "${2} $VAL" | awk '{print ($1 < $2)}') == 1 ]]; then
116-
curl -sSL "https://raw.githubusercontent.com/kubernetes/kubernetes/${RELEASE}/build/debs/10-kubeadm.conf" | sed "s:/usr/bin:/opt/bin:g" > "${kubeadm_conf_file}"
132+
if version_lt "${2}" "${MIN_UPSTREAM_VERSION}"; then
133+
curl -sSfL "https://raw.githubusercontent.com/kubernetes/kubernetes/${RELEASE}/build/debs/10-kubeadm.conf" | sed "s:/usr/bin:/opt/bin:g" > "${kubeadm_conf_file}"
117134
else
118-
curl -sSL "https://raw.githubusercontent.com/shapeblue/cloudstack-nonoss/main/cks/10-kubeadm.conf" | sed "s:/usr/bin:/opt/bin:g" > "${kubeadm_conf_file}"
135+
sed "s:/usr/bin:/opt/bin:g" "${SCRIPT_DIR}/10-kubeadm.conf" > "${kubeadm_conf_file}"
119136
fi
120137

121138
AUTOSCALER_URL="https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/cloudstack/examples/cluster-autoscaler-standard.yaml"
@@ -139,7 +156,11 @@ if ! command -v helm > /dev/null 2>&1; then
139156
exit 1
140157
fi
141158

142-
helm repo add cilium https://helm.cilium.io/ > /dev/null 2>&1 || true
159+
# Add the Cilium Helm repository only if it is not already configured
160+
if ! helm repo list 2>/dev/null | awk 'NR>1 {print $1}' | grep -qx "cilium"; then
161+
helm repo add cilium https://helm.cilium.io/ > /dev/null 2>&1
162+
fi
163+
143164
echo "Updating Helm repositories..."
144165
helm repo update
145166
echo "Generating Cilium manifest with Helm..."
@@ -260,6 +281,12 @@ etcd_dir="${working_dir}/etcd"
260281
mkdir -p "${etcd_dir}"
261282
ETCD_VERSION=v${7}
262283
echo "Downloading etcd ${ETCD_VERSION}..."
263-
curl -sS -L "https://github.com/etcd-io/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-${ARCH}.tar.gz" -o "${etcd_dir}/etcd-linux-${ARCH}.tar.gz"
284+
curl -sSfL "https://github.com/etcd-io/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-${ARCH}.tar.gz" -o "${etcd_dir}/etcd-linux-${ARCH}.tar.gz"
285+
286+
# Validate that the downloaded etcd archive is a valid tar.gz
287+
if ! tar -tzf "${etcd_dir}/etcd-linux-${ARCH}.tar.gz" > /dev/null; then
288+
echo "ERROR: Downloaded etcd archive is invalid or corrupted."
289+
exit 1
290+
fi
264291

265292
mkisofs -o "${output_dir}/${build_name}" -J -R -l "${iso_dir}"

scripts/util/cks/kubelet.service

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[Unit]
2+
Description=kubelet: The Kubernetes Node Agent
3+
Documentation=http://kubernetes.io/docs/
4+
5+
[Service]
6+
ExecStart=/usr/bin/kubelet
7+
Restart=always
8+
StartLimitInterval=0
9+
RestartSec=10
10+
11+
[Install]
12+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)