blob: f31889cb9531b9e2fe6a330f0a8e52d0bdc3631e [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
18#installs kubernetes packages
19function install_kube() {
20 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
aticig8eff33a2022-02-02 12:09:05 +030021 K8S_VERSION=1.23.3-00
garciadeblas5bbe3802021-12-14 18:02:30 +010022 # Kubernetes releases can be found here: https://kubernetes.io/releases/
garciadeblas0bc87522021-10-20 22:16:17 +020023 # To check other available versions, run the following command
24 # curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}'
25 sudo apt-get update && sudo apt-get install -y apt-transport-https
26 sudo apt-get update && sudo apt-get install -y apt-transport-https
27 curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
28 sudo add-apt-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
29 sudo apt-get update
30 echo "Installing Kubernetes Packages ..."
31 sudo apt-get install -y kubelet=${K8S_VERSION} kubeadm=${K8S_VERSION} kubectl=${K8S_VERSION}
aticig8eff33a2022-02-02 12:09:05 +030032 cat << EOF | sudo tee -a /etc/default/kubelet
33 KUBELET_EXTRA_ARGS="--cgroup-driver=cgroupfs"
34EOF
garciadeblas0bc87522021-10-20 22:16:17 +020035 sudo apt-mark hold kubelet kubeadm kubectl
36 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
37}
38
39#initializes kubernetes control plane
40function init_kubeadm() {
41 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
42 sudo swapoff -a
43 sudo sed -i.bak '/.*none.*swap/s/^\(.*\)$/#\1/g' /etc/fstab
44 sudo kubeadm init --config $1
45 sleep 5
46 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
47}
48
49function kube_config_dir() {
50 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
51 K8S_MANIFEST_DIR="/etc/kubernetes/manifests"
52 [ ! -d $K8S_MANIFEST_DIR ] && FATAL "Cannot Install Kubernetes"
53 mkdir -p $HOME/.kube
54 sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
55 sudo chown $(id -u):$(id -g) $HOME/.kube/config
56 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
57}
58
59#deploys flannel as daemonsets
60function deploy_cni_provider() {
61 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
62 CNI_DIR="$(mktemp -d -q --tmpdir "flannel.XXXXXX")"
63 trap 'rm -rf "${CNI_DIR}"' EXIT
64 wget -q https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml -P $CNI_DIR
65 kubectl apply -f $CNI_DIR
66 [ $? -ne 0 ] && FATAL "Cannot Install Flannel"
67 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
68}
69
70#taints K8s master node
71function taint_master_node() {
72 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
73 K8S_MASTER=$(kubectl get nodes | awk '$3~/master/'| awk '{print $1}')
74 kubectl taint node $K8S_MASTER node-role.kubernetes.io/master:NoSchedule-
75 sleep 5
76 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
77}
78
79#Install Helm v3
garciadeblas5bbe3802021-12-14 18:02:30 +010080#Helm releases can be found here: https://github.com/helm/helm/releases
garciadeblas0bc87522021-10-20 22:16:17 +020081function install_helm() {
82 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblas5bbe3802021-12-14 18:02:30 +010083 HELM_VERSION="v3.7.2"
84 if ! [[ "$(helm version --short 2>/dev/null)" =~ ^v3.* ]]; then
garciadeblas0bc87522021-10-20 22:16:17 +020085 # Helm is not installed. Install helm
garciadeblas5bbe3802021-12-14 18:02:30 +010086 echo "Helm3 is not installed, installing ..."
87 curl https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz --output helm-${HELM_VERSION}.tar.gz
88 tar -zxvf helm-${HELM_VERSION}.tar.gz
garciadeblas0bc87522021-10-20 22:16:17 +020089 sudo mv linux-amd64/helm /usr/local/bin/helm
90 rm -r linux-amd64
garciadeblas5bbe3802021-12-14 18:02:30 +010091 rm helm-${HELM_VERSION}.tar.gz
92 else
93 echo "Helm3 is already installed. Skipping installation..."
garciadeblas0bc87522021-10-20 22:16:17 +020094 fi
garciadeblas5bbe3802021-12-14 18:02:30 +010095 helm repo add stable https://charts.helm.sh/stable
96 helm repo update
garciadeblas0bc87522021-10-20 22:16:17 +020097 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
98}
99
100function install_k8s_storageclass() {
101 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
aticig8eff33a2022-02-02 12:09:05 +0300102 echo "Installing open-iscsi"
103 sudo apt-get update
104 sudo apt-get install open-iscsi
105 sudo systemctl enable --now iscsid
106 OPENEBS_VERSION="3.1.0"
garciadeblas0bc87522021-10-20 22:16:17 +0200107 echo "Installing OpenEBS"
108 helm repo add openebs https://openebs.github.io/charts
109 helm repo update
110 helm install --create-namespace --namespace openebs openebs openebs/openebs --version ${OPENEBS_VERSION}
111 helm ls -n openebs
112 local storageclass_timeout=400
113 local counter=0
114 local storageclass_ready=""
115 echo "Waiting for storageclass"
116 while (( counter < storageclass_timeout ))
117 do
118 kubectl get storageclass openebs-hostpath &> /dev/null
119
120 if [ $? -eq 0 ] ; then
121 echo "Storageclass available"
122 storageclass_ready="y"
123 break
124 else
125 counter=$((counter + 15))
126 sleep 15
127 fi
128 done
129 [ -n "$storageclass_ready" ] || FATAL "Storageclass not ready after $storageclass_timeout seconds. Cannot install openebs"
130 kubectl patch storageclass openebs-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
131 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
132}
133
134#installs metallb from helm
135function install_helm_metallb() {
136 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
137 echo "Installing MetalLB"
138 METALLB_VERSION="0.11.0"
139 METALLB_IP_RANGE="$DEFAULT_IP/32"
140 echo "configInline:
141 address-pools:
142 - name: default
143 protocol: layer2
144 addresses:
145 - $METALLB_IP_RANGE" | sudo tee -a ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
146 helm repo add metallb https://metallb.github.io/metallb
147 helm repo update
148 helm install --create-namespace --namespace metallb-system metallb metallb/metallb --version ${METALLB_VERSION} -f ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
149 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
150}
151
152#checks openebs and metallb readiness
153function check_for_readiness() {
154 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
155 # Default input values
156 sampling_period=2 # seconds
157 time_for_readiness=20 # seconds ready
158 time_for_failure=200 # seconds broken
159 OPENEBS_NAMESPACE=openebs
160 METALLB_NAMESPACE=metallb-system
161 # STACK_NAME=osm # By default, "osm"
162
163 # Equivalent number of samples
164 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
165 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
166 failures_in_a_row=0
167 oks_in_a_row=0
168
169 ####################################################################################
170 # Loop to check system readiness
171 ####################################################################################
172 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
173 do
174 # State of OpenEBS
175 OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
176 OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
177 OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
178 COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l)
179 COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l)
180
181 # State of MetalLB
182 METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1)
183 METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
184 METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
185 COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l)
186 COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l)
187
188 # OK sample
189 if [[ $((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
190 then
191 ((++oks_in_a_row))
192 failures_in_a_row=0
193 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
194 # NOK sample
195 else
196 ((++failures_in_a_row))
197 oks_in_a_row=0
198 echo
199 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
200
201 # Reports failed pods in OpenEBS
202 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
203 then
204 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
205 echo "${OPENEBS_NOT_READY}"
206 echo
207 fi
208
209 # Reports failed statefulsets
210 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
211 then
212 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
213 echo "${METALLB_NOT_READY}"
214 echo
215 fi
216 fi
217
218 #------------ NEXT SAMPLE
219 sleep ${sampling_period}
220 done
221
222 ####################################################################################
223 # OUTCOME
224 ####################################################################################
225 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
226 then
227 echo
228 FATAL "K8S CLUSTER IS BROKEN"
229 else
230 echo
231 echo "K8S CLUSTER IS READY"
232 fi
233 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
234}
235
236#removes osm deployments and services
237function remove_k8s_namespace() {
238 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
239 kubectl delete ns $1
240 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
241}
242
243# main
244while getopts ":D:d:i:-: " o; do
245 case "${o}" in
246 i)
247 DEFAULT_IP="${OPTARG}"
248 ;;
249 d)
250 OSM_DOCKER_WORK_DIR="${OPTARG}"
251 ;;
252 D)
253 OSM_DEVOPS="${OPTARG}"
254 ;;
255 -)
256 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
257 echo -e "Invalid option: '--$OPTARG'\n" >&2
258 exit 1
259 ;;
260 :)
261 echo "Option -$OPTARG requires an argument" >&2
262 exit 1
263 ;;
264 \?)
265 echo -e "Invalid option: '-$OPTARG'\n" >&2
266 exit 1
267 ;;
268 *)
269 exit 1
270 ;;
271 esac
272done
273
274source $OSM_DEVOPS/common/logging
275source $OSM_DEVOPS/common/track
276
277echo "DEBUG_INSTALL=$DEBUG_INSTALL"
278echo "DEFAULT_IP=$DEFAULT_IP"
279echo "OSM_DEVOPS=$OSM_DEVOPS"
280echo "OSM_DOCKER_WORK_DIR=$OSM_DOCKER_WORK_DIR"
281echo "INSTALL_K8S_MONITOR=$INSTALL_K8S_MONITOR"
282echo "HOME=$HOME"
283
284
285install_kube
garciadeblasc0d02342021-11-25 11:57:18 +0100286track k8scluster install_k8s_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200287init_kubeadm $OSM_DOCKER_WORK_DIR/cluster-config.yaml
288kube_config_dir
garciadeblasc0d02342021-11-25 11:57:18 +0100289track k8scluster init_k8s_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200290if [ -n "$INSTALL_K8S_MONITOR" ]; then
291 # uninstall OSM MONITORING
292 uninstall_k8s_monitoring
garciadeblasc0d02342021-11-25 11:57:18 +0100293 track k8scluster uninstall_k8s_monitoring_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200294fi
295#remove old namespace
296remove_k8s_namespace osm
297deploy_cni_provider
298taint_master_node
299install_helm
garciadeblasc0d02342021-11-25 11:57:18 +0100300track k8scluster install_helm_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200301install_k8s_storageclass
garciadeblasc0d02342021-11-25 11:57:18 +0100302track k8scluster k8s_storageclass_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200303install_helm_metallb
garciadeblasc0d02342021-11-25 11:57:18 +0100304track k8scluster k8s_metallb_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200305check_for_readiness
garciadeblasc0d02342021-11-25 11:57:18 +0100306track k8scluster k8s_ready_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200307