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