| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 1 | #!/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 | |
| 16 | set +eux |
| 17 | |
| 18 | # K3s releases: https://github.com/k3s-io/k3s/releases/ |
| 19 | K8S_VERSION="v1.29.3+k3s1" |
| 20 | |
| 21 | # installs k3s |
| 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 |
| 28 | } |
| 29 | |
| 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 |
| 35 | } |
| 36 | |
| 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 |
| 44 | local counter=0 |
| 45 | local cluster_ready="" |
| 46 | while (( counter < time_for_failure )) |
| 47 | do |
| 48 | kubectl get nodes |grep master |grep -v none | grep Ready |
| 49 | if [ $? -eq 0 ] ; then |
| 50 | echo "K8s cluster is ready" |
| 51 | cluster_ready="y" |
| 52 | break |
| 53 | else |
| 54 | echo "K8s cluster is not ready yet" |
| 55 | counter=$((counter + sampling_period)) |
| 56 | sleep ${sampling_period} |
| 57 | fi |
| 58 | done |
| 59 | [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster "K3s cluster nodes not ready after $time_for_failure seconds." |
| 60 | |
| 61 | echo "Waiting for pods to be ready" |
| 62 | local time_for_readiness=20 # seconds ready |
| 63 | local time_for_failure=100 # seconds broken |
| 64 | |
| 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 |
| 68 | failures_in_a_row=0 |
| 69 | oks_in_a_row=0 |
| 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}) ]] |
| 75 | do |
| 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) |
| 82 | |
| 83 | # OK sample |
| 84 | if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]] |
| 85 | then |
| 86 | ((++oks_in_a_row)) |
| 87 | failures_in_a_row=0 |
| 88 | echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r |
| 89 | # NOK sample |
| 90 | else |
| 91 | ((++failures_in_a_row)) |
| 92 | oks_in_a_row=0 |
| 93 | echo |
| 94 | echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold} |
| 95 | |
| 96 | # Reports failed pods in K3S |
| 97 | if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]] |
| 98 | then |
| 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}" |
| 101 | echo |
| 102 | fi |
| 103 | fi |
| 104 | |
| 105 | #------------ NEXT SAMPLE |
| 106 | sleep ${sampling_period} |
| 107 | done |
| 108 | |
| 109 | #################################################################################### |
| 110 | # OUTCOME |
| 111 | #################################################################################### |
| 112 | if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]] |
| 113 | then |
| 114 | echo |
| 115 | FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN" |
| 116 | else |
| 117 | echo |
| 118 | echo "K8S CLUSTER IS READY" |
| 119 | fi |
| 120 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 121 | } |
| 122 | |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 123 | # Initializes kubeconfig file |
| 124 | function save_kubeconfig() { |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 125 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 126 | KUBEDIR="${HOME}/.kube" |
| 127 | KUBEFILE="$KUBEDIR/config" |
| 128 | mkdir -p "${KUBEDIR}" |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 129 | K3S_KUBECONFIG="/etc/rancher/k3s/k3s.yaml" |
| 130 | sudo cp "${K3S_KUBECONFIG}" "${KUBEFILE}" |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 131 | sudo chown $(id -u):$(id -g) "${KUBEFILE}" |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 132 | sed -i "s#server: https://127.0.0.1#server: https://${DEFAULT_IP}#g" "${KUBEFILE}" |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 133 | chmod 700 "${KUBEFILE}" |
| 134 | echo |
| 135 | echo "Credentials saved at ${KUBEFILE}" |
| 136 | echo |
| 137 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 138 | } |
| 139 | |
| 140 | # main |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 141 | DEFAULT_IP="127.0.0.1" |
| 142 | OSM_DEVOPS="/usr/share/osm-devops" |
| 143 | DEBUG_INSTALL="" |
| 144 | |
| 145 | while getopts ":D:i:-: " o; do |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 146 | case "${o}" in |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 147 | i) |
| 148 | DEFAULT_IP="${OPTARG}" |
| 149 | ;; |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 150 | D) |
| 151 | OSM_DEVOPS="${OPTARG}" |
| 152 | ;; |
| 153 | -) |
| 154 | [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue |
| 155 | echo -e "Invalid option: '--$OPTARG'\n" >&2 |
| 156 | exit 1 |
| 157 | ;; |
| 158 | :) |
| 159 | echo "Option -$OPTARG requires an argument" >&2 |
| 160 | exit 1 |
| 161 | ;; |
| 162 | \?) |
| 163 | echo -e "Invalid option: '-$OPTARG'\n" >&2 |
| 164 | exit 1 |
| 165 | ;; |
| 166 | *) |
| 167 | exit 1 |
| 168 | ;; |
| 169 | esac |
| 170 | done |
| 171 | |
| 172 | source $OSM_DEVOPS/common/logging |
| 173 | source $OSM_DEVOPS/common/track |
| 174 | |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 175 | echo "DEBUG_INSTALL=${DEBUG_INSTALL}" |
| 176 | echo "DEFAULT_IP=${DEFAULT_IP}" |
| 177 | echo "OSM_DEVOPS=${OSM_DEVOPS}" |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 178 | echo "HOME=$HOME" |
| 179 | |
| 180 | install_k3s |
| 181 | track k8scluster k3s_install_ok |
| 182 | check_for_readiness |
| 183 | track k8scluster k3s_node_ready_ok |
| 184 | # update_service_nodeport_range |
| 185 | # check_for_readiness |
| 186 | # track k8scluster k3s_update_nodeport_range_ok |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 187 | save_kubeconfig |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 188 | track k8scluster k3s_creds_ok |