|
2 | 2 | #
|
3 | 3 | # Copyright (c) 2024 Zededa, Inc.
|
4 | 4 | # SPDX-License-Identifier: Apache-2.0
|
| 5 | +K3S_VERSION=v1.28.5+k3s1 |
| 6 | + |
| 7 | +# |
| 8 | +# Handle any migrations needed due to updated cluster-init.sh |
| 9 | +# This is expected to be bumped any time: |
| 10 | +# - a migration is needed (new path for something) |
| 11 | +# - a version bump of: K3s, multus, kubevirt, cdi, longhorn |
| 12 | +# |
| 13 | +KUBE_VERSION=1 |
| 14 | +APPLIED_KUBE_VERSION_PATH="/var/lib/applied-kube-version" |
| 15 | +update_Version_Set() { |
| 16 | + version=$1 |
| 17 | + echo "$version" > "$APPLIED_KUBE_VERSION_PATH" |
| 18 | +} |
| 19 | + |
| 20 | +update_Version_Get() { |
| 21 | + if [ ! -f "$APPLIED_KUBE_VERSION_PATH" ]; then |
| 22 | + # First Boot |
| 23 | + echo "0" |
| 24 | + fi |
| 25 | + cat "$APPLIED_KUBE_VERSION_PATH" |
| 26 | +} |
| 27 | + |
| 28 | +# |
| 29 | +# update_Failed() |
| 30 | +# Mark failure if Status == COMP_STATUS_FAILED and DestinationKubeUpdateVersion == KUBE_VERSION |
| 31 | +# This allows: |
| 32 | +# - update retry control for a given version |
| 33 | +# - recovery update if the eve os version is updated to another release (with a different cluster-init.sh) |
| 34 | +# |
| 35 | +UPDATE_STATUS_PATH=/persist/status/zedkube/KubeClusterUpdateStatus/global.json |
| 36 | +update_Failed() { |
| 37 | + if [ -f $UPDATE_STATUS_PATH ]; then |
| 38 | + if [ "$(jq --arg gen $KUBE_VERSION '.Status==4 and .DestinationKubeUpdateVersion==$gen' < $UPDATE_STATUS_PATH)" = "true" ]; then |
| 39 | + return 0 |
| 40 | + fi |
| 41 | + fi |
| 42 | + return 1 |
| 43 | +} |
| 44 | + |
| 45 | +trigger_k3s_selfextraction() { |
| 46 | + # Run some k3s cli command so that binaries are self-extracted |
| 47 | + /usr/bin/k3s check-config >> "$INSTALL_LOG" 2>&1 |
| 48 | +} |
5 | 49 |
|
6 | 50 | link_multus_into_k3s() {
|
7 | 51 | ln -s /var/lib/cni/bin/multus /var/lib/rancher/k3s/data/current/bin/multus
|
8 | 52 | }
|
| 53 | + |
| 54 | +update_k3s() { |
| 55 | + logmsg "Installing K3S version $K3S_VERSION" |
| 56 | + mkdir -p /var/lib/k3s/bin |
| 57 | + /usr/bin/curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K3S_VERSION} INSTALL_K3S_SKIP_ENABLE=true INSTALL_K3S_SKIP_START=true INSTALL_K3S_BIN_DIR=/var/lib/k3s/bin sh - |
| 58 | + sleep 5 |
| 59 | + logmsg "Initializing K3S version $K3S_VERSION" |
| 60 | + ln -s /var/lib/k3s/bin/* /usr/bin |
| 61 | + trigger_k3s_selfextraction |
| 62 | + link_multus_into_k3s |
| 63 | + touch /var/lib/k3s_installed_unpacked |
| 64 | +} |
| 65 | + |
| 66 | +# k3s_get_version: return version in form "vW.X.Y+k3sZ" |
| 67 | +k3s_get_version() { |
| 68 | + if [ ! -f /var/lib/k3s/bin/k3s ]; then |
| 69 | + echo "v0.0.0+k3s0" |
| 70 | + return |
| 71 | + fi |
| 72 | + /var/lib/k3s/bin/k3s --version | awk '$1=="k3s" {print $3}' | tr -d '\n' |
| 73 | +} |
| 74 | + |
| 75 | +# Run on every boot before k3s starts |
| 76 | +Update_CheckNodeComponents() { |
| 77 | + applied_version=$(update_Version_Get) |
| 78 | + if [ "$KUBE_VERSION" = "$applied_version" ]; then |
| 79 | + return |
| 80 | + fi |
| 81 | + |
| 82 | + if update_Failed; then |
| 83 | + return |
| 84 | + fi |
| 85 | + logmsg "update_HandleNode: version:$KUBE_VERSION appliedversion:$applied_version continuing" |
| 86 | + |
| 87 | + # Handle version specific node migrations here |
| 88 | + |
| 89 | + # Handle node specific updates, just k3s for now |
| 90 | + if [ "$(k3s_get_version)" != "$K3S_VERSION" ]; then |
| 91 | + publishUpdateStatus "k3s" "download" |
| 92 | + update_k3s |
| 93 | + current_k3s_version=$(k3s_get_version) |
| 94 | + if [ "$current_k3s_version" != "$K3S_VERSION" ]; then |
| 95 | + logmsg "k3s version mismatch after install:$current_k3s_version" |
| 96 | + publishUpdateStatus "k3s" "failed" "version mismatch after install:$current_k3s_version" |
| 97 | + else |
| 98 | + logmsg "k3s installed and unpacked or copied" |
| 99 | + publishUpdateStatus "k3s" "completed" |
| 100 | + fi |
| 101 | + fi |
| 102 | +} |
| 103 | + |
| 104 | +# Run on every boot after k3s is started |
| 105 | +Update_CheckClusterComponents() { |
| 106 | + wait_for_item "update_cluster_pre" |
| 107 | + |
| 108 | + applied_version=$(update_Version_Get) |
| 109 | + if [ "$KUBE_VERSION" = "$applied_version" ]; then |
| 110 | + return |
| 111 | + fi |
| 112 | + |
| 113 | + if update_Failed; then |
| 114 | + return |
| 115 | + fi |
| 116 | + |
| 117 | + if ! update_isClusterReady; then |
| 118 | + return |
| 119 | + fi |
| 120 | + logmsg "update_HandleCluster: version:$KUBE_VERSION appliedversion:$applied_version continuing" |
| 121 | + |
| 122 | + # Handle cluster wide component updates |
| 123 | + for comp in multus kubevirt cdi longhorn; do |
| 124 | + while ! update_Component_CheckReady "$comp"; do |
| 125 | + logmsg "Component: $comp not ready on existing version" |
| 126 | + sleep 60 |
| 127 | + done |
| 128 | + logmsg "Component: $comp ready on existing version" |
| 129 | + if update_Component_IsRunningExpectedVersion "$comp"; then |
| 130 | + logmsg "Component:$comp running expected version, continuing" |
| 131 | + publishUpdateStatus "$comp" "completed" |
| 132 | + continue |
| 133 | + fi |
| 134 | + if ! update_Component "$comp"; then |
| 135 | + logmsg "Not continuing with further updates after component:${comp} update failed" |
| 136 | + break |
| 137 | + fi |
| 138 | + done |
| 139 | + |
| 140 | + update_Version_Set "$KUBE_VERSION" |
| 141 | + wait_for_item "update_cluster_post" |
| 142 | +} |
| 143 | + |
| 144 | +update_isClusterReady() { |
| 145 | + if ! kubectl cluster-info; then |
| 146 | + return 1 |
| 147 | + fi |
| 148 | + |
| 149 | + if ! update_Helper_APIResponding; then |
| 150 | + return 1 |
| 151 | + fi |
| 152 | + return 0 |
| 153 | +} |
| 154 | + |
| 155 | +# |
| 156 | +# Handle kube component updates |
| 157 | +# |
| 158 | +COMP_UPDATE_PATH="/usr/bin/update-component" |
| 159 | + |
| 160 | +update_Helper_APIResponding() { |
| 161 | + if $COMP_UPDATE_PATH --check-api-ready; then |
| 162 | + return 0 |
| 163 | + fi |
| 164 | + return 1 |
| 165 | +} |
| 166 | +update_Component_CheckReady() { |
| 167 | + comp=$1 |
| 168 | + if $COMP_UPDATE_PATH --versions-file /etc/expected_versions.yaml --component "$comp" --check-comp-ready; then |
| 169 | + return 0 |
| 170 | + fi |
| 171 | + return 1 |
| 172 | +} |
| 173 | +update_Component_Uptime() { |
| 174 | + comp=$1 |
| 175 | + $COMP_UPDATE_PATH --versions-file /etc/expected_versions.yaml --component "$comp" --get-uptime |
| 176 | +} |
| 177 | +update_Component_IsRunningExpectedVersion() { |
| 178 | + comp=$1 |
| 179 | + if $COMP_UPDATE_PATH --versions-file /etc/expected_versions.yaml --component "$comp" --compare; then |
| 180 | + return 0 |
| 181 | + fi |
| 182 | + return 1 |
| 183 | +} |
| 184 | + |
| 185 | +update_Component() { |
| 186 | + comp=$1 |
| 187 | + # Run go app to check and apply updates and block until new version is ready |
| 188 | + publishUpdateStatus "$comp" "in_progress" |
| 189 | + if $COMP_UPDATE_PATH --versions-file /etc/expected_versions.yaml --component "$comp" --upgrade; then |
| 190 | + publishUpdateStatus "$comp" "completed" |
| 191 | + return 0 |
| 192 | + fi |
| 193 | + upgrade_log_path="/persist/kubelog/upgrade-component.log" |
| 194 | + logmsg "update_Component comp:${comp} error starting update, see $upgrade_log_path" |
| 195 | + publishUpdateStatus "$comp" "failed" "error in $upgrade_log_path" |
| 196 | + return 1 |
| 197 | +} |
| 198 | + |
| 199 | +publishUpdateStatus() { |
| 200 | + component=$1 |
| 201 | + status=$2 |
| 202 | + errorstr="" |
| 203 | + if [ ! -x "$3" ]; then |
| 204 | + errorstr=$3 |
| 205 | + fi |
| 206 | + |
| 207 | + # If gen==0, then we are in the initial boot not updating, just installing first versions at most-likely first |
| 208 | + # boot of the device. Don't publish as this will trigger zedagent to claim baseos_updating. |
| 209 | + cur_version=$(update_Version_Get) |
| 210 | + if [ "$cur_version" = "0" ]; then |
| 211 | + return |
| 212 | + fi |
| 213 | + |
| 214 | + node=$(jq -r '.DeviceName' < /persist/status/zedagent/EdgeNodeInfo/global.json | tr -d '\n') |
| 215 | + logmsg "publishUpdateStatus() $node $component $status" |
| 216 | + |
| 217 | + pillarRootfs=/hostfs/containers/services/pillar/rootfs |
| 218 | + LD_LIBRARY_PATH=${pillarRootfs}/usr/lib/ ${pillarRootfs}/opt/zededa/bin/zedkube pubKubeClusterUpdateStatus "$node" "$component" "$status" "$KUBE_VERSION" "$errorstr" |
| 219 | + rc=$? |
| 220 | + if [ $rc -ne 0 ]; then |
| 221 | + logmsg "publishUpdateStatus() $node $component $status in error:$rc" |
| 222 | + fi |
| 223 | +} |
0 commit comments