blob: 1bb9162f1171323d0624a3086515219a01843184 [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
garciadeblascf603f52025-06-04 11:57:28 +020066 echo "Creating IP address pool manifest: ${K8SCLUSTER_CONFIG_FOLDER}/metallb-ipaddrpool.yaml"
67 [ ! -d "$K8SCLUSTER_CONFIG_FOLDER" ] && sudo mkdir -p $K8SCLUSTER_CONFIG_FOLDER
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:
garciadeblascf603f52025-06-04 11:57:28 +020076 - ${METALLB_IP_RANGE}" | sudo tee -a ${K8SCLUSTER_CONFIG_FOLDER}/metallb-ipaddrpool.yaml
77 echo "Applying IP address pool manifest: kubectl apply -f ${K8SCLUSTER_CONFIG_FOLDER}/metallb-ipaddrpool.yaml"
78 kubectl apply -f ${K8SCLUSTER_CONFIG_FOLDER}/metallb-ipaddrpool.yaml || FATAL_TRACK k8scluster "Cannot create IP address Pool in MetalLB"
garciadeblas41f5ce52024-04-01 17:46:09 +020079 [ -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
garciadeblascf603f52025-06-04 11:57:28 +0200219while getopts ":-: " o; do
garciadeblas41f5ce52024-04-01 17:46:09 +0200220 case "${o}" in
garciadeblas41f5ce52024-04-01 17:46:09 +0200221 -)
222 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
garciadeblasb3797412024-06-06 14:26:24 +0200223 [ "${OPTARG}" == "storageclass" ] && INSTALL_STORAGECLASS="y" && continue
224 [ "${OPTARG}" == "metallb" ] && INSTALL_METALLB="y" && continue
225 [ "${OPTARG}" == "nginx" ] && INSTALL_NGINX="y" && continue
226 [ "${OPTARG}" == "certmgr" ] && INSTALL_CERTMANAGER="y" && continue
227 [ "${OPTARG}" == "all" ] && INSTALL_STORAGECLASS="y" && INSTALL_METALLB="y" && INSTALL_NGINX="y" && INSTALL_CERTMANAGER="y" && continue
garciadeblas41f5ce52024-04-01 17:46:09 +0200228 echo -e "Invalid option: '--$OPTARG'\n" >&2
229 exit 1
230 ;;
231 :)
232 echo "Option -$OPTARG requires an argument" >&2
233 exit 1
234 ;;
235 \?)
236 echo -e "Invalid option: '-$OPTARG'\n" >&2
237 exit 1
238 ;;
239 *)
240 exit 1
241 ;;
242 esac
243done
244
garciadeblas82981162024-07-23 15:24:00 +0200245DEBUG_INSTALL=${DEBUG_INSTALL:-}
246DEFAULT_IP=${DEFAULT_IP:-}
garciadeblascf603f52025-06-04 11:57:28 +0200247K8SCLUSTER_CONFIG_FOLDER=${K8SCLUSTER_CONFIG_FOLDER:-}
garciadeblas82981162024-07-23 15:24:00 +0200248INSTALL_STORAGECLASS=${INSTALL_STORAGECLASS:-}
249INSTALL_METALLB=${INSTALL_METALLB:-}
250INSTALL_CERTMANAGER=${INSTALL_CERTMANAGER:-}
251INSTALL_NGINX=${INSTALL_NGINX:-}
252echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
253echo "DEFAULT_IP=${DEFAULT_IP}"
garciadeblascf603f52025-06-04 11:57:28 +0200254echo "K8SCLUSTER_CONFIG_FOLDER=${K8SCLUSTER_CONFIG_FOLDER}"
garciadeblas82981162024-07-23 15:24:00 +0200255
garciadeblascf603f52025-06-04 11:57:28 +0200256export HERE=$(dirname "$(readlink --canonicalize "$BASH_SOURCE")")
257source "${HERE}/../../../library/logging"
258source "${HERE}/../../../library/track"
garciadeblas41f5ce52024-04-01 17:46:09 +0200259
garciadeblasb3797412024-06-06 14:26:24 +0200260if [ -n "${INSTALL_STORAGECLASS}" ]; then
261 install_k8s_storageclass
262 track k8scluster k8s_storageclass_ok
263fi
264if [ -n "${INSTALL_METALLB}" ]; then
265 install_helm_metallb
266 track k8scluster k8s_metallb_ok
267fi
268if [ -n "${INSTALL_CERTMANAGER}" ]; then
269 install_helm_certmanager
270 track k8scluster k8s_certmanager_ok
271fi
272if [ -n "${INSTALL_NGINX}" ]; then
273 install_helm_nginx
274 track k8scluster k8s_nginx_ok
275fi
garciadeblas41f5ce52024-04-01 17:46:09 +0200276check_for_readiness
277track k8scluster k8s_ready_ok
garciadeblasb3797412024-06-06 14:26:24 +0200278if [ -n "${INSTALL_METALLB}" ]; then
279 configure_ipaddresspool_metallb
280fi