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