| 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 | |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame^] | 21 | # configure registry |
| 22 | function configure_registry() { |
| 23 | if [ -n "${DOCKER_PROXY_URL}" ]; then |
| 24 | echo "Configuring docker proxy URL in /etc/rancher/k3s/registries.yaml" |
| 25 | cat << EOF | sudo tee /etc/rancher/k3s/registries.yaml > /dev/null |
| 26 | mirrors: |
| 27 | docker.io: |
| 28 | endpoint: |
| 29 | - "${DOCKER_PROXY_URL}" |
| 30 | EOF |
| 31 | fi |
| 32 | } |
| 33 | |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 34 | # installs k3s |
| 35 | function install_k3s() { |
| 36 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 37 | export INSTALL_K3S_EXEC="--disable traefik" |
| 38 | curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K8S_VERSION} sh -s - |
| 39 | sudo chmod 644 /etc/rancher/k3s/k3s.yaml |
| 40 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 41 | } |
| 42 | |
| 43 | # updates service nodeport range |
| 44 | function update_service_nodeport_range() { |
| 45 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 46 | sudo k3s server --kube-apiserver-arg=service-node-port-range=80-32767 |
| 47 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 48 | } |
| 49 | |
| 50 | # checks cluster readiness |
| 51 | function check_for_readiness() { |
| 52 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 53 | # Check for Ready node, takes ~30 seconds |
| 54 | echo "Waiting for K8s nodes to be ready" |
| 55 | local time_for_failure=60 # seconds broken |
| 56 | local sampling_period=5 # seconds |
| 57 | local counter=0 |
| 58 | local cluster_ready="" |
| 59 | while (( counter < time_for_failure )) |
| 60 | do |
| 61 | kubectl get nodes |grep master |grep -v none | grep Ready |
| 62 | if [ $? -eq 0 ] ; then |
| 63 | echo "K8s cluster is ready" |
| 64 | cluster_ready="y" |
| 65 | break |
| 66 | else |
| 67 | echo "K8s cluster is not ready yet" |
| 68 | counter=$((counter + sampling_period)) |
| 69 | sleep ${sampling_period} |
| 70 | fi |
| 71 | done |
| 72 | [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster "K3s cluster nodes not ready after $time_for_failure seconds." |
| 73 | |
| 74 | echo "Waiting for pods to be ready" |
| 75 | local time_for_readiness=20 # seconds ready |
| 76 | local time_for_failure=100 # seconds broken |
| 77 | |
| 78 | # Equivalent number of samples |
| 79 | oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready |
| 80 | failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken |
| 81 | failures_in_a_row=0 |
| 82 | oks_in_a_row=0 |
| 83 | #################################################################################### |
| 84 | # Loop to check system readiness |
| 85 | #################################################################################### |
| 86 | K3S_NAMESPACE=kube-system |
| 87 | while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]] |
| 88 | do |
| 89 | # State of pods rather than completed jobs |
| 90 | K3S_PODS_STATE=$(kubectl get pod -n ${K3S_NAMESPACE} --no-headers |grep -v Completed 2>&1) |
| 91 | K3S_PODS_READY=$(echo "${K3S_PODS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}') |
| 92 | K3S_PODS_NOT_READY=$(echo "${K3S_PODS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}') |
| 93 | COUNT_K3S_PODS_READY=$(echo "${K3S_PODS_READY}"| grep -v -e '^$' | wc -l) |
| 94 | COUNT_K3S_PODS_NOT_READY=$(echo "${K3S_PODS_NOT_READY}" | grep -v -e '^$' | wc -l) |
| 95 | |
| 96 | # OK sample |
| 97 | if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]] |
| 98 | then |
| 99 | ((++oks_in_a_row)) |
| 100 | failures_in_a_row=0 |
| 101 | echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r |
| 102 | # NOK sample |
| 103 | else |
| 104 | ((++failures_in_a_row)) |
| 105 | oks_in_a_row=0 |
| 106 | echo |
| 107 | echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold} |
| 108 | |
| 109 | # Reports failed pods in K3S |
| 110 | if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]] |
| 111 | then |
| 112 | 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:" |
| 113 | echo "${K3S_PODS_NOT_READY}" |
| 114 | echo |
| 115 | fi |
| 116 | fi |
| 117 | |
| 118 | #------------ NEXT SAMPLE |
| 119 | sleep ${sampling_period} |
| 120 | done |
| 121 | |
| 122 | #################################################################################### |
| 123 | # OUTCOME |
| 124 | #################################################################################### |
| 125 | if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]] |
| 126 | then |
| 127 | echo |
| 128 | FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN" |
| 129 | else |
| 130 | echo |
| 131 | echo "K8S CLUSTER IS READY" |
| 132 | fi |
| 133 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 134 | } |
| 135 | |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 136 | # Initializes kubeconfig file |
| 137 | function save_kubeconfig() { |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 138 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 139 | KUBEDIR="${HOME}/.kube" |
| 140 | KUBEFILE="$KUBEDIR/config" |
| 141 | mkdir -p "${KUBEDIR}" |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 142 | K3S_KUBECONFIG="/etc/rancher/k3s/k3s.yaml" |
| 143 | sudo cp "${K3S_KUBECONFIG}" "${KUBEFILE}" |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 144 | sudo chown $(id -u):$(id -g) "${KUBEFILE}" |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 145 | 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] | 146 | chmod 700 "${KUBEFILE}" |
| 147 | echo |
| 148 | echo "Credentials saved at ${KUBEFILE}" |
| 149 | echo |
| 150 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 151 | } |
| 152 | |
| 153 | # main |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame^] | 154 | while getopts ":D:i:p:-: " o; do |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 155 | case "${o}" in |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 156 | i) |
| 157 | DEFAULT_IP="${OPTARG}" |
| 158 | ;; |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 159 | D) |
| 160 | OSM_DEVOPS="${OPTARG}" |
| 161 | ;; |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame^] | 162 | p) |
| 163 | DOCKER_PROXY_URL="${OPTARG}" |
| 164 | ;; |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 165 | -) |
| 166 | [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue |
| 167 | echo -e "Invalid option: '--$OPTARG'\n" >&2 |
| 168 | exit 1 |
| 169 | ;; |
| 170 | :) |
| 171 | echo "Option -$OPTARG requires an argument" >&2 |
| 172 | exit 1 |
| 173 | ;; |
| 174 | \?) |
| 175 | echo -e "Invalid option: '-$OPTARG'\n" >&2 |
| 176 | exit 1 |
| 177 | ;; |
| 178 | *) |
| 179 | exit 1 |
| 180 | ;; |
| 181 | esac |
| 182 | done |
| 183 | |
| garciadeblas | 8298116 | 2024-07-23 15:24:00 +0200 | [diff] [blame] | 184 | DEBUG_INSTALL=${DEBUG_INSTALL:-} |
| 185 | DEFAULT_IP=${DEFAULT_IP:-"127.0.0.1"} |
| 186 | OSM_DEVOPS=${OSM_DEVOPS:-"/usr/share/osm-devops"} |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame^] | 187 | DOCKER_PROXY_URL=${DOCKER_PROXY_URL=-} |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 188 | echo "DEBUG_INSTALL=${DEBUG_INSTALL}" |
| 189 | echo "DEFAULT_IP=${DEFAULT_IP}" |
| 190 | echo "OSM_DEVOPS=${OSM_DEVOPS}" |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame^] | 191 | echo "DOCKER_PROXY_URL=${DOCKER_PROXY_URL}" |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 192 | echo "HOME=$HOME" |
| 193 | |
| garciadeblas | 8298116 | 2024-07-23 15:24:00 +0200 | [diff] [blame] | 194 | source $OSM_DEVOPS/common/logging |
| 195 | source $OSM_DEVOPS/common/track |
| 196 | |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame^] | 197 | configure_registry |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 198 | install_k3s |
| 199 | track k8scluster k3s_install_ok |
| 200 | check_for_readiness |
| 201 | track k8scluster k3s_node_ready_ok |
| 202 | # update_service_nodeport_range |
| 203 | # check_for_readiness |
| 204 | # track k8scluster k3s_update_nodeport_range_ok |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 205 | save_kubeconfig |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 206 | track k8scluster k3s_creds_ok |