blob: babf17ca5f068cedca9a0760a567a1bd34f46785 [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
Pedro Escaleira8e91a952024-03-04 17:07:11 +000018K8S_VERSION=1.24
19K8S_PACKAGE_VERSION="$K8S_VERSION".17-1.1
20HELM_VERSION="v3.10.3"
21
22# installs kubernetes packages
garciadeblas0bc87522021-10-20 22:16:17 +020023function install_kube() {
24 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblasc1ae2392021-12-14 18:02:30 +010025 # Kubernetes releases can be found here: https://kubernetes.io/releases/
garciadeblas0bc87522021-10-20 22:16:17 +020026 # To check other available versions, run the following command
27 # curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}'
Pedro Escaleira8e91a952024-03-04 17:07:11 +000028 sudo mkdir /etc/apt/keyrings
29 sudo apt-get -y update && sudo apt-get install -y apt-transport-https ca-certificates curl
30 curl -fsSL https://pkgs.k8s.io/core:/stable:/v"$K8S_VERSION"/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
31 echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v'$K8S_VERSION'/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
32 sudo apt-get -y update
garciadeblas0bc87522021-10-20 22:16:17 +020033 echo "Installing Kubernetes Packages ..."
Pedro Escaleira8e91a952024-03-04 17:07:11 +000034 sudo apt-get install -y kubelet=${K8S_PACKAGE_VERSION} kubeadm=${K8S_PACKAGE_VERSION} kubectl=${K8S_PACKAGE_VERSION}
garciadeblas0bc87522021-10-20 22:16:17 +020035 sudo apt-mark hold kubelet kubeadm kubectl
Pedro Escaleira8e91a952024-03-04 17:07:11 +000036
37 sudo rm /etc/containerd/config.toml
38 sudo systemctl restart containerd
39
garciadeblas0bc87522021-10-20 22:16:17 +020040 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
41}
42
43#initializes kubernetes control plane
44function init_kubeadm() {
45 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
46 sudo swapoff -a
47 sudo sed -i.bak '/.*none.*swap/s/^\(.*\)$/#\1/g' /etc/fstab
48 sudo kubeadm init --config $1
49 sleep 5
50 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
51}
52
53function kube_config_dir() {
54 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
55 K8S_MANIFEST_DIR="/etc/kubernetes/manifests"
56 [ ! -d $K8S_MANIFEST_DIR ] && FATAL "Cannot Install Kubernetes"
57 mkdir -p $HOME/.kube
58 sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
59 sudo chown $(id -u):$(id -g) $HOME/.kube/config
60 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
61}
62
63#deploys flannel as daemonsets
64function deploy_cni_provider() {
65 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
66 CNI_DIR="$(mktemp -d -q --tmpdir "flannel.XXXXXX")"
67 trap 'rm -rf "${CNI_DIR}"' EXIT
garciadeblasc2d10ed2022-01-21 13:57:06 +010068 wget --retry-on-host-error --retry-on-http-error 404,429,503 --tries=5 https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml -P $CNI_DIR
69 [ ! -f $CNI_DIR/kube-flannel.yml ] && FATAL "Cannot Install Flannel because $CNI_DIR/kube-flannel.yml was not found. Maybe the file https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml is temporarily not accessible"
garciadeblas0bc87522021-10-20 22:16:17 +020070 kubectl apply -f $CNI_DIR
71 [ $? -ne 0 ] && FATAL "Cannot Install Flannel"
72 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
73}
74
75#taints K8s master node
76function taint_master_node() {
77 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
Pedro Escaleira8e91a952024-03-04 17:07:11 +000078 K8S_MASTER=$(kubectl get nodes | awk '$3~/control-plane/'| awk '{print $1}')
garciadeblas0bc87522021-10-20 22:16:17 +020079 kubectl taint node $K8S_MASTER node-role.kubernetes.io/master:NoSchedule-
Pedro Escaleira8e91a952024-03-04 17:07:11 +000080 kubectl taint node $K8S_MASTER node-role.kubernetes.io/control-plane:NoSchedule-
garciadeblas0bc87522021-10-20 22:16:17 +020081 sleep 5
82 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
83}
84
85#Install Helm v3
garciadeblasc1ae2392021-12-14 18:02:30 +010086#Helm releases can be found here: https://github.com/helm/helm/releases
garciadeblas0bc87522021-10-20 22:16:17 +020087function install_helm() {
88 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblasc1ae2392021-12-14 18:02:30 +010089 if ! [[ "$(helm version --short 2>/dev/null)" =~ ^v3.* ]]; then
garciadeblas0bc87522021-10-20 22:16:17 +020090 # Helm is not installed. Install helm
garciadeblasc1ae2392021-12-14 18:02:30 +010091 echo "Helm3 is not installed, installing ..."
92 curl https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz --output helm-${HELM_VERSION}.tar.gz
93 tar -zxvf helm-${HELM_VERSION}.tar.gz
garciadeblas0bc87522021-10-20 22:16:17 +020094 sudo mv linux-amd64/helm /usr/local/bin/helm
95 rm -r linux-amd64
garciadeblasc1ae2392021-12-14 18:02:30 +010096 rm helm-${HELM_VERSION}.tar.gz
97 else
98 echo "Helm3 is already installed. Skipping installation..."
garciadeblas0bc87522021-10-20 22:16:17 +020099 fi
garciadeblasc1ae2392021-12-14 18:02:30 +0100100 helm repo add stable https://charts.helm.sh/stable
101 helm repo update
garciadeblas0bc87522021-10-20 22:16:17 +0200102 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
103}
104
105function install_k8s_storageclass() {
106 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
aticig1f2e2e92022-02-03 16:42:39 +0300107 echo "Installing open-iscsi"
108 sudo apt-get update
109 sudo apt-get install open-iscsi
110 sudo systemctl enable --now iscsid
111 OPENEBS_VERSION="3.1.0"
garciadeblas0bc87522021-10-20 22:16:17 +0200112 echo "Installing OpenEBS"
113 helm repo add openebs https://openebs.github.io/charts
114 helm repo update
115 helm install --create-namespace --namespace openebs openebs openebs/openebs --version ${OPENEBS_VERSION}
116 helm ls -n openebs
117 local storageclass_timeout=400
118 local counter=0
119 local storageclass_ready=""
120 echo "Waiting for storageclass"
121 while (( counter < storageclass_timeout ))
122 do
123 kubectl get storageclass openebs-hostpath &> /dev/null
124
125 if [ $? -eq 0 ] ; then
126 echo "Storageclass available"
127 storageclass_ready="y"
128 break
129 else
130 counter=$((counter + 15))
131 sleep 15
132 fi
133 done
134 [ -n "$storageclass_ready" ] || FATAL "Storageclass not ready after $storageclass_timeout seconds. Cannot install openebs"
135 kubectl patch storageclass openebs-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
136 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
137}
138
139#installs metallb from helm
140function install_helm_metallb() {
141 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
142 echo "Installing MetalLB"
143 METALLB_VERSION="0.11.0"
144 METALLB_IP_RANGE="$DEFAULT_IP/32"
145 echo "configInline:
146 address-pools:
147 - name: default
148 protocol: layer2
149 addresses:
150 - $METALLB_IP_RANGE" | sudo tee -a ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
151 helm repo add metallb https://metallb.github.io/metallb
152 helm repo update
153 helm install --create-namespace --namespace metallb-system metallb metallb/metallb --version ${METALLB_VERSION} -f ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
154 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
155}
156
157#checks openebs and metallb readiness
158function check_for_readiness() {
159 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
160 # Default input values
161 sampling_period=2 # seconds
162 time_for_readiness=20 # seconds ready
163 time_for_failure=200 # seconds broken
164 OPENEBS_NAMESPACE=openebs
165 METALLB_NAMESPACE=metallb-system
166 # STACK_NAME=osm # By default, "osm"
167
168 # Equivalent number of samples
169 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
170 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
171 failures_in_a_row=0
172 oks_in_a_row=0
173
174 ####################################################################################
175 # Loop to check system readiness
176 ####################################################################################
177 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
178 do
179 # State of OpenEBS
180 OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
181 OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
182 OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
183 COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l)
184 COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l)
185
186 # State of MetalLB
187 METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1)
188 METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
189 METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
190 COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l)
191 COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l)
192
193 # OK sample
194 if [[ $((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
195 then
196 ((++oks_in_a_row))
197 failures_in_a_row=0
198 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
199 # NOK sample
200 else
201 ((++failures_in_a_row))
202 oks_in_a_row=0
203 echo
204 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
205
206 # Reports failed pods in OpenEBS
207 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
208 then
209 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
210 echo "${OPENEBS_NOT_READY}"
211 echo
212 fi
213
214 # Reports failed statefulsets
215 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
216 then
217 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
218 echo "${METALLB_NOT_READY}"
219 echo
220 fi
221 fi
222
223 #------------ NEXT SAMPLE
224 sleep ${sampling_period}
225 done
226
227 ####################################################################################
228 # OUTCOME
229 ####################################################################################
230 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
231 then
232 echo
233 FATAL "K8S CLUSTER IS BROKEN"
234 else
235 echo
236 echo "K8S CLUSTER IS READY"
237 fi
238 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
239}
240
241#removes osm deployments and services
242function remove_k8s_namespace() {
243 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblas91f26242022-01-12 09:58:50 +0100244 kubectl delete ns $1 2>&1 >/dev/null
garciadeblas0bc87522021-10-20 22:16:17 +0200245 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
246}
247
248# main
249while getopts ":D:d:i:-: " o; do
250 case "${o}" in
251 i)
252 DEFAULT_IP="${OPTARG}"
253 ;;
254 d)
255 OSM_DOCKER_WORK_DIR="${OPTARG}"
256 ;;
257 D)
258 OSM_DEVOPS="${OPTARG}"
259 ;;
260 -)
261 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
262 echo -e "Invalid option: '--$OPTARG'\n" >&2
263 exit 1
264 ;;
265 :)
266 echo "Option -$OPTARG requires an argument" >&2
267 exit 1
268 ;;
269 \?)
270 echo -e "Invalid option: '-$OPTARG'\n" >&2
271 exit 1
272 ;;
273 *)
274 exit 1
275 ;;
276 esac
277done
278
279source $OSM_DEVOPS/common/logging
280source $OSM_DEVOPS/common/track
281
282echo "DEBUG_INSTALL=$DEBUG_INSTALL"
283echo "DEFAULT_IP=$DEFAULT_IP"
284echo "OSM_DEVOPS=$OSM_DEVOPS"
285echo "OSM_DOCKER_WORK_DIR=$OSM_DOCKER_WORK_DIR"
286echo "INSTALL_K8S_MONITOR=$INSTALL_K8S_MONITOR"
287echo "HOME=$HOME"
288
289
290install_kube
garciadeblas4d89c372021-11-25 11:57:18 +0100291track k8scluster install_k8s_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200292init_kubeadm $OSM_DOCKER_WORK_DIR/cluster-config.yaml
293kube_config_dir
garciadeblas4d89c372021-11-25 11:57:18 +0100294track k8scluster init_k8s_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200295if [ -n "$INSTALL_K8S_MONITOR" ]; then
296 # uninstall OSM MONITORING
297 uninstall_k8s_monitoring
garciadeblas4d89c372021-11-25 11:57:18 +0100298 track k8scluster uninstall_k8s_monitoring_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200299fi
300#remove old namespace
301remove_k8s_namespace osm
302deploy_cni_provider
303taint_master_node
304install_helm
garciadeblas4d89c372021-11-25 11:57:18 +0100305track k8scluster install_helm_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200306install_k8s_storageclass
garciadeblas4d89c372021-11-25 11:57:18 +0100307track k8scluster k8s_storageclass_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200308install_helm_metallb
garciadeblas4d89c372021-11-25 11:57:18 +0100309track k8scluster k8s_metallb_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200310check_for_readiness
garciadeblas4d89c372021-11-25 11:57:18 +0100311track k8scluster k8s_ready_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200312