Skip to content

Commit e781d7b

Browse files
committed
fix(install): fix bugs and optimize install script
- add CHANNEL default value to prevent silent testing channel usage - fix install_helm echo message (crun -> helm) - fix kata runtime comment (Dragonball -> CLH) - add network detection fallback for air-gapped environments - move mkdir into corresponding functions - remove hardcoded vm.nr_hugepages - remove --embedded-registry from agent node - fix KUBECONFIG setup after fresh k3s install - fix exit trap sleep 3 (only on error) - add command whitelist validation - add chmod 600 on temp helm values files - fix unquoted sed patterns and double space
1 parent 3f5989a commit e781d7b

1 file changed

Lines changed: 46 additions & 21 deletions

File tree

install.sh

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set -eo pipefail
33
shopt -s expand_aliases
44

55
# default vars
6+
CHANNEL="${CHANNEL:-testing}"
67
GATEWAY_CLASS="istio"
78
CLUSTER_CIDR=${CLUSTER_CIDR:-"10.42.0.0/16"}
89
SERVICE_CIDR=${SERVICE_CIDR:-"10.43.0.0/16"}
@@ -12,10 +13,8 @@ PROXY_CHARTS_URL=oci://registry.drycc.cc/$([ "$CHANNEL" == "stable" ] && echo ch
1213
DRYCC_CHARTS_URL=oci://registry.drycc.cc/drycc/$([ "$CHANNEL" == "stable" ] && echo charts || echo charts-testing)
1314
CONTAINERD_RUNTIMES="${CONTAINERD_RUNTIMES:-runc}"
1415
CONTAINERD_CONFIG_PATH="${CONTAINERD_CONFIG_PATH:-/var/lib/rancher/k3s/agent/etc/containerd}"
15-
mkdir -p "${CONTAINERD_CONFIG_PATH}"
1616
CONTAINERD_CONFIG_FILE="${CONTAINERD_CONFIG_PATH}/config.toml.tmpl"
1717
REGISTRY_CONFIG_PATH="${REGISTRY_CONFIG_PATH:-/etc/rancher/k3s/}"
18-
mkdir -p "${REGISTRY_CONFIG_PATH}"
1918
REGISTRY_CONFIG_FILE="${REGISTRY_CONFIG_PATH}/registries.yaml"
2019

2120
# initArch discovers the architecture for this system.
@@ -34,9 +33,12 @@ init_arch() {
3433
}
3534

3635
function clean_before_exit {
37-
# delay before exiting, so stdout/stderr flushes through the logging system
36+
local exit_code=$?
3837
rm -rf /tmp/drycc-values.yaml
39-
sleep 3
38+
# delay before exiting on error, so stdout/stderr flushes through the logging system
39+
if [[ $exit_code -ne 0 ]]; then
40+
sleep 3
41+
fi
4042
}
4143
trap clean_before_exit EXIT
4244
init_arch
@@ -101,7 +103,7 @@ function install_helm {
101103
tar -zxvf "${tar_name}"
102104
mv "linux-${ARCH}/helm" /usr/local/bin/helm
103105
rm -rf "${tar_name}" "linux-${ARCH}"
104-
echo -e "\\033[32m---> crun runtime install completed!\\033[0m"
106+
echo -e "\\033[32m---> helm install completed!\\033[0m"
105107
}
106108

107109
# helm_upgrade wraps "helm upgrade --install" with retry logic and a default timeout.
@@ -158,7 +160,6 @@ fs.file-max = 2097152
158160
fs.inotify.max_user_instances = 65535
159161
fs.inotify.max_user_watches = 1048576
160162
net.core.rmem_max = 2500000
161-
vm.nr_hugepages = 1024
162163
EOF
163164
sysctl --system 2>/dev/null || echo -e "\\033[33m---> Warning: sysctl --system failed, skipping (container environment?)\\033[0m"
164165

@@ -186,9 +187,8 @@ function install_crun_runtime {
186187
echo -e "\\033[32m---> crun runtime install completed!\\033[0m"
187188
}
188189

