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_CLIENT_VERSION
="v1.29.3"
20 K8S_VERSION
="v1.29.3+k3s1"
23 function 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
30 - "${DOCKER_PROXY_URL}"
33 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
37 ${DOCKER_REGISTRY_URL}:
39 username: ${DOCKER_REGISTRY_USER}
40 password: ${DOCKER_REGISTRY_PASSWORD}
46 function 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
54 # updates service nodeport range
55 function 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
61 # checks cluster readiness
62 function 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
69 local cluster_ready
=""
70 while (( counter
< time_for_failure
))
72 kubectl get nodes |
grep master |
grep -v none |
grep Ready
73 if [ $?
-eq 0 ] ; then
74 echo "K8s cluster is ready"
78 echo "K8s cluster is not ready yet"
79 counter
=$
((counter
+ sampling_period
))
80 sleep ${sampling_period}
83 [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster
"K3s cluster nodes not ready after $time_for_failure seconds."
85 echo "Waiting for pods to be ready"
86 local time_for_readiness
=20 # seconds ready
87 local time_for_failure
=100 # seconds broken
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
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}) ]]
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)
108 if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]]
112 echo -ne ===\
> Successful checks
: "${oks_in_a_row}"/${oks_threshold}\\r
115 ((++failures_in_a_row
))
118 echo Bootstraping...
"${failures_in_a_row}" checks of
${failures_threshold}
120 # Reports failed pods in K3S
121 if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]]
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}"
129 #------------ NEXT SAMPLE
130 sleep ${sampling_period}
133 ####################################################################################
135 ####################################################################################
136 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
139 FATAL_TRACK k8scluster
"K8S CLUSTER IS BROKEN"
142 echo "K8S CLUSTER IS READY"
144 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
147 # Install kubectl client
148 function 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
153 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
156 # Initializes kubeconfig file
157 function save_kubeconfig
() {
158 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
159 KUBEDIR
="${HOME}/.kube"
160 KUBEFILE
="$KUBEDIR/config"
161 mkdir
-p "${KUBEDIR}"
162 K3S_KUBECONFIG
="/etc/rancher/k3s/k3s.yaml"
163 sudo
cp "${K3S_KUBECONFIG}" "${KUBEFILE}"
164 sudo chown $
(id
-u):$
(id
-g) "${KUBEFILE}"
165 sed -i "s#server: https://127.0.0.1#server: https://${DEFAULT_IP}#g" "${KUBEFILE}"
166 chmod 700 "${KUBEFILE}"
168 echo "Credentials saved at ${KUBEFILE}"
170 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
174 while getopts ":D:i:p:d:u:P:-: " o
; do
177 DEFAULT_IP
="${OPTARG}"
180 OSM_DEVOPS
="${OPTARG}"
183 DOCKER_PROXY_URL
="${OPTARG}"
186 DOCKER_REGISTRY_URL
="${OPTARG}"
189 DOCKER_REGISTRY_USER
="${OPTARG}"
192 DOCKER_REGISTRY_PASSWORD
="${OPTARG}"
195 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL
="y" && continue
196 echo -e "Invalid option: '--$OPTARG'\n" >&2
200 echo "Option -$OPTARG requires an argument" >&2
204 echo -e "Invalid option: '-$OPTARG'\n" >&2
213 DEBUG_INSTALL
=${DEBUG_INSTALL:-}
214 DEFAULT_IP
=${DEFAULT_IP:-"127.0.0.1"}
215 OSM_DEVOPS
=${OSM_DEVOPS:-"/usr/share/osm-devops"}
216 DOCKER_PROXY_URL
=${DOCKER_PROXY_URL=-}
217 DOCKER_REGISTRY_URL
=${DOCKER_REGISTRY_URL=-}
218 DOCKER_REGISTRY_USER
=${DOCKER_REGISTRY_USER=-}
219 DOCKER_REGISTRY_PASSWORD
=${DOCKER_REGISTRY_PASSWORD=-}
220 echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
221 echo "DEFAULT_IP=${DEFAULT_IP}"
222 echo "OSM_DEVOPS=${OSM_DEVOPS}"
223 echo "DOCKER_PROXY_URL=${DOCKER_PROXY_URL}"
224 echo "DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL}"
225 echo "DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER}"
228 source $OSM_DEVOPS/common
/logging
229 source $OSM_DEVOPS/common
/track
233 track k8scluster k3s_install_ok
235 track k8scluster k3s_node_ready_ok
236 # update_service_nodeport_range
237 # check_for_readiness
238 # track k8scluster k3s_update_nodeport_range_ok
241 track k8scluster k3s_creds_ok