From 63c2ae5cded4035a570c5bf962a4bce94bd663a2 Mon Sep 17 00:00:00 2001 From: Brad House Date: Fri, 6 Nov 2026 21:17:59 +0000 Subject: [PATCH] CKS: fix the add-on scripts' download and option handling deploy-csi-driver falls back to downloading the CSI manifests when the node has no copy from the binaries ISO. Its download loop applied both files and exited after fetching only the first: for url in "${CSI_URLS[@]}"; do curl -sSL ${url} -o ${TARGET_DIR}/${filename} ... else kubectl apply -f /opt/csi/snapshot-crds.yaml kubectl apply -f /opt/csi/manifest.yaml # never downloaded exit 0 so manifest.yaml was never fetched and the apply always failed. The snapshot-crds.yaml it did leave behind then sent every later run down the "files already present" branch, which failed the same way. - deploy-csi-driver: download every file, then apply them as when the binaries ISO provides them. Treat the files as present only when both are there, so a node left with only snapshot-crds.yaml downloads again. The script also exited 0 as soon as a cloudstack-csi-controller pod existed. CloudStack runs it once more when it fails, so an apply that failed partway (the controller created, other objects not) passed on that second run. The v3.0.0 manifest it downloads makes this happen: a line ends with its document separator ("rbac.authorization.k8s.io---"), which merges the snapshotter ClusterRoleBinding into the CSIDriver, and the apply of the CSIDriver fails. - Check that every object of the two manifests exists instead, and apply again when one is missing. Wait for the snapshot CRDs to be established instead of sleeping. - Split the glued separator before the check, so the manifest applies and the binding is checked; it changes nothing in a manifest without the defect. - Check the downloaded files against their sha256, so the fallback always applies the same v3.0.0 manifests. deploy-csi-driver, deploy-provider and autoscale-kube-cluster also called curl without -f, so an HTTP error (a moved or missing URL) was saved as the manifest and applied, and the next run applied the saved file again instead of downloading. Their "$? -ne 0" checks could not report anything either: the scripts run under bash -e, so a failing curl ended the script before the check. - Use "curl -fsSL" inside the if, and download to a per-run temporary file (removed by an EXIT trap) that is moved into place only once it is complete, so a failed or interrupted download leaves no file that a later run would apply. autoscale-kube-cluster listed its disable option as "-d | --enable", so "--disable" was rejected as an unknown option ("-d" worked). Run with neither option, the unquoted [ $ENABLE == "true" ] failed with "unary operator expected" and fell through to deleting the autoscaler. Disable also failed when there was no autoscaler deployment to delete, so autoscaling could not be turned off after a failed enable. - Accept "--disable", quote the test, and refuse to run with neither -e nor -d. CloudStack always passes one of them, so its calls are unaffected. - Delete the deployment with --ignore-not-found. Signed-off-by: Brad House --- .../resources/script/autoscale-kube-cluster | 22 ++++--- .../main/resources/script/deploy-csi-driver | 60 ++++++++++++------- .../src/main/resources/script/deploy-provider | 10 ++-- 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/plugins/integrations/kubernetes-service/src/main/resources/script/autoscale-kube-cluster b/plugins/integrations/kubernetes-service/src/main/resources/script/autoscale-kube-cluster index ac3fd8eab4ce..692815f3d09f 100755 --- a/plugins/integrations/kubernetes-service/src/main/resources/script/autoscale-kube-cluster +++ b/plugins/integrations/kubernetes-service/src/main/resources/script/autoscale-kube-cluster @@ -51,7 +51,7 @@ while [ -n "$1" ]; do ENABLE="true" shift 1 ;; - -d | --enable) + -d | --disable) ENABLE="false" shift 1 ;; @@ -69,7 +69,11 @@ while [ -n "$1" ]; do ;; esac done -if [ $ENABLE == "true" ] ; then +if [ -z "$ENABLE" ]; then + echo "ERROR: one of -e/--enable or -d/--disable is required. -h or --help for help" + exit 1 +fi +if [ "$ENABLE" == "true" ] ; then if [ -e /opt/autoscaler/autoscaler_tmpl.yaml ]; then sed -e "s//$ID/g" -e "s//$MINSIZE/g" -e "s//$MAXSIZE/g" /opt/autoscaler/autoscaler_tmpl.yaml > /opt/autoscaler/autoscaler_now.yaml /opt/bin/kubectl apply -f /opt/autoscaler/autoscaler_now.yaml @@ -78,16 +82,16 @@ if [ $ENABLE == "true" ] ; then mkdir -p /opt/autoscaler AUTOSCALER_URL="https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/cloudstack/examples/cluster-autoscaler-standard.yaml" autoscaler_conf_file="/opt/autoscaler/autoscaler_tmpl.yaml" - curl -sSL ${AUTOSCALER_URL} -o ${autoscaler_conf_file} - if [ $? -ne 0 ]; then + trap 'rm -f "$autoscaler_conf_file.tmp.$$"' EXIT + if ! curl -fsSL "$AUTOSCALER_URL" -o "$autoscaler_conf_file.tmp.$$"; then echo "Unable to connect to the internet to download the autoscaler deployment and image" exit 1 - else - sed -e "s//$ID/g" -e "s//$MINSIZE/g" -e "s//$MAXSIZE/g" /opt/autoscaler/autoscaler_tmpl.yaml > /opt/autoscaler/autoscaler_now.yaml - /opt/bin/kubectl apply -f /opt/autoscaler/autoscaler_now.yaml - exit 0 fi + mv -f "$autoscaler_conf_file.tmp.$$" "$autoscaler_conf_file" + sed -e "s//$ID/g" -e "s//$MINSIZE/g" -e "s//$MAXSIZE/g" /opt/autoscaler/autoscaler_tmpl.yaml > /opt/autoscaler/autoscaler_now.yaml + /opt/bin/kubectl apply -f /opt/autoscaler/autoscaler_now.yaml + exit 0 fi else - /opt/bin/kubectl delete deployment -n kube-system cluster-autoscaler + /opt/bin/kubectl delete deployment -n kube-system cluster-autoscaler --ignore-not-found fi diff --git a/plugins/integrations/kubernetes-service/src/main/resources/script/deploy-csi-driver b/plugins/integrations/kubernetes-service/src/main/resources/script/deploy-csi-driver index 69684fa32bd5..16757adabfbe 100644 --- a/plugins/integrations/kubernetes-service/src/main/resources/script/deploy-csi-driver +++ b/plugins/integrations/kubernetes-service/src/main/resources/script/deploy-csi-driver @@ -16,31 +16,49 @@ # specific language governing permissions and limitations # under the License. -(/opt/bin/kubectl get pods -A | grep cloudstack-csi-controller) && exit 0 +KUBECTL=/opt/bin/kubectl +TARGET_DIR="/opt/csi" +CRDS="$TARGET_DIR/snapshot-crds.yaml" +MANIFEST="$TARGET_DIR/manifest.yaml" -if [ -e /opt/csi/snapshot-crds.yaml ]; then - /opt/bin/kubectl apply -f /opt/csi/snapshot-crds.yaml - sleep 5 - /opt/bin/kubectl apply -f /opt/csi/manifest.yaml - exit 0 -else - TARGET_DIR="/opt/csi" - mkdir -p "$TARGET_DIR" - CSI_URLS=( - "https://github.com/cloudstack/cloudstack-csi-driver/releases/download/v3.0.0/snapshot-crds.yaml" - "https://github.com/cloudstack/cloudstack-csi-driver/releases/download/v3.0.0/manifest.yaml" - ) - for url in "${CSI_URLS[@]}"; do - filename=$(basename "$url") +# Used only when the binaries ISO had no CSI manifests +CSI_URL="https://github.com/cloudstack/cloudstack-csi-driver/releases/download/v3.0.0" +CSI_FILES=( + "snapshot-crds.yaml 52cadf0a145f4d57e6076f82dc8d657dbce71d07d92cc9ba9a518beeef753f8f" + "manifest.yaml 9b6d41d278f19642b4c945795d362214fb590b9979bf1a616a286dbceef98339" +) - curl -sSL ${url} -o ${TARGET_DIR}/${filename} - if [ $? -ne 0 ]; then +if [ ! -e "$CRDS" ] || [ ! -e "$MANIFEST" ]; then + echo "Downloading the CloudStack CSI driver manifests" + mkdir -p "$TARGET_DIR" + trap 'rm -f "$TARGET_DIR"/*.tmp.$$' EXIT + for file in "${CSI_FILES[@]}"; do + read -r filename sha256 <<< "$file" + if ! curl -fsSL --connect-timeout 10 --max-time 15 "$CSI_URL/$filename" -o "$TARGET_DIR/$filename.tmp.$$"; then echo "Unable to connect to the internet to download the relevant files to install and setup CloudStack CSI driver" exit 1 - else - /opt/bin/kubectl apply -f /opt/csi/snapshot-crds.yaml - /opt/bin/kubectl apply -f /opt/csi/manifest.yaml - exit 0 fi + if ! echo "$sha256 $TARGET_DIR/$filename.tmp.$$" | sha256sum -c --status; then + echo "$CSI_URL/$filename does not have the expected checksum $sha256" + exit 1 + fi + done + for file in "${CSI_FILES[@]}"; do + read -r filename _ <<< "$file" + mv -f "$TARGET_DIR/$filename.tmp.$$" "$TARGET_DIR/$filename" done fi + +# The v3.0.0 manifest ends a line with its document separator, which merges two documents +sed -i '/^[[:space:]]*#/!s/^\(.*[^-[:space:]]\)---$/\1\n---/' "$MANIFEST" + +# Check every object, not only the controller: a failed apply leaves the rest in place, and the retry must apply again +echo "Checking the CloudStack CSI driver objects" +if $KUBECTL get --request-timeout=20s -f "$CRDS" -f "$MANIFEST" >/dev/null 2>&1; then + echo "CloudStack CSI driver already deployed" + exit 0 +fi + +$KUBECTL apply --request-timeout=20s -f "$CRDS" +$KUBECTL wait --for condition=established --timeout=20s -f "$CRDS" +$KUBECTL apply --request-timeout=20s -f "$MANIFEST" diff --git a/plugins/integrations/kubernetes-service/src/main/resources/script/deploy-provider b/plugins/integrations/kubernetes-service/src/main/resources/script/deploy-provider index ce71e21072b6..df884eba7d46 100755 --- a/plugins/integrations/kubernetes-service/src/main/resources/script/deploy-provider +++ b/plugins/integrations/kubernetes-service/src/main/resources/script/deploy-provider @@ -25,12 +25,12 @@ else mkdir -p /opt/provider PROVIDER_URL="https://raw.githubusercontent.com/apache/cloudstack-kubernetes-provider/main/deployment.yaml" provider_conf_file="/opt/provider/provider.yaml" - curl -sSL ${PROVIDER_URL} -o ${provider_conf_file} - if [ $? -ne 0 ]; then + trap 'rm -f "$provider_conf_file.tmp.$$"' EXIT + if ! curl -fsSL "$PROVIDER_URL" -o "$provider_conf_file.tmp.$$"; then echo "Unable to connect to the internet to download the provider deployment and image" exit 1 - else - /opt/bin/kubectl apply -f /opt/provider/provider.yaml - exit 0 fi + mv -f "$provider_conf_file.tmp.$$" "$provider_conf_file" + /opt/bin/kubectl apply -f /opt/provider/provider.yaml + exit 0 fi