blob: fea91f411e127b3fd43360f258cb42f7a58cde29 [file] [log] [blame]
garciadeblasb3797412024-06-06 14:26:24 +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# K3s releases: https://github.com/k3s-io/k3s/releases/
19K8S_VERSION="v1.29.3+k3s1"
20
garciadeblas117fd4a2024-08-21 18:18:14 +020021# configure registry
22function configure_registry() {
23 if [ -n "${DOCKER_PROXY_URL}" ]; then
24 echo "Configuring docker proxy URL in /etc/rancher/k3s/registries.yaml"
25 cat << EOF | sudo tee /etc/rancher/k3s/registries.yaml > /dev/null
26mirrors:
27 docker.io:
28 endpoint:
29 - "${DOCKER_PROXY_URL}"
30EOF
31 fi
32}
33
garciadeblasb3797412024-06-06 14:26:24 +020034# installs k3s
35function install_k3s() {
36 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
37 export INSTALL_K3S_EXEC="--disable traefik"
38 curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K8S_VERSION} sh -s -
39 sudo chmod 644 /etc/rancher/k3s/k3s.yaml
40 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
41}
42
43# updates service nodeport range
44function update_service_nodeport_range() {
45 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
46 sudo k3s server --kube-apiserver-arg=service-node-port-range=80-32767
47 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
48}
49
50# checks cluster readiness
51function check_for_readiness() {
52 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
53 # Check for Ready node, takes ~30 seconds
54 echo "Waiting for K8s nodes to be ready"
55 local time_for_failure=60 # seconds broken
56 local sampling_period=5 # seconds
57 local counter=0
58 local cluster_ready=""
59 while (( counter < time_for_failure ))
60 do
61 kubectl get nodes |grep master |grep -v none | grep Ready
62 if [ $? -eq 0 ] ; then
63 echo "K8s cluster is ready"
64 cluster_ready="y"
65 break
66 else
67 echo "K8s cluster is not ready yet"
68 counter=$((counter + sampling_period))
69 sleep ${sampling_period}
70 fi
71 done
72 [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster "K3s cluster nodes not ready after $time_for_failure seconds."
73
74 echo "Waiting for pods to be ready"
75 local time_for_readiness=20 # seconds ready
76 local time_for_failure=100 # seconds broken
77
78 # Equivalent number of samples
79 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
80 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
81 failures_in_a_row=0
82 oks_in_a_row=0
83 ####################################################################################
84 # Loop to check system readiness
85 ####################################################################################
86 K3S_NAMESPACE=kube-system
87 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
88 do
89 # State of pods rather than completed jobs
90 K3S_PODS_STATE=$(kubectl get pod -n ${K3S_NAMESPACE} --no-headers |grep -v Completed 2>&1)
91 K3S_PODS_READY=$(echo "${K3S_PODS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
92 K3S_PODS_NOT_READY=$(echo "${K3S_PODS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
93 COUNT_K3S_PODS_READY=$(echo "${K3S_PODS_READY}"| grep -v -e '^$' | wc -l)
94 COUNT_K3S_PODS_NOT_READY=$(echo "${K3S_PODS_NOT_READY}" | grep -v -e '^$' | wc -l)
95
96 # OK sample
97 if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]]
98 then
99 ((++oks_in_a_row))
100 failures_in_a_row=0
101 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
102 # NOK sample
103 else
104 ((++failures_in_a_row))
105 oks_in_a_row=0
106 echo
107 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
108
109 # Reports failed pods in K3S
110 if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]]
111 then
112 echo "K3S kube-system: Waiting for ${COUNT_K3S_PODS_NOT_READY} of $((${COUNT_K3S_PODS_NOT_READY}+${COUNT_K3S_PODS_READY})) pods to be ready:"
113 echo "${K3S_PODS_NOT_READY}"
114 echo
115 fi
116 fi
117
118 #------------ NEXT SAMPLE
119 sleep ${sampling_period}
120 done
121
122 ####################################################################################
123 # OUTCOME
124 ####################################################################################
125 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
126 then
127 echo
128 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
129 else
130 echo
131 echo "K8S CLUSTER IS READY"
132 fi
133 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
134}
135
garciadeblas1f338482024-07-04 19:26:54 +0200136# Initializes kubeconfig file
137function save_kubeconfig() {
garciadeblasb3797412024-06-06 14:26:24 +0200138 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
139 KUBEDIR="${HOME}/.kube"
140 KUBEFILE="$KUBEDIR/config"
141 mkdir -p "${KUBEDIR}"
garciadeblas1f338482024-07-04 19:26:54 +0200142 K3S_KUBECONFIG="/etc/rancher/k3s/k3s.yaml"
143 sudo cp "${K3S_KUBECONFIG}" "${KUBEFILE}"
garciadeblasb3797412024-06-06 14:26:24 +0200144 sudo chown $(id -u):$(id -g) "${KUBEFILE}"
garciadeblas1f338482024-07-04 19:26:54 +0200145 sed -i "s#server: https://127.0.0.1#server: https://${DEFAULT_IP}#g" "${KUBEFILE}"
garciadeblasb3797412024-06-06 14:26:24 +0200146 chmod 700 "${KUBEFILE}"
147 echo
148 echo "Credentials saved at ${KUBEFILE}"
149 echo
150 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
151}
152
153# main
garciadeblas117fd4a2024-08-21 18:18:14 +0200154while getopts ":D:i:p:-: " o; do
garciadeblasb3797412024-06-06 14:26:24 +0200155 case "${o}" in
garciadeblas1f338482024-07-04 19:26:54 +0200156 i)
157 DEFAULT_IP="${OPTARG}"
158 ;;
garciadeblasb3797412024-06-06 14:26:24 +0200159 D)
160 OSM_DEVOPS="${OPTARG}"
161 ;;
garciadeblas117fd4a2024-08-21 18:18:14 +0200162 p)
163 DOCKER_PROXY_URL="${OPTARG}"
164 ;;
garciadeblasb3797412024-06-06 14:26:24 +0200165 -)
166 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
167 echo -e "Invalid option: '--$OPTARG'\n" >&2
168 exit 1
169 ;;
170 :)
171 echo "Option -$OPTARG requires an argument" >&2
172 exit 1
173 ;;
174 \?)
175 echo -e "Invalid option: '-$OPTARG'\n" >&2
176 exit 1
177 ;;
178 *)
179 exit 1
180 ;;
181 esac
182done
183
garciadeblas82981162024-07-23 15:24:00 +0200184DEBUG_INSTALL=${DEBUG_INSTALL:-}
185DEFAULT_IP=${DEFAULT_IP:-"127.0.0.1"}
186OSM_DEVOPS=${OSM_DEVOPS:-"/usr/share/osm-devops"}
garciadeblas117fd4a2024-08-21 18:18:14 +0200187DOCKER_PROXY_URL=${DOCKER_PROXY_URL=-}
garciadeblas1f338482024-07-04 19:26:54 +0200188echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
189echo "DEFAULT_IP=${DEFAULT_IP}"
190echo "OSM_DEVOPS=${OSM_DEVOPS}"
garciadeblas117fd4a2024-08-21 18:18:14 +0200191echo "DOCKER_PROXY_URL=${DOCKER_PROXY_URL}"
garciadeblasb3797412024-06-06 14:26:24 +0200192echo "HOME=$HOME"
193
garciadeblas82981162024-07-23 15:24:00 +0200194source $OSM_DEVOPS/common/logging
195source $OSM_DEVOPS/common/track
196
garciadeblas117fd4a2024-08-21 18:18:14 +0200197configure_registry
garciadeblasb3797412024-06-06 14:26:24 +0200198install_k3s
199track k8scluster k3s_install_ok
200check_for_readiness
201track k8scluster k3s_node_ready_ok
202# update_service_nodeport_range
203# check_for_readiness
204# track k8scluster k3s_update_nodeport_range_ok
garciadeblas1f338482024-07-04 19:26:54 +0200205save_kubeconfig
garciadeblasb3797412024-06-06 14:26:24 +0200206track k8scluster k3s_creds_ok