blob: 8f42c7f17422a950f8600017aa44bae94d367029 [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'
garciadeblas18582e92024-05-21 12:13:50 +0200102 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
garciadeblas41f5ce52024-04-01 17:46:09 +0200114function 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
garciadeblas41f5ce52024-04-01 17:46:09 +0200123
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
garciadeblasb3797412024-06-06 14:26:24 +0200136 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
garciadeblas41f5ce52024-04-01 17:46:09 +0200143
144 # State of MetalLB
garciadeblasb3797412024-06-06 14:26:24 +0200145 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
garciadeblas41f5ce52024-04-01 17:46:09 +0200152
153 # State of CertManager
garciadeblasb3797412024-06-06 14:26:24 +0200154 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
garciadeblas41f5ce52024-04-01 17:46:09 +0200161
162 # OK sample
garciadeblasb3797412024-06-06 14:26:24 +0200163 if [[ $((${COUNT_OPENEBS_NOT_READY:-0}+${COUNT_METALLB_NOT_READY:-0}+${COUNT_CERTMANAGER_NOT_READY:-0})) -eq 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200164 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
garciadeblasb3797412024-06-06 14:26:24 +0200176 if [[ "${COUNT_OPENEBS_NOT_READY:-0}" -ne 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200177 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
garciadeblasb3797412024-06-06 14:26:24 +0200184 if [[ "${COUNT_METALLB_NOT_READY:-0}" -ne 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200185 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
garciadeblasb3797412024-06-06 14:26:24 +0200192 if [[ "${COUNT_CERTMANAGER_NOT_READY:-0}" -ne 0 ]]
garciadeblas41f5ce52024-04-01 17:46:09 +0200193 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
219while 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
garciadeblasb3797412024-06-06 14:26:24 +0200232 [ "${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
garciadeblas41f5ce52024-04-01 17:46:09 +0200237 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
252done
253
garciadeblas82981162024-07-23 15:24:00 +0200254DEBUG_INSTALL=${DEBUG_INSTALL:-}
255DEFAULT_IP=${DEFAULT_IP:-}
256OSM_DEVOPS=${OSM_DEVOPS:-}
257OSM_CLUSTER_WORK_DIR=${OSM_CLUSTER_WORK_DIR:-}
258INSTALL_STORAGECLASS=${INSTALL_STORAGECLASS:-}
259INSTALL_METALLB=${INSTALL_METALLB:-}
260INSTALL_CERTMANAGER=${INSTALL_CERTMANAGER:-}
261INSTALL_NGINX=${INSTALL_NGINX:-}
262echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
263echo "DEFAULT_IP=${DEFAULT_IP}"
264echo "OSM_DEVOPS=${OSM_DEVOPS}"
265echo "OSM_CLUSTER_WORK_DIR=${OSM_CLUSTER_WORK_DIR}"
266
garciadeblas41f5ce52024-04-01 17:46:09 +0200267source $OSM_DEVOPS/common/logging
268source $OSM_DEVOPS/common/track
269
garciadeblasb3797412024-06-06 14:26:24 +0200270if [ -n "${INSTALL_STORAGECLASS}" ]; then
271 install_k8s_storageclass
272 track k8scluster k8s_storageclass_ok
273fi
274if [ -n "${INSTALL_METALLB}" ]; then
275 install_helm_metallb
276 track k8scluster k8s_metallb_ok
277fi
278if [ -n "${INSTALL_CERTMANAGER}" ]; then
279 install_helm_certmanager
280 track k8scluster k8s_certmanager_ok
281fi
282if [ -n "${INSTALL_NGINX}" ]; then
283 install_helm_nginx
284 track k8scluster k8s_nginx_ok
285fi
garciadeblas41f5ce52024-04-01 17:46:09 +0200286check_for_readiness
287track k8scluster k8s_ready_ok
garciadeblasb3797412024-06-06 14:26:24 +0200288if [ -n "${INSTALL_METALLB}" ]; then
289 configure_ipaddresspool_metallb
290fi