blob: 86bc1353bececc915dd7c08157bb5b91519ae1d9 [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
garciadeblas2efb98b2024-08-21 21:19:36 +020032 if [ -n "${DOCKER_REGISTRY_URL}" ]; then
33 echo "Configuring docker private registry in /etc/rancher/k3s/registries.yaml"
34 cat << EOF | sudo tee -a /etc/rancher/k3s/registries.yaml > /dev/null
35configs:
36 ${DOCKER_REGISTRY_URL}:
37 auth:
38 username: ${DOCKER_REGISTRY_USER}
39 password: ${DOCKER_REGISTRY_PASSWORD}
40EOF
41 fi
garciadeblas117fd4a2024-08-21 18:18:14 +020042}
43
garciadeblasb3797412024-06-06 14:26:24 +020044# installs k3s
45function install_k3s() {
46 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
47 export INSTALL_K3S_EXEC="--disable traefik"
48 curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K8S_VERSION} sh -s -
49 sudo chmod 644 /etc/rancher/k3s/k3s.yaml
50 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
51}
52
53# updates service nodeport range
54function update_service_nodeport_range() {
55 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
56 sudo k3s server --kube-apiserver-arg=service-node-port-range=80-32767
57 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
58}
59
60# checks cluster readiness
61function check_for_readiness() {
62 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
63 # Check for Ready node, takes ~30 seconds
64 echo "Waiting for K8s nodes to be ready"
65 local time_for_failure=60 # seconds broken
66 local sampling_period=5 # seconds
67 local counter=0
68 local cluster_ready=""
69 while (( counter < time_for_failure ))
70 do
71 kubectl get nodes |grep master |grep -v none | grep Ready
72 if [ $? -eq 0 ] ; then
73 echo "K8s cluster is ready"
74 cluster_ready="y"
75 break
76 else
77 echo "K8s cluster is not ready yet"
78 counter=$((counter + sampling_period))
79 sleep ${sampling_period}
80 fi
81 done
82 [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster "K3s cluster nodes not ready after $time_for_failure seconds."
83
84 echo "Waiting for pods to be ready"
85 local time_for_readiness=20 # seconds ready
86 local time_for_failure=100 # seconds broken
87
88 # Equivalent number of samples
89 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
90 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
91 failures_in_a_row=0
92 oks_in_a_row=0
93 ####################################################################################
94 # Loop to check system readiness
95 ####################################################################################
96 K3S_NAMESPACE=kube-system
97 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
98 do
99 # State of pods rather than completed jobs
100 K3S_PODS_STATE=$(kubectl get pod -n ${K3S_NAMESPACE} --no-headers |grep -v Completed 2>&1)
101 K3S_PODS_READY=$(echo "${K3S_PODS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
102 K3S_PODS_NOT_READY=$(echo "${K3S_PODS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
103 COUNT_K3S_PODS_READY=$(echo "${K3S_PODS_READY}"| grep -v -e '^$' | wc -l)
104 COUNT_K3S_PODS_NOT_READY=$(echo "${K3S_PODS_NOT_READY}" | grep -v -e '^$' | wc -l)
105
106 # OK sample
107 if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]]
108 then
109 ((++oks_in_a_row))
110 failures_in_a_row=0
111 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
112 # NOK sample
113 else
114 ((++failures_in_a_row))
115 oks_in_a_row=0
116 echo
117 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
118
119 # Reports failed pods in K3S
120 if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]]
121 then
122 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:"
123 echo "${K3S_PODS_NOT_READY}"
124 echo
125 fi
126 fi
127
128 #------------ NEXT SAMPLE
129 sleep ${sampling_period}
130 done
131
132 ####################################################################################
133 # OUTCOME
134 ####################################################################################
135 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
136 then
137 echo
138 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
139 else
140 echo
141 echo "K8S CLUSTER IS READY"
142 fi
143 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
144}
145
garciadeblas1f338482024-07-04 19:26:54 +0200146# Initializes kubeconfig file
147function save_kubeconfig() {
garciadeblasb3797412024-06-06 14:26:24 +0200148 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
149 KUBEDIR="${HOME}/.kube"
150 KUBEFILE="$KUBEDIR/config"
151 mkdir -p "${KUBEDIR}"
garciadeblas1f338482024-07-04 19:26:54 +0200152 K3S_KUBECONFIG="/etc/rancher/k3s/k3s.yaml"
153 sudo cp "${K3S_KUBECONFIG}" "${KUBEFILE}"
garciadeblasb3797412024-06-06 14:26:24 +0200154 sudo chown $(id -u):$(id -g) "${KUBEFILE}"
garciadeblas1f338482024-07-04 19:26:54 +0200155 sed -i "s#server: https://127.0.0.1#server: https://${DEFAULT_IP}#g" "${KUBEFILE}"
garciadeblasb3797412024-06-06 14:26:24 +0200156 chmod 700 "${KUBEFILE}"
157 echo
158 echo "Credentials saved at ${KUBEFILE}"
159 echo
160 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
161}
162
163# main
garciadeblas2efb98b2024-08-21 21:19:36 +0200164while getopts ":D:i:p:d:u:P:-: " o; do
garciadeblasb3797412024-06-06 14:26:24 +0200165 case "${o}" in
garciadeblas1f338482024-07-04 19:26:54 +0200166 i)
167 DEFAULT_IP="${OPTARG}"
168 ;;
garciadeblasb3797412024-06-06 14:26:24 +0200169 D)
170 OSM_DEVOPS="${OPTARG}"
171 ;;
garciadeblas117fd4a2024-08-21 18:18:14 +0200172 p)
173 DOCKER_PROXY_URL="${OPTARG}"
174 ;;
garciadeblas2efb98b2024-08-21 21:19:36 +0200175 d)
176 DOCKER_REGISTRY_URL="${OPTARG}"
177 ;;
178 u)
179 DOCKER_REGISTRY_USER="${OPTARG}"
180 ;;
181 P)
182 DOCKER_REGISTRY_PASSWORD="${OPTARG}"
183 ;;
garciadeblasb3797412024-06-06 14:26:24 +0200184 -)
185 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
186 echo -e "Invalid option: '--$OPTARG'\n" >&2
187 exit 1
188 ;;
189 :)
190 echo "Option -$OPTARG requires an argument" >&2
191 exit 1
192 ;;
193 \?)
194 echo -e "Invalid option: '-$OPTARG'\n" >&2
195 exit 1
196 ;;
197 *)
198 exit 1
199 ;;
200 esac
201done
202
garciadeblas82981162024-07-23 15:24:00 +0200203DEBUG_INSTALL=${DEBUG_INSTALL:-}
204DEFAULT_IP=${DEFAULT_IP:-"127.0.0.1"}
205OSM_DEVOPS=${OSM_DEVOPS:-"/usr/share/osm-devops"}
garciadeblas117fd4a2024-08-21 18:18:14 +0200206DOCKER_PROXY_URL=${DOCKER_PROXY_URL=-}
garciadeblas2efb98b2024-08-21 21:19:36 +0200207DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL=-}
208DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER=-}
209DOCKER_REGISTRY_PASSWORD=${DOCKER_REGISTRY_PASSWORD=-}
garciadeblas1f338482024-07-04 19:26:54 +0200210echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
211echo "DEFAULT_IP=${DEFAULT_IP}"
212echo "OSM_DEVOPS=${OSM_DEVOPS}"
garciadeblas117fd4a2024-08-21 18:18:14 +0200213echo "DOCKER_PROXY_URL=${DOCKER_PROXY_URL}"
garciadeblas2efb98b2024-08-21 21:19:36 +0200214echo "DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL}"
215echo "DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER}"
garciadeblasb3797412024-06-06 14:26:24 +0200216echo "HOME=$HOME"
217
garciadeblas82981162024-07-23 15:24:00 +0200218source $OSM_DEVOPS/common/logging
219source $OSM_DEVOPS/common/track
220
garciadeblas117fd4a2024-08-21 18:18:14 +0200221configure_registry
garciadeblasb3797412024-06-06 14:26:24 +0200222install_k3s
223track k8scluster k3s_install_ok
224check_for_readiness
225track k8scluster k3s_node_ready_ok
226# update_service_nodeport_range
227# check_for_readiness
228# track k8scluster k3s_update_nodeport_range_ok
garciadeblas1f338482024-07-04 19:26:54 +0200229save_kubeconfig
garciadeblasb3797412024-06-06 14:26:24 +0200230track k8scluster k3s_creds_ok