blob: 648a1be0092566ae38ab41cd6059d13f2b8938d6 [file] [log] [blame]
garciadeblas0bc87522021-10-20 22:16:17 +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
garciadeblas8fed1082022-08-29 11:25:02 +020018K8S_VERSION=1.23.3-00
19
20# installs kubernetes packages
garciadeblas0bc87522021-10-20 22:16:17 +020021function install_kube() {
22 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblasc1ae2392021-12-14 18:02:30 +010023 # Kubernetes releases can be found here: https://kubernetes.io/releases/
garciadeblas0bc87522021-10-20 22:16:17 +020024 # To check other available versions, run the following command
25 # curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}'
26 sudo apt-get update && sudo apt-get install -y apt-transport-https
27 sudo apt-get update && sudo apt-get install -y apt-transport-https
28 curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
29 sudo add-apt-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
30 sudo apt-get update
31 echo "Installing Kubernetes Packages ..."
32 sudo apt-get install -y kubelet=${K8S_VERSION} kubeadm=${K8S_VERSION} kubectl=${K8S_VERSION}
aticig1f2e2e92022-02-03 16:42:39 +030033 cat << EOF | sudo tee -a /etc/default/kubelet
garciadeblasfa3eb332022-11-15 14:11:56 +010034KUBELET_EXTRA_ARGS="--cgroup-driver=cgroupfs"
aticig1f2e2e92022-02-03 16:42:39 +030035EOF
garciadeblas0bc87522021-10-20 22:16:17 +020036 sudo apt-mark hold kubelet kubeadm kubectl
37 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
38}
39
garciadeblas8fed1082022-08-29 11:25:02 +020040# check and track kube packages installation
41function check_and_track_kube_install() {
42 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
43 kubelet_version=$(dpkg -s kubelet|grep Version|awk '{print $2}')
44 [ -n "${kubelet_version}" ] || FATAL_TRACK k8scluster "Kubelet was not installed."
45 kubeadm_version=$(dpkg -s kubeadm|grep Version|awk '{print $2}')
46 [ -n "${kubeadm_version}" ] || FATAL_TRACK k8scluster "Kubeadm was not installed."
47 kubectl_version=$(dpkg -s kubectl|grep Version|awk '{print $2}')
48 [ -n "${kubectl_version}" ] || FATAL_TRACK k8scluster "Kubectl was not installed."
49 track k8scluster install_k8s_ok kubelet ${kubelet_version} none none kubeadm ${kubeadm_version} none none kubectl ${kubectl_version} none none
50 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
51}
52
53# initializes kubernetes control plane
garciadeblas0bc87522021-10-20 22:16:17 +020054function init_kubeadm() {
55 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
56 sudo swapoff -a
57 sudo sed -i.bak '/.*none.*swap/s/^\(.*\)$/#\1/g' /etc/fstab
garciadeblas8fed1082022-08-29 11:25:02 +020058 sudo kubeadm init --dry-run || FATAL_TRACK k8scluster "kubeadm init dry-run failed"
garciadeblas0bc87522021-10-20 22:16:17 +020059 sudo kubeadm init --config $1
60 sleep 5
61 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
62}
63
garciadeblas8fed1082022-08-29 11:25:02 +020064# Initializes kubeconfig file
garciadeblas0bc87522021-10-20 22:16:17 +020065function kube_config_dir() {
66 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
67 K8S_MANIFEST_DIR="/etc/kubernetes/manifests"
garciadeblas8fed1082022-08-29 11:25:02 +020068 [ ! -d $K8S_MANIFEST_DIR ] && FATAL_TRACK k8scluster "Kubernetes folder $K8S_MANIFEST_DIR was not found"
garciadeblas0bc87522021-10-20 22:16:17 +020069 mkdir -p $HOME/.kube
70 sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
71 sudo chown $(id -u):$(id -g) $HOME/.kube/config
72 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
73}
74
garciadeblas8fed1082022-08-29 11:25:02 +020075# test kubernetes installation
76function check_and_track_init_k8s() {
77 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
78 kubectl get ns || FATAL_TRACK k8scluster "Failed getting namespaces"
79 track k8scluster init_k8s_ok
80 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
81}
82
83# deploys flannel as daemonsets
garciadeblas0bc87522021-10-20 22:16:17 +020084function deploy_cni_provider() {
85 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
86 CNI_DIR="$(mktemp -d -q --tmpdir "flannel.XXXXXX")"
87 trap 'rm -rf "${CNI_DIR}"' EXIT
garciadeblas8fed1082022-08-29 11:25:02 +020088 KUBE_FLANNEL_FILE_URL="https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
89 wget --retry-on-host-error --retry-on-http-error 404,429,503 --tries=5 "${KUBE_FLANNEL_FILE_URL}" -P $CNI_DIR
90 [ ! -f $CNI_DIR/kube-flannel.yml ] && FATAL_TRACK k8scluster "Cannot Install Flannel because $CNI_DIR/kube-flannel.yml was not found. Maybe the file ${KUBE_FLANNEL_FILE_URL} is temporarily not accessible"
garciadeblas0bc87522021-10-20 22:16:17 +020091 kubectl apply -f $CNI_DIR
garciadeblas8fed1082022-08-29 11:25:02 +020092 [ $? -ne 0 ] && FATAL_TRACK k8scluster "Cannot Install Flannel"
garciadeblas0bc87522021-10-20 22:16:17 +020093 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
94}
95
garciadeblas8fed1082022-08-29 11:25:02 +020096# taints K8s master node
garciadeblas0bc87522021-10-20 22:16:17 +020097function taint_master_node() {
98 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
99 K8S_MASTER=$(kubectl get nodes | awk '$3~/master/'| awk '{print $1}')
100 kubectl taint node $K8S_MASTER node-role.kubernetes.io/master:NoSchedule-
101 sleep 5
102 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
103}
104
garciadeblas8fed1082022-08-29 11:25:02 +0200105# check and track kube packages installation
106function check_and_track_k8s_ready_before_helm() {
107 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
108 kubectl get events || FATAL_TRACK k8scluster "Failed getting events"
109 track k8scluster k8s_ready_before_helm
110 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
111}
112
garciadeblas0bc87522021-10-20 22:16:17 +0200113#Install Helm v3
garciadeblasc1ae2392021-12-14 18:02:30 +0100114#Helm releases can be found here: https://github.com/helm/helm/releases
garciadeblas0bc87522021-10-20 22:16:17 +0200115function install_helm() {
116 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblasc1ae2392021-12-14 18:02:30 +0100117 HELM_VERSION="v3.7.2"
118 if ! [[ "$(helm version --short 2>/dev/null)" =~ ^v3.* ]]; then
garciadeblas0bc87522021-10-20 22:16:17 +0200119 # Helm is not installed. Install helm
garciadeblasc1ae2392021-12-14 18:02:30 +0100120 echo "Helm3 is not installed, installing ..."
121 curl https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz --output helm-${HELM_VERSION}.tar.gz
122 tar -zxvf helm-${HELM_VERSION}.tar.gz
garciadeblas0bc87522021-10-20 22:16:17 +0200123 sudo mv linux-amd64/helm /usr/local/bin/helm
124 rm -r linux-amd64
garciadeblasc1ae2392021-12-14 18:02:30 +0100125 rm helm-${HELM_VERSION}.tar.gz
126 else
127 echo "Helm3 is already installed. Skipping installation..."
garciadeblas0bc87522021-10-20 22:16:17 +0200128 fi
garciadeblas8fed1082022-08-29 11:25:02 +0200129 helm version || FATAL_TRACK k8scluster "Could not obtain helm version. Maybe helm client was not installed"
130 helm repo add stable https://charts.helm.sh/stable || FATAL_TRACK k8scluster "Helm repo stable could not be added"
131 helm repo update || FATAL_TRACK k8scluster "Helm repo stable could not be updated"
garciadeblas0bc87522021-10-20 22:16:17 +0200132 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
133}
134
135function install_k8s_storageclass() {
136 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
aticig1f2e2e92022-02-03 16:42:39 +0300137 echo "Installing open-iscsi"
138 sudo apt-get update
139 sudo apt-get install open-iscsi
140 sudo systemctl enable --now iscsid
141 OPENEBS_VERSION="3.1.0"
garciadeblas0bc87522021-10-20 22:16:17 +0200142 echo "Installing OpenEBS"
143 helm repo add openebs https://openebs.github.io/charts
144 helm repo update
145 helm install --create-namespace --namespace openebs openebs openebs/openebs --version ${OPENEBS_VERSION}
146 helm ls -n openebs
147 local storageclass_timeout=400
148 local counter=0
149 local storageclass_ready=""
150 echo "Waiting for storageclass"
151 while (( counter < storageclass_timeout ))
152 do
153 kubectl get storageclass openebs-hostpath &> /dev/null
154
155 if [ $? -eq 0 ] ; then
156 echo "Storageclass available"
157 storageclass_ready="y"
158 break
159 else
160 counter=$((counter + 15))
161 sleep 15
162 fi
163 done
garciadeblas8fed1082022-08-29 11:25:02 +0200164 [ -n "$storageclass_ready" ] || FATAL_TRACK k8scluster "Storageclass not ready after $storageclass_timeout seconds. Cannot install openebs"
garciadeblas0bc87522021-10-20 22:16:17 +0200165 kubectl patch storageclass openebs-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
166 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
167}
168
169#installs metallb from helm
170function install_helm_metallb() {
171 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
172 echo "Installing MetalLB"
173 METALLB_VERSION="0.11.0"
174 METALLB_IP_RANGE="$DEFAULT_IP/32"
175 echo "configInline:
176 address-pools:
177 - name: default
178 protocol: layer2
179 addresses:
180 - $METALLB_IP_RANGE" | sudo tee -a ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
181 helm repo add metallb https://metallb.github.io/metallb
182 helm repo update
183 helm install --create-namespace --namespace metallb-system metallb metallb/metallb --version ${METALLB_VERSION} -f ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
184 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
185}
186
Gabriel Cuba0d4965f2022-11-06 19:39:02 -0500187#installs cert-manager
188function install_helm_certmanager() {
189 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
190 echo "Installing cert-manager"
191 CERTMANAGER_VERSION="v1.9.1"
192 helm repo add jetstack https://charts.jetstack.io
193 helm repo update
194 helm install cert-manager --create-namespace --namespace cert-manager jetstack/cert-manager \
195 --version ${CERTMANAGER_VERSION} --set installCRDs=true --set prometheus.enabled=false \
196 --set clusterResourceNamespace=osm \
197 --set extraArgs="{--enable-certificate-owner-ref=true}"
198 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
199}
200
garciadeblas0bc87522021-10-20 22:16:17 +0200201#checks openebs and metallb readiness
202function check_for_readiness() {
203 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
204 # Default input values
205 sampling_period=2 # seconds
206 time_for_readiness=20 # seconds ready
207 time_for_failure=200 # seconds broken
208 OPENEBS_NAMESPACE=openebs
209 METALLB_NAMESPACE=metallb-system
210 # STACK_NAME=osm # By default, "osm"
211
212 # Equivalent number of samples
213 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
214 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
215 failures_in_a_row=0
216 oks_in_a_row=0
217
218 ####################################################################################
219 # Loop to check system readiness
220 ####################################################################################
221 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
222 do
223 # State of OpenEBS
224 OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
225 OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
226 OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
227 COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l)
228 COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l)
229
230 # State of MetalLB
231 METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1)
232 METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
233 METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
234 COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l)
235 COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l)
236
Gabriel Cuba0d4965f2022-11-06 19:39:02 -0500237 # State of CertManager
238 CERTMANAGER_STATE=$(kubectl get pod -n ${CERTMANAGER_NAMESPACE} --no-headers 2>&1)
239 CERTMANAGER_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
240 CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
241 COUNT_CERTMANAGER_READY=$(echo "${CERTMANAGER_READY}" | grep -v -e '^$' | wc -l)
242 COUNT_CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_NOT_READY}" | grep -v -e '^$' | wc -l)
243
garciadeblas0bc87522021-10-20 22:16:17 +0200244 # OK sample
245 if [[ $((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
246 then
247 ((++oks_in_a_row))
248 failures_in_a_row=0
249 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
250 # NOK sample
251 else
252 ((++failures_in_a_row))
253 oks_in_a_row=0
254 echo
255 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
256
257 # Reports failed pods in OpenEBS
258 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
259 then
260 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
261 echo "${OPENEBS_NOT_READY}"
262 echo
263 fi
264
Gabriel Cuba0d4965f2022-11-06 19:39:02 -0500265 # Reports failed pods in MetalLB
garciadeblas0bc87522021-10-20 22:16:17 +0200266 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
267 then
268 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
269 echo "${METALLB_NOT_READY}"
270 echo
271 fi
Gabriel Cuba0d4965f2022-11-06 19:39:02 -0500272
273 # Reports failed pods in CertManager
274 if [[ "${COUNT_CERTMANAGER_NOT_READY}" -ne 0 ]]
275 then
276 echo "CertManager: Waiting for ${COUNT_CERTMANAGER_NOT_READY} of $((${COUNT_CERTMANAGER_NOT_READY}+${COUNT_CERTMANAGER_READY})) pods to be ready:"
277 echo "${CERTMANAGER_NOT_READY}"
278 echo
279 fi
garciadeblas0bc87522021-10-20 22:16:17 +0200280 fi
281
282 #------------ NEXT SAMPLE
283 sleep ${sampling_period}
284 done
285
286 ####################################################################################
287 # OUTCOME
288 ####################################################################################
289 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
290 then
291 echo
garciadeblas8fed1082022-08-29 11:25:02 +0200292 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
garciadeblas0bc87522021-10-20 22:16:17 +0200293 else
294 echo
295 echo "K8S CLUSTER IS READY"
296 fi
297 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
298}
299
300#removes osm deployments and services
301function remove_k8s_namespace() {
302 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblas91f26242022-01-12 09:58:50 +0100303 kubectl delete ns $1 2>&1 >/dev/null
garciadeblas0bc87522021-10-20 22:16:17 +0200304 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
305}
306
307# main
308while getopts ":D:d:i:-: " o; do
309 case "${o}" in
310 i)
311 DEFAULT_IP="${OPTARG}"
312 ;;
313 d)
314 OSM_DOCKER_WORK_DIR="${OPTARG}"
315 ;;
316 D)
317 OSM_DEVOPS="${OPTARG}"
318 ;;
319 -)
320 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
321 echo -e "Invalid option: '--$OPTARG'\n" >&2
322 exit 1
323 ;;
324 :)
325 echo "Option -$OPTARG requires an argument" >&2
326 exit 1
327 ;;
328 \?)
329 echo -e "Invalid option: '-$OPTARG'\n" >&2
330 exit 1
331 ;;
332 *)
333 exit 1
334 ;;
335 esac
336done
337
338source $OSM_DEVOPS/common/logging
339source $OSM_DEVOPS/common/track
340
341echo "DEBUG_INSTALL=$DEBUG_INSTALL"
342echo "DEFAULT_IP=$DEFAULT_IP"
343echo "OSM_DEVOPS=$OSM_DEVOPS"
344echo "OSM_DOCKER_WORK_DIR=$OSM_DOCKER_WORK_DIR"
345echo "INSTALL_K8S_MONITOR=$INSTALL_K8S_MONITOR"
346echo "HOME=$HOME"
347
348
349install_kube
garciadeblas8fed1082022-08-29 11:25:02 +0200350check_and_track_kube_install
351
garciadeblas0bc87522021-10-20 22:16:17 +0200352init_kubeadm $OSM_DOCKER_WORK_DIR/cluster-config.yaml
353kube_config_dir
garciadeblas8fed1082022-08-29 11:25:02 +0200354check_and_track_init_k8s
355
garciadeblas0bc87522021-10-20 22:16:17 +0200356if [ -n "$INSTALL_K8S_MONITOR" ]; then
357 # uninstall OSM MONITORING
358 uninstall_k8s_monitoring
garciadeblas4d89c372021-11-25 11:57:18 +0100359 track k8scluster uninstall_k8s_monitoring_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200360fi
garciadeblas8fed1082022-08-29 11:25:02 +0200361
garciadeblas0bc87522021-10-20 22:16:17 +0200362remove_k8s_namespace osm
363deploy_cni_provider
364taint_master_node
garciadeblas8fed1082022-08-29 11:25:02 +0200365check_and_track_k8s_ready_before_helm
366
garciadeblas0bc87522021-10-20 22:16:17 +0200367install_helm
garciadeblas4d89c372021-11-25 11:57:18 +0100368track k8scluster install_helm_ok
garciadeblas8fed1082022-08-29 11:25:02 +0200369
garciadeblas0bc87522021-10-20 22:16:17 +0200370install_k8s_storageclass
garciadeblas4d89c372021-11-25 11:57:18 +0100371track k8scluster k8s_storageclass_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200372install_helm_metallb
garciadeblas4d89c372021-11-25 11:57:18 +0100373track k8scluster k8s_metallb_ok
Gabriel Cuba0d4965f2022-11-06 19:39:02 -0500374install_helm_certmanager
375track k8scluster k8s_certmanager_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200376check_for_readiness
garciadeblas4d89c372021-11-25 11:57:18 +0100377track k8scluster k8s_ready_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200378