blob: e117ab44e614f9ee2c2093be4f480bcedeb6848f [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/
garciadeblas72a25d52024-09-19 12:38:36 +020019K8S_CLIENT_VERSION="v1.29.3"
garciadeblasb3797412024-06-06 14:26:24 +020020K8S_VERSION="v1.29.3+k3s1"
21
garciadeblas117fd4a2024-08-21 18:18:14 +020022# configure registry
23function configure_registry() {
24 if [ -n "${DOCKER_PROXY_URL}" ]; then
25 echo "Configuring docker proxy URL in /etc/rancher/k3s/registries.yaml"
26 cat << EOF | sudo tee /etc/rancher/k3s/registries.yaml > /dev/null
27mirrors:
28 docker.io:
29 endpoint:
30 - "${DOCKER_PROXY_URL}"
31EOF
32 fi
garciadeblas2efb98b2024-08-21 21:19:36 +020033 if [ -n "${DOCKER_REGISTRY_URL}" ]; then
34 echo "Configuring docker private registry in /etc/rancher/k3s/registries.yaml"
35 cat << EOF | sudo tee -a /etc/rancher/k3s/registries.yaml > /dev/null
36configs:
37 ${DOCKER_REGISTRY_URL}:
38 auth:
39 username: ${DOCKER_REGISTRY_USER}
40 password: ${DOCKER_REGISTRY_PASSWORD}
41EOF
42 fi
garciadeblas117fd4a2024-08-21 18:18:14 +020043}
44
garciadeblasb3797412024-06-06 14:26:24 +020045# installs k3s
46function install_k3s() {
47 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
48 export INSTALL_K3S_EXEC="--disable traefik"
49 curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K8S_VERSION} sh -s -
50 sudo chmod 644 /etc/rancher/k3s/k3s.yaml
51 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
52}
53
54# updates service nodeport range
55function update_service_nodeport_range() {
56 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
57 sudo k3s server --kube-apiserver-arg=service-node-port-range=80-32767
58 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
59}
60
61# checks cluster readiness
62function check_for_readiness() {
63 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
64 # Check for Ready node, takes ~30 seconds
65 echo "Waiting for K8s nodes to be ready"
66 local time_for_failure=60 # seconds broken
67 local sampling_period=5 # seconds
68 local counter=0
69 local cluster_ready=""
70 while (( counter < time_for_failure ))
71 do
72 kubectl get nodes |grep master |grep -v none | grep Ready
73 if [ $? -eq 0 ] ; then
74 echo "K8s cluster is ready"
75 cluster_ready="y"
76 break
77 else
78 echo "K8s cluster is not ready yet"
79 counter=$((counter + sampling_period))
80 sleep ${sampling_period}
81 fi
82 done
83 [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster "K3s cluster nodes not ready after $time_for_failure seconds."
84
85 echo "Waiting for pods to be ready"
86 local time_for_readiness=20 # seconds ready
87 local time_for_failure=100 # seconds broken
88
89 # Equivalent number of samples
90 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
91 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
92 failures_in_a_row=0
93 oks_in_a_row=0
94 ####################################################################################
95 # Loop to check system readiness
96 ####################################################################################
97 K3S_NAMESPACE=kube-system
98 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
99 do
100 # State of pods rather than completed jobs
101 K3S_PODS_STATE=$(kubectl get pod -n ${K3S_NAMESPACE} --no-headers |grep -v Completed 2>&1)
102 K3S_PODS_READY=$(echo "${K3S_PODS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
103 K3S_PODS_NOT_READY=$(echo "${K3S_PODS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
104 COUNT_K3S_PODS_READY=$(echo "${K3S_PODS_READY}"| grep -v -e '^$' | wc -l)
105 COUNT_K3S_PODS_NOT_READY=$(echo "${K3S_PODS_NOT_READY}" | grep -v -e '^$' | wc -l)
106
107 # OK sample
108 if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]]
109 then
110 ((++oks_in_a_row))
111 failures_in_a_row=0
112 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
113 # NOK sample
114 else
115 ((++failures_in_a_row))
116 oks_in_a_row=0
117 echo
118 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
119
120 # Reports failed pods in K3S
121 if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]]
122 then
123 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:"
124 echo "${K3S_PODS_NOT_READY}"
125 echo
126 fi
127 fi
128
129 #------------ NEXT SAMPLE
130 sleep ${sampling_period}
131 done
132
133 ####################################################################################
134 # OUTCOME
135 ####################################################################################
136 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
137 then
138 echo
139 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
140 else
141 echo
142 echo "K8S CLUSTER IS READY"
143 fi
144 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
145}
146
garciadeblas72a25d52024-09-19 12:38:36 +0200147# Install kubectl client
148function install_kubectl() {
149 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
150 curl -LO "https://dl.k8s.io/release/${K8S_CLIENT_VERSION}/bin/linux/amd64/kubectl"
151 sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
152 rm kubectl
153 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
154}
155
garciadeblas1f338482024-07-04 19:26:54 +0200156# Initializes kubeconfig file
157function save_kubeconfig() {
garciadeblasb3797412024-06-06 14:26:24 +0200158 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
159 KUBEDIR="${HOME}/.kube"
160 KUBEFILE="$KUBEDIR/config"
161 mkdir -p "${KUBEDIR}"
garciadeblas1f338482024-07-04 19:26:54 +0200162 K3S_KUBECONFIG="/etc/rancher/k3s/k3s.yaml"
163 sudo cp "${K3S_KUBECONFIG}" "${KUBEFILE}"
garciadeblasb3797412024-06-06 14:26:24 +0200164 sudo chown $(id -u):$(id -g) "${KUBEFILE}"
garciadeblas1f338482024-07-04 19:26:54 +0200165 sed -i "s#server: https://127.0.0.1#server: https://${DEFAULT_IP}#g" "${KUBEFILE}"
garciadeblasb3797412024-06-06 14:26:24 +0200166 chmod 700 "${KUBEFILE}"
167 echo
168 echo "Credentials saved at ${KUBEFILE}"
169 echo
170 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
171}
172
173# main
garciadeblas2efb98b2024-08-21 21:19:36 +0200174while getopts ":D:i:p:d:u:P:-: " o; do
garciadeblasb3797412024-06-06 14:26:24 +0200175 case "${o}" in
garciadeblas1f338482024-07-04 19:26:54 +0200176 i)
177 DEFAULT_IP="${OPTARG}"
178 ;;
garciadeblasb3797412024-06-06 14:26:24 +0200179 D)
180 OSM_DEVOPS="${OPTARG}"
181 ;;
garciadeblas117fd4a2024-08-21 18:18:14 +0200182 p)
183 DOCKER_PROXY_URL="${OPTARG}"
184 ;;
garciadeblas2efb98b2024-08-21 21:19:36 +0200185 d)
186 DOCKER_REGISTRY_URL="${OPTARG}"
187 ;;
188 u)
189 DOCKER_REGISTRY_USER="${OPTARG}"
190 ;;
191 P)
192 DOCKER_REGISTRY_PASSWORD="${OPTARG}"
193 ;;
garciadeblasb3797412024-06-06 14:26:24 +0200194 -)
195 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
196 echo -e "Invalid option: '--$OPTARG'\n" >&2
197 exit 1
198 ;;
199 :)
200 echo "Option -$OPTARG requires an argument" >&2
201 exit 1
202 ;;
203 \?)
204 echo -e "Invalid option: '-$OPTARG'\n" >&2
205 exit 1
206 ;;
207 *)
208 exit 1
209 ;;
210 esac
211done
212
garciadeblas82981162024-07-23 15:24:00 +0200213DEBUG_INSTALL=${DEBUG_INSTALL:-}
214DEFAULT_IP=${DEFAULT_IP:-"127.0.0.1"}
215OSM_DEVOPS=${OSM_DEVOPS:-"/usr/share/osm-devops"}
garciadeblas117fd4a2024-08-21 18:18:14 +0200216DOCKER_PROXY_URL=${DOCKER_PROXY_URL=-}
garciadeblas2efb98b2024-08-21 21:19:36 +0200217DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL=-}
218DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER=-}
219DOCKER_REGISTRY_PASSWORD=${DOCKER_REGISTRY_PASSWORD=-}
garciadeblas1f338482024-07-04 19:26:54 +0200220echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
221echo "DEFAULT_IP=${DEFAULT_IP}"
222echo "OSM_DEVOPS=${OSM_DEVOPS}"
garciadeblas117fd4a2024-08-21 18:18:14 +0200223echo "DOCKER_PROXY_URL=${DOCKER_PROXY_URL}"
garciadeblas2efb98b2024-08-21 21:19:36 +0200224echo "DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL}"
225echo "DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER}"
garciadeblasb3797412024-06-06 14:26:24 +0200226echo "HOME=$HOME"
227
garciadeblas82981162024-07-23 15:24:00 +0200228source $OSM_DEVOPS/common/logging
229source $OSM_DEVOPS/common/track
230
garciadeblas117fd4a2024-08-21 18:18:14 +0200231configure_registry
garciadeblasb3797412024-06-06 14:26:24 +0200232install_k3s
233track k8scluster k3s_install_ok
234check_for_readiness
235track k8scluster k3s_node_ready_ok
236# update_service_nodeport_range
237# check_for_readiness
238# track k8scluster k3s_update_nodeport_range_ok
garciadeblas72a25d52024-09-19 12:38:36 +0200239install_kubectl
garciadeblas1f338482024-07-04 19:26:54 +0200240save_kubeconfig
garciadeblasb3797412024-06-06 14:26:24 +0200241track k8scluster k3s_creds_ok