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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # K3s releases: https://github.com/k3s-io/k3s/releases/
19 K8S_VERSION
="v1.29.3+k3s1"
22 function install_k3s
() {
23 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
24 export INSTALL_K3S_EXEC
="--disable traefik"
25 curl
-sfL https
://get.k3s.io | INSTALL_K3S_VERSION
=${K8S_VERSION} sh
-s -
26 sudo
chmod 644 /etc
/rancher
/k3s
/k3s.yaml
27 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
30 # updates service nodeport range
31 function update_service_nodeport_range
() {
32 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
33 sudo k3s server
--kube-apiserver-arg=service-node-port-range
=80-32767
34 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
37 # checks cluster readiness
38 function check_for_readiness
() {
39 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
40 # Check for Ready node, takes ~30 seconds
41 echo "Waiting for K8s nodes to be ready"
42 local time_for_failure
=60 # seconds broken
43 local sampling_period
=5 # seconds
45 local cluster_ready
=""
46 while (( counter
< time_for_failure
))
48 kubectl get nodes |
grep master |
grep -v none |
grep Ready
49 if [ $?
-eq 0 ] ; then
50 echo "K8s cluster is ready"
54 echo "K8s cluster is not ready yet"
55 counter
=$
((counter
+ sampling_period
))
56 sleep ${sampling_period}
59 [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster
"K3s cluster nodes not ready after $time_for_failure seconds."
61 echo "Waiting for pods to be ready"
62 local time_for_readiness
=20 # seconds ready
63 local time_for_failure
=100 # seconds broken
65 # Equivalent number of samples
66 oks_threshold
=$
((time_for_readiness
/${sampling_period})) # No. ok samples to declare the system ready
67 failures_threshold
=$
((time_for_failure
/${sampling_period})) # No. nok samples to declare the system broken
70 ####################################################################################
71 # Loop to check system readiness
72 ####################################################################################
73 K3S_NAMESPACE
=kube-system
74 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
76 # State of pods rather than completed jobs
77 K3S_PODS_STATE
=$
(kubectl get pod
-n ${K3S_NAMESPACE} --no-headers |
grep -v Completed
2>&1)
78 K3S_PODS_READY
=$
(echo "${K3S_PODS_STATE}" |
awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
79 K3S_PODS_NOT_READY
=$
(echo "${K3S_PODS_STATE}" |
awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
80 COUNT_K3S_PODS_READY
=$
(echo "${K3S_PODS_READY}"|
grep -v -e '^$' |
wc -l)
81 COUNT_K3S_PODS_NOT_READY
=$
(echo "${K3S_PODS_NOT_READY}" |
grep -v -e '^$' |
wc -l)
84 if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]]
88 echo -ne ===\
> Successful checks
: "${oks_in_a_row}"/${oks_threshold}\\r
91 ((++failures_in_a_row
))
94 echo Bootstraping...
"${failures_in_a_row}" checks of
${failures_threshold}
96 # Reports failed pods in K3S
97 if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]]
99 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:"
100 echo "${K3S_PODS_NOT_READY}"
105 #------------ NEXT SAMPLE
106 sleep ${sampling_period}
109 ####################################################################################
111 ####################################################################################
112 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
115 FATAL_TRACK k8scluster
"K8S CLUSTER IS BROKEN"
118 echo "K8S CLUSTER IS READY"
120 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
123 # Retrieves and saves the credentials
124 function save_credentials
() {
125 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
126 KUBEDIR
="${HOME}/.kube"
127 KUBEFILE
="$KUBEDIR/config"
128 mkdir
-p "${KUBEDIR}"
129 sudo
cp /etc
/rancher
/k3s
/k3s.yaml
"${KUBEFILE}"
130 sudo chown $
(id
-u):$
(id
-g) "${KUBEFILE}"
131 chmod 700 "${KUBEFILE}"
133 echo "Credentials saved at ${KUBEFILE}"
135 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
139 while getopts ":D:-: " o
; do
142 OSM_DEVOPS
="${OPTARG}"
145 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL
="y" && continue
146 echo -e "Invalid option: '--$OPTARG'\n" >&2
150 echo "Option -$OPTARG requires an argument" >&2
154 echo -e "Invalid option: '-$OPTARG'\n" >&2
163 source $OSM_DEVOPS/common
/logging
164 source $OSM_DEVOPS/common
/track
166 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
167 echo "OSM_DEVOPS=$OSM_DEVOPS"
171 track k8scluster k3s_install_ok
173 track k8scluster k3s_node_ready_ok
174 # update_service_nodeport_range
175 # check_for_readiness
176 # track k8scluster k3s_update_nodeport_range_ok
178 track k8scluster k3s_creds_ok