189-
# install_kata_runtime downloads and installs the Kata Containers runtime with Dragonball VMM.
190-
# The Dragonball configuration is used instead of the default QEMU, providing lower
191-
# memory overhead (~130Mi vs 160Mi) and faster startup (~100ms vs 500ms).
190+
# install_kata_runtime downloads and installs the Kata Containers runtime with Cloud Hypervisor VMM.
191+
# CLH (Cloud Hypervisor) is used instead of QEMU, providing lower memory overhead and faster startup.
192192
# sandbox_cgroup_only is set to true for complete resource tracking and cgroups v2 support.
193193
# Requires PodOverhead configured in the RuntimeClass (see install_k3s_server).
194194
function install_kata_runtime {
@@ -205,13 +205,18 @@ function install_kata_runtime {
205205

206206
curl -fL "${kata_download_url}" -o ${kata_package}
207207
tar -I zstd -xf ${kata_package} -C /
208-
cp /opt/kata/share/defaults/kata-containers/runtime-rs/configuration-dragonball.toml \
208+
cp /opt/kata/share/defaults/kata-containers/runtime-rs/configuration-clh-runtime-rs.toml \
209+
/opt/kata/share/defaults/kata-containers/runtime-rs/configuration.toml
210+
sed -i 's/sandbox_cgroup_only=false/sandbox_cgroup_only=true/g' \
211+
/opt/kata/share/defaults/kata-containers/runtime-rs/configuration.toml
212+
sed -i 's/static_sandbox_resource_mgmt=false/static_sandbox_resource_mgmt=true/g' \
213+
/opt/kata/share/defaults/kata-containers/runtime-rs/configuration.toml
214+
sed -i 's/^default_vcpus = .*/default_vcpus = 1/' \
209215
/opt/kata/share/defaults/kata-containers/runtime-rs/configuration.toml
210-
sed -i s/sandbox_cgroup_only=false/sandbox_cgroup_only=true/g \
216+
sed -i 's/^default_maxvcpus = .*/default_maxvcpus = 32/' \
211217
/opt/kata/share/defaults/kata-containers/runtime-rs/configuration.toml
212-
ln -sf /opt/kata/bin/containerd-shim-kata-v2 /usr/local/bin/containerd-shim-kata-v2
213-
ln -sf /opt/kata/bin/kata-collect-data.sh /usr/local/bin/kata-collect-data.sh
214-
ln -sf /opt/kata/bin/kata-runtime /usr/local/bin/kata-runtime
218+
ln -sf /opt/kata/runtime-rs/bin/containerd-shim-kata-v2 /usr/local/bin/containerd-shim-kata-v2
219+
ln -sf /opt/kata/runtime-rs/bin/kata-runtime /usr/local/bin/kata-runtime
215220
rm -rf ${kata_package}
216221
echo -e "\\033[32m---> Kata runtime install completed!\\033[0m"
217222
}
@@ -221,6 +226,7 @@ function install_kata_runtime {
221226
# Generates the containerd config template and installs the selected runtimes.
222227
function install_runtime {
223228
readarray -d , -t containerd_runtimes <<<"$CONTAINERD_RUNTIMES"
229+
mkdir -p "${CONTAINERD_CONFIG_PATH}"
224230
if [[ "$CONTAINERD_RUNTIMES" =~ "crun" ]]; then
225231
containerd_default_runtime="crun"
226232
else
@@ -271,6 +277,7 @@ EOF
271277
# Only applies when INSTALL_DRYCC_MIRROR is set to "cn".
272278
function configure_registry {
273279
if [[ "${INSTALL_DRYCC_MIRROR}" == "cn" ]]; then
280+
mkdir -p "${REGISTRY_CONFIG_PATH}"
274281
cat << EOF > "${REGISTRY_CONFIG_FILE}"
275282
mirrors:
276283
docker.io:
@@ -315,6 +322,12 @@ EOF
315322
echo -e "\\033[32m---> Kubectl defaults configured (server-side apply enabled)\\033[0m"
316323
}
317324

325+
function configure_kubeconfig {
326+
if [[ -f /etc/rancher/k3s/k3s.yaml ]] ; then
327+
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
328+
fi
329+
}
330+
318331
# configure_k3s_mirrors selects the k3s install URL based on INSTALL_DRYCC_MIRROR.
319332
function configure_k3s_mirrors {
320333
echo -e "\\033[32m---> Start configuring k3s mirrors\\033[0m"
@@ -339,7 +352,7 @@ function install_k3s_server {
339352
configure_kubectl
340353
configure_registry
341354
configure_k3s_mirrors
342-
INSTALL_K3S_EXEC="server ${INSTALL_K3S_EXEC} --embedded-registry --flannel-backend=none --disable-network-policy --disable=traefik --disable=servicelb --disable-kube-proxy --cluster-cidr=${CLUSTER_CIDR} --service-cidr=${SERVICE_CIDR}"
355+
INSTALL_K3S_EXEC="server ${INSTALL_K3S_EXEC} --embedded-registry --flannel-backend=none --disable-network-policy --disable=traefik --disable=servicelb --disable-kube-proxy --cluster-cidr=${CLUSTER_CIDR} --service-cidr=${SERVICE_CIDR}"
343356
if [[ -n "${K3S_DATA_DIR}" ]] ; then
344357
INSTALL_K3S_EXEC="$INSTALL_K3S_EXEC --data-dir=${K3S_DATA_DIR}/rancher/k3s"
345358
fi
@@ -348,6 +361,8 @@ function install_k3s_server {
348361
fi
349362
curl -sfL "${k3s_install_url}" |INSTALL_K3S_EXEC="$INSTALL_K3S_EXEC" sh -s -
350363

364+
configure_kubeconfig
365+
351366
readarray -d , -t containerd_runtimes <<<"$CONTAINERD_RUNTIMES"
352367
for (( n=0; n < ${#containerd_runtimes[*]}; n++ ))
353368
do
@@ -383,9 +398,10 @@ function install_k3s_agent {
383398
configure_registry
384399
configure_k3s_mirrors
385400
if [[ -n "${K3S_DATA_DIR}" ]] ; then
386-
INSTALL_K3S_EXEC="$INSTALL_K3S_EXEC --embedded-registry --data-dir=${K3S_DATA_DIR}/rancher/k3s"
401+
INSTALL_K3S_EXEC="$INSTALL_K3S_EXEC --data-dir=${K3S_DATA_DIR}/rancher/k3s"
387402
fi
388403
curl -sfL "${k3s_install_url}" |INSTALL_K3S_EXEC="$INSTALL_K3S_EXEC" sh -s -
404+
configure_kubeconfig
389405
}
390406

391407
# install_longhorn deploys Longhorn distributed block storage via Helm.
@@ -450,7 +466,10 @@ function check_metallb {
450466
function install_network() {
451467
options=${1:-""}
452468
echo -e "\\033[32m---> Start install network...\\033[0m"
453-
kubernetes_service_host=(`ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p'`)
469+
kubernetes_service_host=$(ip -o route get 8.8.8.8 2>/dev/null | sed -n 's/.*src \([0-9.]\+\).*/\1/p')
470+
if [[ -z "$kubernetes_service_host" ]]; then
471+
kubernetes_service_host=$(ip -o route get default 2>/dev/null | sed -n 's/.*src \([0-9.]\+\).*/\1/p')
472+
fi
454473
helm_upgrade cilium $PROXY_CHARTS_URL/cilium \
455474
--set endpointHealthChecking.enabled=false \
456475
--set healthChecking=false \
@@ -692,6 +711,7 @@ acme:
692711
keyID: ${ACME_EAB_KEY_ID:-""}
693712
keySecret: ${ACME_EAB_KEY_SECRET:-""}
694713
EOF
714+
chmod 600 "/tmp/drycc-values.yaml"
695715

696716
if [[ "${INSTALL_DRYCC_MIRROR}" == "cn" ]] ; then
697717
cat << EOF > "/tmp/drycc-mirror-values.yaml"
@@ -714,6 +734,7 @@ imagebuilder:
714734
short-name-mode="permissive"
715735
EOF
716736
fi
737+
chmod 600 "/tmp/drycc-mirror-values.yaml"
717738
if [[ -z "${VICTORIAMETRICS_CONFIG_FILE}" ]] ; then
718739
VICTORIAMETRICS_CONFIG_FILE="/tmp/drycc-victoriametrics-values.yaml"
719740
cat << EOF > "${VICTORIAMETRICS_CONFIG_FILE}"
@@ -761,11 +782,9 @@ function upgrade {
761782
echo -e "\\033[32m---> Upgrade complete, enjoy life...\\033[0m"
762783
}
763784

764-
if [[ -f /etc/rancher/k3s/k3s.yaml ]] ; then
765-
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
766-
fi
785+
configure_kubeconfig
767786

768-
if [[ -z "$@" ]] ; then
787+
if [[ $# -eq 0 ]] ; then
769788
check_drycc
770789
check_metallb
771790
install_k3s_server
@@ -776,6 +795,12 @@ if [[ -z "$@" ]] ; then
776795
install_drycc
777796
echo -e "\\033[32m---> Installation complete, enjoy life...\\033[0m"
778797
else
798+
for command in "$@"; do
799+
if ! declare -f "$command" > /dev/null 2>&1; then
800+
echo -e "\\033[31m---> Error: unknown command '$command'\\033[0m"
801+
exit 1
802+
fi
803+
done
779804
for command in "$@"
780805
do
781806
$command

0 commit comments

Comments
 (0)