| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | |
| 16 | set +eux |
| 17 | |
| 18 | function install_k8s_storageclass() { |
| 19 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 20 | # Openebs versions can be found here: https://github.com/openebs/openebs/releases |
| 21 | OPENEBS_VERSION="3.7.0" |
| 22 | echo "Installing OpenEBS" |
| 23 | helm repo add openebs https://openebs.github.io/charts |
| 24 | helm repo update |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 25 | helm upgrade --install --create-namespace --namespace openebs openebs openebs/openebs --version ${OPENEBS_VERSION} |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 26 | helm ls -n openebs |
| 27 | local storageclass_timeout=400 |
| 28 | local counter=0 |
| 29 | local storageclass_ready="" |
| 30 | echo "Waiting for storageclass" |
| 31 | while (( counter < storageclass_timeout )) |
| 32 | do |
| 33 | kubectl get storageclass openebs-hostpath &> /dev/null |
| 34 | |
| 35 | if [ $? -eq 0 ] ; then |
| 36 | echo "Storageclass available" |
| 37 | storageclass_ready="y" |
| 38 | break |
| 39 | else |
| 40 | counter=$((counter + 15)) |
| 41 | sleep 15 |
| 42 | fi |
| 43 | done |
| 44 | [ -n "$storageclass_ready" ] || FATAL_TRACK k8scluster "Storageclass not ready after $storageclass_timeout seconds. Cannot install openebs" |
| 45 | kubectl patch storageclass openebs-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}' |
| 46 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 47 | } |
| 48 | |
| 49 | #installs metallb from helm |
| 50 | function install_helm_metallb() { |
| 51 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 52 | echo "Installing MetalLB" |
| 53 | METALLB_VERSION="0.13.10" |
| 54 | helm repo add metallb https://metallb.github.io/metallb |
| 55 | helm repo update |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 56 | # kubectl create namespace metallb-system |
| 57 | # kubectl label namespaces metallb-system pod-security.kubernetes.io/enforce=privileged |
| 58 | # kubectl label namespaces metallb-system pod-security.kubernetes.io/audit=privileged |
| 59 | # kubectl label namespaces metallb-system pod-security.kubernetes.io/warn=privileged |
| 60 | helm upgrade --install --create-namespace --namespace metallb-system metallb metallb/metallb --version ${METALLB_VERSION} |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 61 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 62 | } |
| 63 | |
| 64 | function configure_ipaddresspool_metallb() { |
| 65 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 66 | echo "Creating IP address pool manifest: ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml" |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 67 | [ ! -d "$OSM_CLUSTER_WORK_DIR" ] && sudo mkdir -p $OSM_CLUSTER_WORK_DIR |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 68 | METALLB_IP_RANGE="$DEFAULT_IP/32" |
| 69 | echo "apiVersion: metallb.io/v1beta1 |
| 70 | kind: IPAddressPool |
| 71 | metadata: |
| 72 | name: first-pool |
| 73 | namespace: metallb-system |
| 74 | spec: |
| 75 | addresses: |
| 76 | - ${METALLB_IP_RANGE}" | sudo tee -a ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml |
| 77 | echo "Applying IP address pool manifest: kubectl apply -f ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml" |
| 78 | kubectl apply -f ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml || FATAL_TRACK k8scluster "Cannot create IP address Pool in MetalLB" |
| 79 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 80 | } |
| 81 | |
| 82 | #installs cert-manager |
| 83 | function install_helm_certmanager() { |
| 84 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 85 | echo "Installing cert-manager" |
| 86 | CERTMANAGER_VERSION="v1.9.1" |
| 87 | helm repo add jetstack https://charts.jetstack.io |
| 88 | helm repo update |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 89 | helm upgrade --install cert-manager --create-namespace --namespace cert-manager jetstack/cert-manager \ |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 90 | --version ${CERTMANAGER_VERSION} --set installCRDs=true --set prometheus.enabled=false \ |
| 91 | --set clusterResourceNamespace=osm \ |
| 92 | --set extraArgs="{--enable-certificate-owner-ref=true}" |
| 93 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 94 | } |
| 95 | |
| garciadeblas | 18582e9 | 2024-05-21 12:13:50 +0200 | [diff] [blame] | 96 | #installs nginx |
| 97 | function install_helm_nginx() { |
| 98 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 99 | echo "Installing nginx" |
| 100 | NGINX_VERSION="4.10.0" |
| 101 | ANNOTATIONS='--set controller.service.annotations."service\.beta\.kubernetes\.io/azure-load-balancer-health-probe-request-path"=/healthz' |
| garciadeblas | 18582e9 | 2024-05-21 12:13:50 +0200 | [diff] [blame] | 102 | helm upgrade --install ingress-nginx ingress-nginx \ |
| 103 | --repo https://kubernetes.github.io/ingress-nginx --version ${NGINX_VERSION} \ |
| 104 | --namespace ingress-nginx --create-namespace ${ANNOTATIONS} |
| 105 | # Wait until ready |
| 106 | kubectl wait --namespace ingress-nginx \ |
| 107 | --for=condition=ready pod \ |
| 108 | --selector=app.kubernetes.io/component=controller \ |
| 109 | --timeout=120s |
| 110 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 111 | } |
| 112 | |
| 113 | #checks openebs, metallb and cert-manager readiness |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 114 | function check_for_readiness() { |
| 115 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 116 | # Default input values |
| 117 | sampling_period=2 # seconds |
| 118 | time_for_readiness=20 # seconds ready |
| 119 | time_for_failure=200 # seconds broken |
| 120 | OPENEBS_NAMESPACE=openebs |
| 121 | METALLB_NAMESPACE=metallb-system |
| 122 | CERTMANAGER_NAMESPACE=cert-manager |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 123 | |
| 124 | # Equivalent number of samples |
| 125 | oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready |
| 126 | failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken |
| 127 | failures_in_a_row=0 |
| 128 | oks_in_a_row=0 |
| 129 | |
| 130 | #################################################################################### |
| 131 | # Loop to check system readiness |
| 132 | #################################################################################### |
| 133 | while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]] |
| 134 | do |
| 135 | # State of OpenEBS |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 136 | if [ -n "${INSTALL_STORAGECLASS}" ]; then |
| 137 | OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1) |
| 138 | OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}') |
| 139 | OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}') |
| 140 | COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l) |
| 141 | COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l) |
| 142 | fi |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 143 | |
| 144 | # State of MetalLB |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 145 | if [ -n "${INSTALL_METALLB}" ]; then |
| 146 | METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1) |
| 147 | METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="4/4" {printf ("%s\t%s\t\n", $1, $2)}') |
| 148 | METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="4/4" {printf ("%s\t%s\t\n", $1, $2)}') |
| 149 | COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l) |
| 150 | COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l) |
| 151 | fi |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 152 | |
| 153 | # State of CertManager |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 154 | if [ -n "${INSTALL_CERTMANAGER}" ]; then |
| 155 | CERTMANAGER_STATE=$(kubectl get pod -n ${CERTMANAGER_NAMESPACE} --no-headers 2>&1) |
| 156 | CERTMANAGER_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}') |
| 157 | CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}') |
| 158 | COUNT_CERTMANAGER_READY=$(echo "${CERTMANAGER_READY}" | grep -v -e '^$' | wc -l) |
| 159 | COUNT_CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_NOT_READY}" | grep -v -e '^$' | wc -l) |
| 160 | fi |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 161 | |
| 162 | # OK sample |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 163 | if [[ $((${COUNT_OPENEBS_NOT_READY:-0}+${COUNT_METALLB_NOT_READY:-0}+${COUNT_CERTMANAGER_NOT_READY:-0})) -eq 0 ]] |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 164 | then |
| 165 | ((++oks_in_a_row)) |
| 166 | failures_in_a_row=0 |
| 167 | echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r |
| 168 | # NOK sample |
| 169 | else |
| 170 | ((++failures_in_a_row)) |
| 171 | oks_in_a_row=0 |
| 172 | echo |
| 173 | echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold} |
| 174 | |
| 175 | # Reports failed pods in OpenEBS |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 176 | if [[ "${COUNT_OPENEBS_NOT_READY:-0}" -ne 0 ]] |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 177 | then |
| 178 | echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:" |
| 179 | echo "${OPENEBS_NOT_READY}" |
| 180 | echo |
| 181 | fi |
| 182 | |
| 183 | # Reports failed pods in MetalLB |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 184 | if [[ "${COUNT_METALLB_NOT_READY:-0}" -ne 0 ]] |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 185 | then |
| 186 | echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:" |
| 187 | echo "${METALLB_NOT_READY}" |
| 188 | echo |
| 189 | fi |
| 190 | |
| 191 | # Reports failed pods in CertManager |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 192 | if [[ "${COUNT_CERTMANAGER_NOT_READY:-0}" -ne 0 ]] |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 193 | then |
| 194 | echo "CertManager: Waiting for ${COUNT_CERTMANAGER_NOT_READY} of $((${COUNT_CERTMANAGER_NOT_READY}+${COUNT_CERTMANAGER_READY})) pods to be ready:" |
| 195 | echo "${CERTMANAGER_NOT_READY}" |
| 196 | echo |
| 197 | fi |
| 198 | fi |
| 199 | |
| 200 | #------------ NEXT SAMPLE |
| 201 | sleep ${sampling_period} |
| 202 | done |
| 203 | |
| 204 | #################################################################################### |
| 205 | # OUTCOME |
| 206 | #################################################################################### |
| 207 | if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]] |
| 208 | then |
| 209 | echo |
| 210 | FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN" |
| 211 | else |
| 212 | echo |
| 213 | echo "K8S CLUSTER IS READY" |
| 214 | fi |
| 215 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 216 | } |
| 217 | |
| 218 | # main |
| 219 | while getopts ":D:d:i:-: " o; do |
| 220 | case "${o}" in |
| 221 | i) |
| 222 | DEFAULT_IP="${OPTARG}" |
| 223 | ;; |
| 224 | d) |
| 225 | OSM_CLUSTER_WORK_DIR="${OPTARG}" |
| 226 | ;; |
| 227 | D) |
| 228 | OSM_DEVOPS="${OPTARG}" |
| 229 | ;; |
| 230 | -) |
| 231 | [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 232 | [ "${OPTARG}" == "storageclass" ] && INSTALL_STORAGECLASS="y" && continue |
| 233 | [ "${OPTARG}" == "metallb" ] && INSTALL_METALLB="y" && continue |
| 234 | [ "${OPTARG}" == "nginx" ] && INSTALL_NGINX="y" && continue |
| 235 | [ "${OPTARG}" == "certmgr" ] && INSTALL_CERTMANAGER="y" && continue |
| 236 | [ "${OPTARG}" == "all" ] && INSTALL_STORAGECLASS="y" && INSTALL_METALLB="y" && INSTALL_NGINX="y" && INSTALL_CERTMANAGER="y" && continue |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 237 | echo -e "Invalid option: '--$OPTARG'\n" >&2 |
| 238 | exit 1 |
| 239 | ;; |
| 240 | :) |
| 241 | echo "Option -$OPTARG requires an argument" >&2 |
| 242 | exit 1 |
| 243 | ;; |
| 244 | \?) |
| 245 | echo -e "Invalid option: '-$OPTARG'\n" >&2 |
| 246 | exit 1 |
| 247 | ;; |
| 248 | *) |
| 249 | exit 1 |
| 250 | ;; |
| 251 | esac |
| 252 | done |
| 253 | |
| garciadeblas | 8298116 | 2024-07-23 15:24:00 +0200 | [diff] [blame] | 254 | DEBUG_INSTALL=${DEBUG_INSTALL:-} |
| 255 | DEFAULT_IP=${DEFAULT_IP:-} |
| 256 | OSM_DEVOPS=${OSM_DEVOPS:-} |
| 257 | OSM_CLUSTER_WORK_DIR=${OSM_CLUSTER_WORK_DIR:-} |
| 258 | INSTALL_STORAGECLASS=${INSTALL_STORAGECLASS:-} |
| 259 | INSTALL_METALLB=${INSTALL_METALLB:-} |
| 260 | INSTALL_CERTMANAGER=${INSTALL_CERTMANAGER:-} |
| 261 | INSTALL_NGINX=${INSTALL_NGINX:-} |
| 262 | echo "DEBUG_INSTALL=${DEBUG_INSTALL}" |
| 263 | echo "DEFAULT_IP=${DEFAULT_IP}" |
| 264 | echo "OSM_DEVOPS=${OSM_DEVOPS}" |
| 265 | echo "OSM_CLUSTER_WORK_DIR=${OSM_CLUSTER_WORK_DIR}" |
| 266 | |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 267 | source $OSM_DEVOPS/common/logging |
| 268 | source $OSM_DEVOPS/common/track |
| 269 | |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 270 | if [ -n "${INSTALL_STORAGECLASS}" ]; then |
| 271 | install_k8s_storageclass |
| 272 | track k8scluster k8s_storageclass_ok |
| 273 | fi |
| 274 | if [ -n "${INSTALL_METALLB}" ]; then |
| 275 | install_helm_metallb |
| 276 | track k8scluster k8s_metallb_ok |
| 277 | fi |
| 278 | if [ -n "${INSTALL_CERTMANAGER}" ]; then |
| 279 | install_helm_certmanager |
| 280 | track k8scluster k8s_certmanager_ok |
| 281 | fi |
| 282 | if [ -n "${INSTALL_NGINX}" ]; then |
| 283 | install_helm_nginx |
| 284 | track k8scluster k8s_nginx_ok |
| 285 | fi |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 286 | check_for_readiness |
| 287 | track k8scluster k8s_ready_ok |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 288 | if [ -n "${INSTALL_METALLB}" ]; then |
| 289 | configure_ipaddresspool_metallb |
| 290 | fi |