Separate cluster addons installation (openebs, metallb, cert-mgr) from install_kubead...
[osm/devops.git] / installers / install_cluster_addons.sh
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
25 helm install --create-namespace --namespace openebs openebs openebs/openebs --version ${OPENEBS_VERSION}
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
56 helm install --create-namespace --namespace metallb-system metallb metallb/metallb --version ${METALLB_VERSION}
57 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
58 }
59
60 function configure_ipaddresspool_metallb() {
61 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
62 echo "Creating IP address pool manifest: ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml"
63 METALLB_IP_RANGE="$DEFAULT_IP/32"
64 echo "apiVersion: metallb.io/v1beta1
65 kind: IPAddressPool
66 metadata:
67 name: first-pool
68 namespace: metallb-system
69 spec:
70 addresses:
71 - ${METALLB_IP_RANGE}" | sudo tee -a ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml
72 echo "Applying IP address pool manifest: kubectl apply -f ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml"
73 kubectl apply -f ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml || FATAL_TRACK k8scluster "Cannot create IP address Pool in MetalLB"
74 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
75 }
76
77 #installs cert-manager
78 function install_helm_certmanager() {
79 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
80 echo "Installing cert-manager"
81 CERTMANAGER_VERSION="v1.9.1"
82 helm repo add jetstack https://charts.jetstack.io
83 helm repo update
84 helm install cert-manager --create-namespace --namespace cert-manager jetstack/cert-manager \
85 --version ${CERTMANAGER_VERSION} --set installCRDs=true --set prometheus.enabled=false \
86 --set clusterResourceNamespace=osm \
87 --set extraArgs="{--enable-certificate-owner-ref=true}"
88 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
89 }
90
91 #checks openebs and metallb readiness
92 function check_for_readiness() {
93 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
94 # Default input values
95 sampling_period=2 # seconds
96 time_for_readiness=20 # seconds ready
97 time_for_failure=200 # seconds broken
98 OPENEBS_NAMESPACE=openebs
99 METALLB_NAMESPACE=metallb-system
100 CERTMANAGER_NAMESPACE=cert-manager
101 # STACK_NAME=osm # By default, "osm"
102
103 # Equivalent number of samples
104 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
105 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
106 failures_in_a_row=0
107 oks_in_a_row=0
108
109 ####################################################################################
110 # Loop to check system readiness
111 ####################################################################################
112 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
113 do
114 # State of OpenEBS
115 OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
116 OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
117 OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
118 COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l)
119 COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l)
120
121 # State of MetalLB
122 METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1)
123 METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
124 METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
125 COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l)
126 COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l)
127
128 # State of CertManager
129 CERTMANAGER_STATE=$(kubectl get pod -n ${CERTMANAGER_NAMESPACE} --no-headers 2>&1)
130 CERTMANAGER_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
131 CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
132 COUNT_CERTMANAGER_READY=$(echo "${CERTMANAGER_READY}" | grep -v -e '^$' | wc -l)
133 COUNT_CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_NOT_READY}" | grep -v -e '^$' | wc -l)
134
135 # OK sample
136 if [[ $((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
137 then
138 ((++oks_in_a_row))
139 failures_in_a_row=0
140 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
141 # NOK sample
142 else
143 ((++failures_in_a_row))
144 oks_in_a_row=0
145 echo
146 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
147
148 # Reports failed pods in OpenEBS
149 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
150 then
151 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
152 echo "${OPENEBS_NOT_READY}"
153 echo
154 fi
155
156 # Reports failed pods in MetalLB
157 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
158 then
159 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
160 echo "${METALLB_NOT_READY}"
161 echo
162 fi
163
164 # Reports failed pods in CertManager
165 if [[ "${COUNT_CERTMANAGER_NOT_READY}" -ne 0 ]]
166 then
167 echo "CertManager: Waiting for ${COUNT_CERTMANAGER_NOT_READY} of $((${COUNT_CERTMANAGER_NOT_READY}+${COUNT_CERTMANAGER_READY})) pods to be ready:"
168 echo "${CERTMANAGER_NOT_READY}"
169 echo
170 fi
171 fi
172
173 #------------ NEXT SAMPLE
174 sleep ${sampling_period}
175 done
176
177 ####################################################################################
178 # OUTCOME
179 ####################################################################################
180 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
181 then
182 echo
183 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
184 else
185 echo
186 echo "K8S CLUSTER IS READY"
187 fi
188 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
189 }
190
191 # main
192 while getopts ":D:d:i:-: " o; do
193 case "${o}" in
194 i)
195 DEFAULT_IP="${OPTARG}"
196 ;;
197 d)
198 OSM_CLUSTER_WORK_DIR="${OPTARG}"
199 ;;
200 D)
201 OSM_DEVOPS="${OPTARG}"
202 ;;
203 -)
204 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
205 echo -e "Invalid option: '--$OPTARG'\n" >&2
206 exit 1
207 ;;
208 :)
209 echo "Option -$OPTARG requires an argument" >&2
210 exit 1
211 ;;
212 \?)
213 echo -e "Invalid option: '-$OPTARG'\n" >&2
214 exit 1
215 ;;
216 *)
217 exit 1
218 ;;
219 esac
220 done
221
222 source $OSM_DEVOPS/common/logging
223 source $OSM_DEVOPS/common/track
224
225 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
226 echo "DEFAULT_IP=$DEFAULT_IP"
227 echo "OSM_DEVOPS=$OSM_DEVOPS"
228 echo "OSM_CLUSTER_WORK_DIR=$OSM_CLUSTER_WORK_DIR"
229
230 install_k8s_storageclass
231 track k8scluster k8s_storageclass_ok
232 install_helm_metallb
233 track k8scluster k8s_metallb_ok
234 install_helm_certmanager
235 track k8scluster k8s_certmanager_ok
236 check_for_readiness
237 track k8scluster k8s_ready_ok
238 configure_ipaddresspool_metallb