blob: b54641890a5b3f893858be935d4f4d9f6d1d4831 [file] [log] [blame]
garciadeblas41f5ce52024-04-01 17:46:09 +02001#!/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
16set +eux
17
18function 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
garciadeblasb3797412024-06-06 14:26:24 +020025 helm upgrade --install --create-namespace --namespace openebs openebs openebs/openebs --version ${OPENEBS_VERSION}
garciadeblas41f5ce52024-04-01 17:46:09 +020026 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
50function 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
garciadeblasb3797412024-06-06 14:26:24 +020056 # 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}
garciadeblas41f5ce52024-04-01 17:46:09 +020061 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
62}
63
64function 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"
garciadeblasb3797412024-06-06 14:26:24 +020067 [ ! -d "$OSM_CLUSTER_WORK_DIR" ] && sudo mkdir -p $OSM_CLUSTER_WORK_DIR
garciadeblas41f5ce52024-04-01 17:46:09 +020068 METALLB_IP_RANGE="$DEFAULT_IP/32"
69 echo "apiVersion: metallb.io/v1beta1
70kind: IPAddressPool
71metadata:
72 name: first-pool
73 namespace: metallb-system
74spec:
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
83function 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
garciadeblasb3797412024-06-06 14:26:24 +020089 helm upgrade --install cert-manager --create-namespace --namespace cert-manager jetstack/cert-manager \
garciadeblas41f5ce52024-04-01 17:46:09 +020090 --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
garciadeblas18582e92024-05-21 12:13:50 +020096#installs nginx
97function 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'
102 ANNOTATIONS=${ANNOTATIONS:-""}
103 helm upgrade --install ingress-nginx ingress-nginx \
104 --repo https://kubernetes.github.io/ingress-nginx --version ${NGINX_VERSION} \
105 --namespace ingress-nginx --create-namespace ${ANNOTATIONS}
106 # Wait until ready
107 kubectl wait --namespace ingress-nginx \
108 --for=condition=ready pod \
109 --selector=app.kubernetes.io/component=controller \
110 --timeout=120s
111 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
112}
113
114#checks openebs, metallb and cert-manager readiness
garciadeblas41f5ce52024-04-01 17:46:09 +0200115function check_for_readiness() {
116 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
117 # Default input values
118 sampling_period=2 # seconds
119 time_for_readiness=20 # seconds ready
120 time_for_failure=200 # seconds broken
121 OPENEBS_NAMESPACE=openebs
122 METALLB_NAMESPACE=metallb-system
123 CERTMANAGER_NAMESPACE=cert-manager
garciadeblas41f5ce52024-04-01 17:46:09 +0200124
125 # Equivalent number of samples
126 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
127 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
128 failures_in_a_row=0
129 oks_in_a_row=0
130
131 ####################################################################################
132 # Loop to check system readiness
133 ####################################################################################
134 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
135 do
136 # State of OpenEBS
garciadeblasb3797412024-06-06 14:26:24 +0200137 if [ -n "${INSTALL_STORAGECLASS}" ]; then
138 OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
139 OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
140 OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
141 COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l)
142 COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l)
143 fi
garciadeblas41f5ce52024-04-01 17:46:09 +0200144
145 # State of MetalLB
garciadeblasb3797412024-06-06 14:26:24 +0200146 if [ -n "${INSTALL_METALLB}" ]; then
147 METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1)
148 METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
149 METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
150 COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l)
151 COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l)
152 fi
garciadeblas41f5ce52024-04-01 17:46:09 +0200153
154 # State of CertManager
garciadeblasb3797412024-06-06 14:26:24 +0200155 if [ -n "${INSTALL_CERTMANAGER}" ]; then
156 CERTMANAGER_STATE=$(kubectl get pod -n ${CERTMANAGER_NAMESPACE} --no-headers 2>&1)
157 CERTMANAGER_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
158 CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
159 COUNT_CERTMANAGER_READY=$(echo "${CERTMANAGER_READY}" | grep -v -e '^$' | wc -l)
160 COUNT_CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_NOT_READY}" | grep -v -e '^$' | wc -l)
161 fi
garciadeblas41f5ce52024-04-01 17:46:09 +0200162
163 # OK sample
garciadeblasb3797412024-06-06 14:26:24 +0200164 if [[ $((${COUNT_OPENEBS_NOT_READY:-0}+${COUNT_METALLB_NOT_READY:-0}+${COUNT_CERTMANAGER_NOT_READY:-0})) -eq 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200165 then
166 ((++oks_in_a_row))
167 failures_in_a_row=0
168 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
169 # NOK sample
170 else
171 ((++failures_in_a_row))
172 oks_in_a_row=0
173 echo
174 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
175
176 # Reports failed pods in OpenEBS
garciadeblasb3797412024-06-06 14:26:24 +0200177 if [[ "${COUNT_OPENEBS_NOT_READY:-0}" -ne 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200178 then
179 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
180 echo "${OPENEBS_NOT_READY}"
181 echo
182 fi
183
184 # Reports failed pods in MetalLB
garciadeblasb3797412024-06-06 14:26:24 +0200185 if [[ "${COUNT_METALLB_NOT_READY:-0}" -ne 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200186 then
187 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
188 echo "${METALLB_NOT_READY}"
189 echo
190 fi
191
192 # Reports failed pods in CertManager
garciadeblasb3797412024-06-06 14:26:24 +0200193 if [[ "${COUNT_CERTMANAGER_NOT_READY:-0}" -ne 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200194 then
195 echo "CertManager: Waiting for ${COUNT_CERTMANAGER_NOT_READY} of $((${COUNT_CERTMANAGER_NOT_READY}+${COUNT_CERTMANAGER_READY})) pods to be ready:"
196 echo "${CERTMANAGER_NOT_READY}"
197 echo
198 fi
199 fi
200
201 #------------ NEXT SAMPLE
202 sleep ${sampling_period}
203 done
204
205 ####################################################################################
206 # OUTCOME
207 ####################################################################################
208 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
209 then
210 echo
211 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
212 else
213 echo
214 echo "K8S CLUSTER IS READY"
215 fi
216 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
217}
218
219# main
220while getopts ":D:d:i:-: " o; do
221 case "${o}" in
222 i)
223 DEFAULT_IP="${OPTARG}"
224 ;;
225 d)
226 OSM_CLUSTER_WORK_DIR="${OPTARG}"
227 ;;
228 D)
229 OSM_DEVOPS="${OPTARG}"
230 ;;
231 -)
232 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
garciadeblasb3797412024-06-06 14:26:24 +0200233 [ "${OPTARG}" == "storageclass" ] && INSTALL_STORAGECLASS="y" && continue
234 [ "${OPTARG}" == "metallb" ] && INSTALL_METALLB="y" && continue
235 [ "${OPTARG}" == "nginx" ] && INSTALL_NGINX="y" && continue
236 [ "${OPTARG}" == "certmgr" ] && INSTALL_CERTMANAGER="y" && continue
237 [ "${OPTARG}" == "all" ] && INSTALL_STORAGECLASS="y" && INSTALL_METALLB="y" && INSTALL_NGINX="y" && INSTALL_CERTMANAGER="y" && continue
garciadeblas41f5ce52024-04-01 17:46:09 +0200238 echo -e "Invalid option: '--$OPTARG'\n" >&2
239 exit 1
240 ;;
241 :)
242 echo "Option -$OPTARG requires an argument" >&2
243 exit 1
244 ;;
245 \?)
246 echo -e "Invalid option: '-$OPTARG'\n" >&2
247 exit 1
248 ;;
249 *)
250 exit 1
251 ;;
252 esac
253done
254
garciadeblas82981162024-07-23 15:24:00 +0200255DEBUG_INSTALL=${DEBUG_INSTALL:-}
256DEFAULT_IP=${DEFAULT_IP:-}
257OSM_DEVOPS=${OSM_DEVOPS:-}
258OSM_CLUSTER_WORK_DIR=${OSM_CLUSTER_WORK_DIR:-}
259INSTALL_STORAGECLASS=${INSTALL_STORAGECLASS:-}
260INSTALL_METALLB=${INSTALL_METALLB:-}
261INSTALL_CERTMANAGER=${INSTALL_CERTMANAGER:-}
262INSTALL_NGINX=${INSTALL_NGINX:-}
263echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
264echo "DEFAULT_IP=${DEFAULT_IP}"
265echo "OSM_DEVOPS=${OSM_DEVOPS}"
266echo "OSM_CLUSTER_WORK_DIR=${OSM_CLUSTER_WORK_DIR}"
267
garciadeblas41f5ce52024-04-01 17:46:09 +0200268source $OSM_DEVOPS/common/logging
269source $OSM_DEVOPS/common/track
270
garciadeblasb3797412024-06-06 14:26:24 +0200271if [ -n "${INSTALL_STORAGECLASS}" ]; then
272 install_k8s_storageclass
273 track k8scluster k8s_storageclass_ok
274fi
275if [ -n "${INSTALL_METALLB}" ]; then
276 install_helm_metallb
277 track k8scluster k8s_metallb_ok
278fi
279if [ -n "${INSTALL_CERTMANAGER}" ]; then
280 install_helm_certmanager
281 track k8scluster k8s_certmanager_ok
282fi
283if [ -n "${INSTALL_NGINX}" ]; then
284 install_helm_nginx
285 track k8scluster k8s_nginx_ok
286fi
garciadeblas41f5ce52024-04-01 17:46:09 +0200287check_for_readiness
288track k8scluster k8s_ready_ok
garciadeblasb3797412024-06-06 14:26:24 +0200289if [ -n "${INSTALL_METALLB}" ]; then
290 configure_ipaddresspool_metallb
291fi