blob: 68e61a6e6f46eba13edf0a9714c8dd00eff7dc9e [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/
garciadeblas6075ff22024-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"
garciadeblas8692f2a2024-11-18 12:58:29 +010026 sudo mkdir -p /etc/rancher/k3s/
garciadeblas117fd4a2024-08-21 18:18:14 +020027 cat << EOF | sudo tee /etc/rancher/k3s/registries.yaml > /dev/null
28mirrors:
29 docker.io:
30 endpoint:
31 - "${DOCKER_PROXY_URL}"
32EOF
33 fi
garciadeblas2efb98b2024-08-21 21:19:36 +020034 if [ -n "${DOCKER_REGISTRY_URL}" ]; then
35 echo "Configuring docker private registry in /etc/rancher/k3s/registries.yaml"
36 cat << EOF | sudo tee -a /etc/rancher/k3s/registries.yaml > /dev/null
37configs:
38 ${DOCKER_REGISTRY_URL}:
39 auth:
40 username: ${DOCKER_REGISTRY_USER}
41 password: ${DOCKER_REGISTRY_PASSWORD}
42EOF
43 fi
garciadeblas117fd4a2024-08-21 18:18:14 +020044}
45
garciadeblasb3797412024-06-06 14:26:24 +020046# installs k3s
47function install_k3s() {
48 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
49 export INSTALL_K3S_EXEC="--disable traefik"
garciadeblas6a2de6d2024-09-27 11:34:55 +020050
51 # Regular installation of K3s
garciadeblasb3797412024-06-06 14:26:24 +020052 curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K8S_VERSION} sh -s -
garciadeblas6a2de6d2024-09-27 11:34:55 +020053
54 # If specified a public IP, K3s service is updated accordingly and restarted
55 if [ -n "${K3S_PUBLIC_IP}" ]; then
56 # Back-up service config file to home
57 cp /etc/systemd/system/k3s.service ~/BASE-k3s.service
58
59 # Generate new service config file with additions for using a public IP
60 (
61 cat ~/BASE-k3s.service | sed '${/^$/d}'
62 echo -e "\t'--node-external-ip' \\"
63 echo -e "\t'${K3S_PUBLIC_IP}' \\"
64 echo
65 )| \
66 tee ~/PUBLIC-k3s.service
67
68 # Replace service config and apply
69 sudo cp ~/PUBLIC-k3s.service /etc/systemd/system/k3s.service
70 sudo systemctl daemon-reload
71 sudo systemctl restart k3s
72
73 # Cleanup
74 rm ~/BASE-k3s.service ~/PUBLIC-k3s.service
75 fi
76
77 # Make kubeconfig permissions less restrictive
garciadeblasb3797412024-06-06 14:26:24 +020078 sudo chmod 644 /etc/rancher/k3s/k3s.yaml
79 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
80}
81
82# updates service nodeport range
83function update_service_nodeport_range() {
84 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
85 sudo k3s server --kube-apiserver-arg=service-node-port-range=80-32767
86 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
87}
88
89# checks cluster readiness
90function check_for_readiness() {
91 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
92 # Check for Ready node, takes ~30 seconds
93 echo "Waiting for K8s nodes to be ready"
94 local time_for_failure=60 # seconds broken
95 local sampling_period=5 # seconds
96 local counter=0
97 local cluster_ready=""
98 while (( counter < time_for_failure ))
99 do
100 kubectl get nodes |grep master |grep -v none | grep Ready
101 if [ $? -eq 0 ] ; then
102 echo "K8s cluster is ready"
103 cluster_ready="y"
104 break
105 else
106 echo "K8s cluster is not ready yet"
107 counter=$((counter + sampling_period))
108 sleep ${sampling_period}
109 fi
110 done
111 [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster "K3s cluster nodes not ready after $time_for_failure seconds."
112
113 echo "Waiting for pods to be ready"
114 local time_for_readiness=20 # seconds ready
115 local time_for_failure=100 # seconds broken
116
117 # Equivalent number of samples
118 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
119 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
120 failures_in_a_row=0
121 oks_in_a_row=0
122 ####################################################################################
123 # Loop to check system readiness
124 ####################################################################################
125 K3S_NAMESPACE=kube-system
126 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
127 do
128 # State of pods rather than completed jobs
129 K3S_PODS_STATE=$(kubectl get pod -n ${K3S_NAMESPACE} --no-headers |grep -v Completed 2>&1)
130 K3S_PODS_READY=$(echo "${K3S_PODS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
131 K3S_PODS_NOT_READY=$(echo "${K3S_PODS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
132 COUNT_K3S_PODS_READY=$(echo "${K3S_PODS_READY}"| grep -v -e '^$' | wc -l)
133 COUNT_K3S_PODS_NOT_READY=$(echo "${K3S_PODS_NOT_READY}" | grep -v -e '^$' | wc -l)
134
135 # OK sample
136 if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]]
137 then
138 ((++oks_in_a_row))
139 failures_in_a_row=0
140 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
141 # NOK sample
142 else
143 ((++failures_in_a_row))
144 oks_in_a_row=0
145 echo
146 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
147
148 # Reports failed pods in K3S
149 if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]]
150 then
151 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:"
152 echo "${K3S_PODS_NOT_READY}"
153 echo
154 fi
155 fi
156
157 #------------ NEXT SAMPLE
158 sleep ${sampling_period}
159 done
160
161 ####################################################################################
162 # OUTCOME
163 ####################################################################################
164 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
165 then
166 echo
167 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
168 else
169 echo
170 echo "K8S CLUSTER IS READY"
171 fi
172 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
173}
174
garciadeblas6075ff22024-09-19 12:38:36 +0200175# Install kubectl client
176function install_kubectl() {
177 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
178 curl -LO "https://dl.k8s.io/release/${K8S_CLIENT_VERSION}/bin/linux/amd64/kubectl"
179 sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
180 rm kubectl
181 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
182}
183
garciadeblas1f338482024-07-04 19:26:54 +0200184# Initializes kubeconfig file
185function save_kubeconfig() {
garciadeblasb3797412024-06-06 14:26:24 +0200186 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
187 KUBEDIR="${HOME}/.kube"
188 KUBEFILE="$KUBEDIR/config"
189 mkdir -p "${KUBEDIR}"
garciadeblas1f338482024-07-04 19:26:54 +0200190 K3S_KUBECONFIG="/etc/rancher/k3s/k3s.yaml"
191 sudo cp "${K3S_KUBECONFIG}" "${KUBEFILE}"
garciadeblasb3797412024-06-06 14:26:24 +0200192 sudo chown $(id -u):$(id -g) "${KUBEFILE}"
garciadeblas1f338482024-07-04 19:26:54 +0200193 sed -i "s#server: https://127.0.0.1#server: https://${DEFAULT_IP}#g" "${KUBEFILE}"
garciadeblasb3797412024-06-06 14:26:24 +0200194 chmod 700 "${KUBEFILE}"
195 echo
196 echo "Credentials saved at ${KUBEFILE}"
197 echo
198 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
199}
200
201# main
garciadeblas6a2de6d2024-09-27 11:34:55 +0200202while getopts ":D:i:e:p:d:u:P:-: " o; do
garciadeblasb3797412024-06-06 14:26:24 +0200203 case "${o}" in
garciadeblas1f338482024-07-04 19:26:54 +0200204 i)
205 DEFAULT_IP="${OPTARG}"
206 ;;
garciadeblas6a2de6d2024-09-27 11:34:55 +0200207 e)
208 K3S_PUBLIC_IP="${OPTARG}"
209 ;;
garciadeblasb3797412024-06-06 14:26:24 +0200210 D)
211 OSM_DEVOPS="${OPTARG}"
212 ;;
garciadeblas117fd4a2024-08-21 18:18:14 +0200213 p)
214 DOCKER_PROXY_URL="${OPTARG}"
215 ;;
garciadeblas2efb98b2024-08-21 21:19:36 +0200216 d)
217 DOCKER_REGISTRY_URL="${OPTARG}"
218 ;;
219 u)
220 DOCKER_REGISTRY_USER="${OPTARG}"
221 ;;
222 P)
223 DOCKER_REGISTRY_PASSWORD="${OPTARG}"
224 ;;
garciadeblasb3797412024-06-06 14:26:24 +0200225 -)
226 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
227 echo -e "Invalid option: '--$OPTARG'\n" >&2
228 exit 1
229 ;;
230 :)
231 echo "Option -$OPTARG requires an argument" >&2
232 exit 1
233 ;;
234 \?)
235 echo -e "Invalid option: '-$OPTARG'\n" >&2
236 exit 1
237 ;;
238 *)
239 exit 1
240 ;;
241 esac
242done
243
garciadeblas82981162024-07-23 15:24:00 +0200244DEBUG_INSTALL=${DEBUG_INSTALL:-}
245DEFAULT_IP=${DEFAULT_IP:-"127.0.0.1"}
246OSM_DEVOPS=${OSM_DEVOPS:-"/usr/share/osm-devops"}
garciadeblasae02bac2024-11-20 12:55:45 +0100247DOCKER_PROXY_URL=${DOCKER_PROXY_URL:-}
248DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL:-}
249DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER:-}
250DOCKER_REGISTRY_PASSWORD=${DOCKER_REGISTRY_PASSWORD:-}
garciadeblas6a2de6d2024-09-27 11:34:55 +0200251K3S_PUBLIC_IP=${K3S_PUBLIC_IP:-}
garciadeblas1f338482024-07-04 19:26:54 +0200252echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
253echo "DEFAULT_IP=${DEFAULT_IP}"
254echo "OSM_DEVOPS=${OSM_DEVOPS}"
garciadeblas117fd4a2024-08-21 18:18:14 +0200255echo "DOCKER_PROXY_URL=${DOCKER_PROXY_URL}"
garciadeblas2efb98b2024-08-21 21:19:36 +0200256echo "DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL}"
257echo "DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER}"
garciadeblas6a2de6d2024-09-27 11:34:55 +0200258echo "K3S_PUBLIC_IP=${K3S_PUBLIC_IP}"
garciadeblasb3797412024-06-06 14:26:24 +0200259echo "HOME=$HOME"
260
garciadeblas82981162024-07-23 15:24:00 +0200261source $OSM_DEVOPS/common/logging
262source $OSM_DEVOPS/common/track
263
garciadeblas117fd4a2024-08-21 18:18:14 +0200264configure_registry
garciadeblasb3797412024-06-06 14:26:24 +0200265install_k3s
266track k8scluster k3s_install_ok
267check_for_readiness
268track k8scluster k3s_node_ready_ok
269# update_service_nodeport_range
270# check_for_readiness
271# track k8scluster k3s_update_nodeport_range_ok
garciadeblas6075ff22024-09-19 12:38:36 +0200272install_kubectl
garciadeblas1f338482024-07-04 19:26:54 +0200273save_kubeconfig
garciadeblasb3797412024-06-06 14:26:24 +0200274track k8scluster k3s_creds_ok