Fix installation of Kubernetes metrics server by updating the URL
[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 #installs nginx
92 function install_helm_nginx() {
93 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
94 echo "Installing nginx"
95 NGINX_VERSION="4.10.0"
96 ANNOTATIONS='--set controller.service.annotations."service\.beta\.kubernetes\.io/azure-load-balancer-health-probe-request-path"=/healthz'
97 ANNOTATIONS=${ANNOTATIONS:-""}
98 helm upgrade --install ingress-nginx ingress-nginx \
99 --repo https://kubernetes.github.io/ingress-nginx --version ${NGINX_VERSION} \
100 --namespace ingress-nginx --create-namespace ${ANNOTATIONS}
101 # Wait until ready
102 kubectl wait --namespace ingress-nginx \
103 --for=condition=ready pod \
104 --selector=app.kubernetes.io/component=controller \
105 --timeout=120s
106 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
107 }
108
109 #checks openebs, metallb and cert-manager readiness
110 function check_for_readiness() {
111 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
112 # Default input values
113 sampling_period=2 # seconds
114 time_for_readiness=20 # seconds ready
115 time_for_failure=200 # seconds broken
116 OPENEBS_NAMESPACE=openebs
117 METALLB_NAMESPACE=metallb-system
118 CERTMANAGER_NAMESPACE=cert-manager
119 # STACK_NAME=osm # By default, "osm"
120
121 # Equivalent number of samples
122 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
123 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
124 failures_in_a_row=0
125 oks_in_a_row=0
126
127 ####################################################################################
128 # Loop to check system readiness
129 ####################################################################################
130 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
131 do
132 # State of OpenEBS
133 OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
134 OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
135 OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
136 COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l)
137 COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l)
138
139 # State of MetalLB
140 METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1)
141 METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
142 METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
143 COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l)
144 COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l)
145
146 # State of CertManager
147 CERTMANAGER_STATE=$(kubectl get pod -n ${CERTMANAGER_NAMESPACE} --no-headers 2>&1)
148 CERTMANAGER_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
149 CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
150 COUNT_CERTMANAGER_READY=$(echo "${CERTMANAGER_READY}" | grep -v -e '^$' | wc -l)
151 COUNT_CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_NOT_READY}" | grep -v -e '^$' | wc -l)
152
153 # OK sample
154 if [[ $((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
155 then
156 ((++oks_in_a_row))
157 failures_in_a_row=0
158 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
159 # NOK sample
160 else
161 ((++failures_in_a_row))
162 oks_in_a_row=0
163 echo
164 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
165
166 # Reports failed pods in OpenEBS
167 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
168 then
169 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
170 echo "${OPENEBS_NOT_READY}"
171 echo
172 fi
173
174 # Reports failed pods in MetalLB
175 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
176 then
177 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
178 echo "${METALLB_NOT_READY}"
179 echo
180 fi
181
182 # Reports failed pods in CertManager
183 if [[ "${COUNT_CERTMANAGER_NOT_READY}" -ne 0 ]]
184 then
185 echo "CertManager: Waiting for ${COUNT_CERTMANAGER_NOT_READY} of $((${COUNT_CERTMANAGER_NOT_READY}+${COUNT_CERTMANAGER_READY})) pods to be ready:"
186 echo "${CERTMANAGER_NOT_READY}"
187 echo
188 fi
189 fi
190
191 #------------ NEXT SAMPLE
192 sleep ${sampling_period}
193 done
194
195 ####################################################################################
196 # OUTCOME
197 ####################################################################################
198 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
199 then
200 echo
201 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
202 else
203 echo
204 echo "K8S CLUSTER IS READY"
205 fi
206 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
207 }
208
209 # main
210 while getopts ":D:d:i:-: " o; do
211 case "${o}" in
212 i)
213 DEFAULT_IP="${OPTARG}"
214 ;;
215 d)
216 OSM_CLUSTER_WORK_DIR="${OPTARG}"
217 ;;
218 D)
219 OSM_DEVOPS="${OPTARG}"
220 ;;
221 -)
222 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
223 echo -e "Invalid option: '--$OPTARG'\n" >&2
224 exit 1
225 ;;
226 :)
227 echo "Option -$OPTARG requires an argument" >&2
228 exit 1
229 ;;
230 \?)
231 echo -e "Invalid option: '-$OPTARG'\n" >&2
232 exit 1
233 ;;
234 *)
235 exit 1
236 ;;
237 esac
238 done
239
240 source $OSM_DEVOPS/common/logging
241 source $OSM_DEVOPS/common/track
242
243 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
244 echo "DEFAULT_IP=$DEFAULT_IP"
245 echo "OSM_DEVOPS=$OSM_DEVOPS"
246 echo "OSM_CLUSTER_WORK_DIR=$OSM_CLUSTER_WORK_DIR"
247
248 install_k8s_storageclass
249 track k8scluster k8s_storageclass_ok
250 install_helm_metallb
251 track k8scluster k8s_metallb_ok
252 install_helm_certmanager
253 track k8scluster k8s_certmanager_ok
254 install_helm_nginx
255 track k8scluster k8s_nginx_ok
256 check_for_readiness
257 track k8scluster k8s_ready_ok
258 configure_ipaddresspool_metallb