| 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/ |
| garciadeblas | 6075ff2 | 2024-09-19 12:38:36 +0200 | [diff] [blame] | 19 | K8S_CLIENT_VERSION="v1.29.3" |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 20 | K8S_VERSION="v1.29.3+k3s1" |
| 21 | |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame] | 22 | # configure registry |
| 23 | function configure_registry() { |
| 24 | if [ -n "${DOCKER_PROXY_URL}" ]; then |
| 25 | echo "Configuring docker proxy URL in /etc/rancher/k3s/registries.yaml" |
| garciadeblas | 8692f2a | 2024-11-18 12:58:29 +0100 | [diff] [blame] | 26 | sudo mkdir -p /etc/rancher/k3s/ |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame] | 27 | cat << EOF | sudo tee /etc/rancher/k3s/registries.yaml > /dev/null |
| 28 | mirrors: |
| 29 | docker.io: |
| 30 | endpoint: |
| 31 | - "${DOCKER_PROXY_URL}" |
| 32 | EOF |
| 33 | fi |
| garciadeblas | 2efb98b | 2024-08-21 21:19:36 +0200 | [diff] [blame] | 34 | if [ -n "${DOCKER_REGISTRY_URL}" ]; then |
| 35 | echo "Configuring docker private registry in /etc/rancher/k3s/registries.yaml" |
| 36 | cat << EOF | sudo tee -a /etc/rancher/k3s/registries.yaml > /dev/null |
| 37 | configs: |
| 38 | ${DOCKER_REGISTRY_URL}: |
| 39 | auth: |
| 40 | username: ${DOCKER_REGISTRY_USER} |
| 41 | password: ${DOCKER_REGISTRY_PASSWORD} |
| 42 | EOF |
| 43 | fi |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 46 | # installs k3s |
| 47 | function install_k3s() { |
| 48 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 49 | export INSTALL_K3S_EXEC="--disable traefik" |
| garciadeblas | 6a2de6d | 2024-09-27 11:34:55 +0200 | [diff] [blame] | 50 | |
| 51 | # Regular installation of K3s |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 52 | curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K8S_VERSION} sh -s - |
| garciadeblas | 6a2de6d | 2024-09-27 11:34:55 +0200 | [diff] [blame] | 53 | |
| 54 | # If specified a public IP, K3s service is updated accordingly and restarted |
| 55 | if [ -n "${K3S_PUBLIC_IP}" ]; then |
| 56 | # Back-up service config file to home |
| 57 | cp /etc/systemd/system/k3s.service ~/BASE-k3s.service |
| 58 | |
| 59 | # Generate new service config file with additions for using a public IP |
| 60 | ( |
| 61 | cat ~/BASE-k3s.service | sed '${/^$/d}' |
| 62 | echo -e "\t'--node-external-ip' \\" |
| 63 | echo -e "\t'${K3S_PUBLIC_IP}' \\" |
| 64 | echo |
| 65 | )| \ |
| 66 | tee ~/PUBLIC-k3s.service |
| 67 | |
| 68 | # Replace service config and apply |
| 69 | sudo cp ~/PUBLIC-k3s.service /etc/systemd/system/k3s.service |
| 70 | sudo systemctl daemon-reload |
| 71 | sudo systemctl restart k3s |
| 72 | |
| 73 | # Cleanup |
| 74 | rm ~/BASE-k3s.service ~/PUBLIC-k3s.service |
| 75 | fi |
| 76 | |
| 77 | # Make kubeconfig permissions less restrictive |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 78 | sudo chmod 644 /etc/rancher/k3s/k3s.yaml |
| 79 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 80 | } |
| 81 | |
| 82 | # updates service nodeport range |
| 83 | function update_service_nodeport_range() { |
| 84 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 85 | sudo k3s server --kube-apiserver-arg=service-node-port-range=80-32767 |
| 86 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 87 | } |
| 88 | |
| 89 | # checks cluster readiness |
| 90 | function check_for_readiness() { |
| 91 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 92 | # Check for Ready node, takes ~30 seconds |
| 93 | echo "Waiting for K8s nodes to be ready" |
| 94 | local time_for_failure=60 # seconds broken |
| 95 | local sampling_period=5 # seconds |
| 96 | local counter=0 |
| 97 | local cluster_ready="" |
| 98 | while (( counter < time_for_failure )) |
| 99 | do |
| 100 | kubectl get nodes |grep master |grep -v none | grep Ready |
| 101 | if [ $? -eq 0 ] ; then |
| 102 | echo "K8s cluster is ready" |
| 103 | cluster_ready="y" |
| 104 | break |
| 105 | else |
| 106 | echo "K8s cluster is not ready yet" |
| 107 | counter=$((counter + sampling_period)) |
| 108 | sleep ${sampling_period} |
| 109 | fi |
| 110 | done |
| 111 | [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster "K3s cluster nodes not ready after $time_for_failure seconds." |
| 112 | |
| 113 | echo "Waiting for pods to be ready" |
| 114 | local time_for_readiness=20 # seconds ready |
| 115 | local time_for_failure=100 # seconds broken |
| 116 | |
| 117 | # Equivalent number of samples |
| 118 | oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready |
| 119 | failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken |
| 120 | failures_in_a_row=0 |
| 121 | oks_in_a_row=0 |
| 122 | #################################################################################### |
| 123 | # Loop to check system readiness |
| 124 | #################################################################################### |
| 125 | K3S_NAMESPACE=kube-system |
| 126 | while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]] |
| 127 | do |
| 128 | # State of pods rather than completed jobs |
| 129 | K3S_PODS_STATE=$(kubectl get pod -n ${K3S_NAMESPACE} --no-headers |grep -v Completed 2>&1) |
| 130 | K3S_PODS_READY=$(echo "${K3S_PODS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}') |
| 131 | K3S_PODS_NOT_READY=$(echo "${K3S_PODS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}') |
| 132 | COUNT_K3S_PODS_READY=$(echo "${K3S_PODS_READY}"| grep -v -e '^$' | wc -l) |
| 133 | COUNT_K3S_PODS_NOT_READY=$(echo "${K3S_PODS_NOT_READY}" | grep -v -e '^$' | wc -l) |
| 134 | |
| 135 | # OK sample |
| 136 | if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]] |
| 137 | then |
| 138 | ((++oks_in_a_row)) |
| 139 | failures_in_a_row=0 |
| 140 | echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r |
| 141 | # NOK sample |
| 142 | else |
| 143 | ((++failures_in_a_row)) |
| 144 | oks_in_a_row=0 |
| 145 | echo |
| 146 | echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold} |
| 147 | |
| 148 | # Reports failed pods in K3S |
| 149 | if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]] |
| 150 | then |
| 151 | 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:" |
| 152 | echo "${K3S_PODS_NOT_READY}" |
| 153 | echo |
| 154 | fi |
| 155 | fi |
| 156 | |
| 157 | #------------ NEXT SAMPLE |
| 158 | sleep ${sampling_period} |
| 159 | done |
| 160 | |
| 161 | #################################################################################### |
| 162 | # OUTCOME |
| 163 | #################################################################################### |
| 164 | if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]] |
| 165 | then |
| 166 | echo |
| 167 | FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN" |
| 168 | else |
| 169 | echo |
| 170 | echo "K8S CLUSTER IS READY" |
| 171 | fi |
| 172 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 173 | } |
| 174 | |
| garciadeblas | 6075ff2 | 2024-09-19 12:38:36 +0200 | [diff] [blame] | 175 | # Install kubectl client |
| 176 | function install_kubectl() { |
| 177 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 178 | curl -LO "https://dl.k8s.io/release/${K8S_CLIENT_VERSION}/bin/linux/amd64/kubectl" |
| 179 | sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl |
| 180 | rm kubectl |
| 181 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 182 | } |
| 183 | |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 184 | # Initializes kubeconfig file |
| 185 | function save_kubeconfig() { |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 186 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 187 | KUBEDIR="${HOME}/.kube" |
| 188 | KUBEFILE="$KUBEDIR/config" |
| 189 | mkdir -p "${KUBEDIR}" |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 190 | K3S_KUBECONFIG="/etc/rancher/k3s/k3s.yaml" |
| 191 | sudo cp "${K3S_KUBECONFIG}" "${KUBEFILE}" |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 192 | sudo chown $(id -u):$(id -g) "${KUBEFILE}" |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 193 | 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] | 194 | chmod 700 "${KUBEFILE}" |
| 195 | echo |
| 196 | echo "Credentials saved at ${KUBEFILE}" |
| 197 | echo |
| 198 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 199 | } |
| 200 | |
| 201 | # main |
| garciadeblas | 6a2de6d | 2024-09-27 11:34:55 +0200 | [diff] [blame] | 202 | while getopts ":D:i:e:p:d:u:P:-: " o; do |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 203 | case "${o}" in |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 204 | i) |
| 205 | DEFAULT_IP="${OPTARG}" |
| 206 | ;; |
| garciadeblas | 6a2de6d | 2024-09-27 11:34:55 +0200 | [diff] [blame] | 207 | e) |
| 208 | K3S_PUBLIC_IP="${OPTARG}" |
| 209 | ;; |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 210 | D) |
| 211 | OSM_DEVOPS="${OPTARG}" |
| 212 | ;; |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame] | 213 | p) |
| 214 | DOCKER_PROXY_URL="${OPTARG}" |
| 215 | ;; |
| garciadeblas | 2efb98b | 2024-08-21 21:19:36 +0200 | [diff] [blame] | 216 | d) |
| 217 | DOCKER_REGISTRY_URL="${OPTARG}" |
| 218 | ;; |
| 219 | u) |
| 220 | DOCKER_REGISTRY_USER="${OPTARG}" |
| 221 | ;; |
| 222 | P) |
| 223 | DOCKER_REGISTRY_PASSWORD="${OPTARG}" |
| 224 | ;; |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 225 | -) |
| 226 | [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue |
| 227 | echo -e "Invalid option: '--$OPTARG'\n" >&2 |
| 228 | exit 1 |
| 229 | ;; |
| 230 | :) |
| 231 | echo "Option -$OPTARG requires an argument" >&2 |
| 232 | exit 1 |
| 233 | ;; |
| 234 | \?) |
| 235 | echo -e "Invalid option: '-$OPTARG'\n" >&2 |
| 236 | exit 1 |
| 237 | ;; |
| 238 | *) |
| 239 | exit 1 |
| 240 | ;; |
| 241 | esac |
| 242 | done |
| 243 | |
| garciadeblas | 8298116 | 2024-07-23 15:24:00 +0200 | [diff] [blame] | 244 | DEBUG_INSTALL=${DEBUG_INSTALL:-} |
| 245 | DEFAULT_IP=${DEFAULT_IP:-"127.0.0.1"} |
| 246 | OSM_DEVOPS=${OSM_DEVOPS:-"/usr/share/osm-devops"} |
| garciadeblas | ae02bac | 2024-11-20 12:55:45 +0100 | [diff] [blame] | 247 | DOCKER_PROXY_URL=${DOCKER_PROXY_URL:-} |
| 248 | DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL:-} |
| 249 | DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER:-} |
| 250 | DOCKER_REGISTRY_PASSWORD=${DOCKER_REGISTRY_PASSWORD:-} |
| garciadeblas | 6a2de6d | 2024-09-27 11:34:55 +0200 | [diff] [blame] | 251 | K3S_PUBLIC_IP=${K3S_PUBLIC_IP:-} |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 252 | echo "DEBUG_INSTALL=${DEBUG_INSTALL}" |
| 253 | echo "DEFAULT_IP=${DEFAULT_IP}" |
| 254 | echo "OSM_DEVOPS=${OSM_DEVOPS}" |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame] | 255 | echo "DOCKER_PROXY_URL=${DOCKER_PROXY_URL}" |
| garciadeblas | 2efb98b | 2024-08-21 21:19:36 +0200 | [diff] [blame] | 256 | echo "DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL}" |
| 257 | echo "DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER}" |
| garciadeblas | 6a2de6d | 2024-09-27 11:34:55 +0200 | [diff] [blame] | 258 | echo "K3S_PUBLIC_IP=${K3S_PUBLIC_IP}" |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 259 | echo "HOME=$HOME" |
| 260 | |
| garciadeblas | 8298116 | 2024-07-23 15:24:00 +0200 | [diff] [blame] | 261 | source $OSM_DEVOPS/common/logging |
| 262 | source $OSM_DEVOPS/common/track |
| 263 | |
| garciadeblas | 117fd4a | 2024-08-21 18:18:14 +0200 | [diff] [blame] | 264 | configure_registry |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 265 | install_k3s |
| 266 | track k8scluster k3s_install_ok |
| 267 | check_for_readiness |
| 268 | track k8scluster k3s_node_ready_ok |
| 269 | # update_service_nodeport_range |
| 270 | # check_for_readiness |
| 271 | # track k8scluster k3s_update_nodeport_range_ok |
| garciadeblas | 6075ff2 | 2024-09-19 12:38:36 +0200 | [diff] [blame] | 272 | install_kubectl |
| garciadeblas | 1f33848 | 2024-07-04 19:26:54 +0200 | [diff] [blame] | 273 | save_kubeconfig |
| garciadeblas | b379741 | 2024-06-06 14:26:24 +0200 | [diff] [blame] | 274 | track k8scluster k3s_creds_ok |