blob: 9a3d43f3eb20d6730c2e0ca1fa5fba0fffb9e23c [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 \
garciadeblas41f5ce52024-04-01 17:46:09 +020091 --set extraArgs="{--enable-certificate-owner-ref=true}"
92 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
93}
94
garciadeblas18582e92024-05-21 12:13:50 +020095#installs nginx
96function install_helm_nginx() {
97 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
98 echo "Installing nginx"
99 NGINX_VERSION="4.10.0"
100 ANNOTATIONS='--set controller.service.annotations."service\.beta\.kubernetes\.io/azure-load-balancer-health-probe-request-path"=/healthz'
garciadeblas18582e92024-05-21 12:13:50 +0200101 helm upgrade --install ingress-nginx ingress-nginx \
102 --repo https://kubernetes.github.io/ingress-nginx --version ${NGINX_VERSION} \
103 --namespace ingress-nginx --create-namespace ${ANNOTATIONS}
104 # Wait until ready
105 kubectl wait --namespace ingress-nginx \
106 --for=condition=ready pod \
107 --selector=app.kubernetes.io/component=controller \
108 --timeout=120s
109 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
110}
111
112#checks openebs, metallb and cert-manager readiness
garciadeblas41f5ce52024-04-01 17:46:09 +0200113function check_for_readiness() {
114 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
115 # Default input values
116 sampling_period=2 # seconds
117 time_for_readiness=20 # seconds ready
118 time_for_failure=200 # seconds broken
119 OPENEBS_NAMESPACE=openebs
120 METALLB_NAMESPACE=metallb-system
121 CERTMANAGER_NAMESPACE=cert-manager
garciadeblas41f5ce52024-04-01 17:46:09 +0200122
123 # Equivalent number of samples
124 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
125 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
126 failures_in_a_row=0
127 oks_in_a_row=0
128
129 ####################################################################################
130 # Loop to check system readiness
131 ####################################################################################
132 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
133 do
134 # State of OpenEBS
garciadeblasb3797412024-06-06 14:26:24 +0200135 if [ -n "${INSTALL_STORAGECLASS}" ]; then
136 OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
137 OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
138 OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
139 COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l)
140 COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l)
141 fi
garciadeblas41f5ce52024-04-01 17:46:09 +0200142
143 # State of MetalLB
garciadeblasb3797412024-06-06 14:26:24 +0200144 if [ -n "${INSTALL_METALLB}" ]; then
145 METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1)
146 METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
147 METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
148 COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l)
149 COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l)
150 fi
garciadeblas41f5ce52024-04-01 17:46:09 +0200151
152 # State of CertManager
garciadeblasb3797412024-06-06 14:26:24 +0200153 if [ -n "${INSTALL_CERTMANAGER}" ]; then
154 CERTMANAGER_STATE=$(kubectl get pod -n ${CERTMANAGER_NAMESPACE} --no-headers 2>&1)
155 CERTMANAGER_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
156 CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
157 COUNT_CERTMANAGER_READY=$(echo "${CERTMANAGER_READY}" | grep -v -e '^$' | wc -l)
158 COUNT_CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_NOT_READY}" | grep -v -e '^$' | wc -l)
159 fi
garciadeblas41f5ce52024-04-01 17:46:09 +0200160
161 # OK sample
garciadeblasb3797412024-06-06 14:26:24 +0200162 if [[ $((${COUNT_OPENEBS_NOT_READY:-0}+${COUNT_METALLB_NOT_READY:-0}+${COUNT_CERTMANAGER_NOT_READY:-0})) -eq 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200163 then
164 ((++oks_in_a_row))
165 failures_in_a_row=0
166 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
167 # NOK sample
168 else
169 ((++failures_in_a_row))
170 oks_in_a_row=0
171 echo
172 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
173
174 # Reports failed pods in OpenEBS
garciadeblasb3797412024-06-06 14:26:24 +0200175 if [[ "${COUNT_OPENEBS_NOT_READY:-0}" -ne 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200176 then
177 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
178 echo "${OPENEBS_NOT_READY}"
179 echo
180 fi
181
182 # Reports failed pods in MetalLB
garciadeblasb3797412024-06-06 14:26:24 +0200183 if [[ "${COUNT_METALLB_NOT_READY:-0}" -ne 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200184 then
185 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
186 echo "${METALLB_NOT_READY}"
187 echo
188 fi
189
190 # Reports failed pods in CertManager
garciadeblasb3797412024-06-06 14:26:24 +0200191 if [[ "${COUNT_CERTMANAGER_NOT_READY:-0}" -ne 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200192 then
193 echo "CertManager: Waiting for ${COUNT_CERTMANAGER_NOT_READY} of $((${COUNT_CERTMANAGER_NOT_READY}+${COUNT_CERTMANAGER_READY})) pods to be ready:"
194 echo "${CERTMANAGER_NOT_READY}"
195 echo
196 fi
197 fi
198
199 #------------ NEXT SAMPLE
200 sleep ${sampling_period}
201 done
202
203 ####################################################################################
204 # OUTCOME
205 ####################################################################################
206 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
207 then
208 echo
209 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
210 else
211 echo
212 echo "K8S CLUSTER IS READY"
213 fi
214 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
215}
216
217# main
218while getopts ":D:d:i:-: " o; do
219 case "${o}" in
220 i)
221 DEFAULT_IP="${OPTARG}"
222 ;;
223 d)
224 OSM_CLUSTER_WORK_DIR="${OPTARG}"
225 ;;
226 D)
227 OSM_DEVOPS="${OPTARG}"
228 ;;
229 -)
230 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
garciadeblasb3797412024-06-06 14:26:24 +0200231 [ "${OPTARG}" == "storageclass" ] && INSTALL_STORAGECLASS="y" && continue
232 [ "${OPTARG}" == "metallb" ] && INSTALL_METALLB="y" && continue
233 [ "${OPTARG}" == "nginx" ] && INSTALL_NGINX="y" && continue
234 [ "${OPTARG}" == "certmgr" ] && INSTALL_CERTMANAGER="y" && continue
235 [ "${OPTARG}" == "all" ] && INSTALL_STORAGECLASS="y" && INSTALL_METALLB="y" && INSTALL_NGINX="y" && INSTALL_CERTMANAGER="y" && continue
garciadeblas41f5ce52024-04-01 17:46:09 +0200236 echo -e "Invalid option: '--$OPTARG'\n" >&2
237 exit 1
238 ;;
239 :)
240 echo "Option -$OPTARG requires an argument" >&2
241 exit 1
242 ;;
243 \?)
244 echo -e "Invalid option: '-$OPTARG'\n" >&2
245 exit 1
246 ;;
247 *)
248 exit 1
249 ;;
250 esac
251done
252
garciadeblas82981162024-07-23 15:24:00 +0200253DEBUG_INSTALL=${DEBUG_INSTALL:-}
254DEFAULT_IP=${DEFAULT_IP:-}
255OSM_DEVOPS=${OSM_DEVOPS:-}
256OSM_CLUSTER_WORK_DIR=${OSM_CLUSTER_WORK_DIR:-}
257INSTALL_STORAGECLASS=${INSTALL_STORAGECLASS:-}
258INSTALL_METALLB=${INSTALL_METALLB:-}
259INSTALL_CERTMANAGER=${INSTALL_CERTMANAGER:-}
260INSTALL_NGINX=${INSTALL_NGINX:-}
261echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
262echo "DEFAULT_IP=${DEFAULT_IP}"
263echo "OSM_DEVOPS=${OSM_DEVOPS}"
264echo "OSM_CLUSTER_WORK_DIR=${OSM_CLUSTER_WORK_DIR}"
265
garciadeblas41f5ce52024-04-01 17:46:09 +0200266source $OSM_DEVOPS/common/logging
267source $OSM_DEVOPS/common/track
268
garciadeblasb3797412024-06-06 14:26:24 +0200269if [ -n "${INSTALL_STORAGECLASS}" ]; then
270 install_k8s_storageclass
271 track k8scluster k8s_storageclass_ok
272fi
273if [ -n "${INSTALL_METALLB}" ]; then
274 install_helm_metallb
275 track k8scluster k8s_metallb_ok
276fi
277if [ -n "${INSTALL_CERTMANAGER}" ]; then
278 install_helm_certmanager
279 track k8scluster k8s_certmanager_ok
280fi
281if [ -n "${INSTALL_NGINX}" ]; then
282 install_helm_nginx
283 track k8scluster k8s_nginx_ok
284fi
garciadeblas41f5ce52024-04-01 17:46:09 +0200285check_for_readiness
286track k8scluster k8s_ready_ok
garciadeblasb3797412024-06-06 14:26:24 +0200287if [ -n "${INSTALL_METALLB}" ]; then
288 configure_ipaddresspool_metallb
289fi