|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +collateral_path=$HOME/collaterals/ |
| 4 | + |
| 5 | +K8S_VERSION="" |
| 6 | +K3S_VERSION="" |
| 7 | + |
| 8 | +fetch_current_k8s_version() { |
| 9 | + local version |
| 10 | + |
| 11 | + version=$(yq .jobs.envtest.strategy.matrix.version[-1] $GITHUB_WORKSPACE/.github/workflows/lib-validate.yaml) |
| 12 | + |
| 13 | + # cut the ".x" from the version |
| 14 | + version=$(echo $version | tr -d '"' | sed 's/\.x$//') |
| 15 | + |
| 16 | + if [ -z "$version" ]; then |
| 17 | + echo "Couldn't find k8s version in the workflow file" |
| 18 | + |
| 19 | + return 1 |
| 20 | + fi |
| 21 | + |
| 22 | + K8S_VERSION=$version |
| 23 | +} |
| 24 | + |
| 25 | +k3s_version_for_k8s_version() { |
| 26 | + local requested="$K8S_VERSION" |
| 27 | + |
| 28 | + local known_versions |
| 29 | + |
| 30 | + known_versions="v1.34.1+k3s1;v1.33.5+k3s1;v1.32.9+k3s1;v1.31.9+k3s1;v1.30.13+k3s1" |
| 31 | + |
| 32 | + local latest |
| 33 | + latest=$(echo $known_versions | tr ';' '\n' | grep "$requested" | head -1) |
| 34 | + if [ -z "$latest" ]; then |
| 35 | + echo "No k3s version found for requested k8s version $requested" |
| 36 | + |
| 37 | + return 1 |
| 38 | + fi |
| 39 | + |
| 40 | + K3S_VERSION=$latest |
| 41 | +} |
| 42 | + |
| 43 | +download_k3s_binaries() { |
| 44 | + mkdir -p $collateral_path/k3s-cache |
| 45 | + |
| 46 | + [ -e $collateral_path/k3s-cache/install-k3s.sh ] || { |
| 47 | + wget https://get.k3s.io/ -O $collateral_path/k3s-cache/install-k3s.sh || { |
| 48 | + echo "Failed to download k3s install script" |
| 49 | + |
| 50 | + return 1 |
| 51 | + } |
| 52 | + |
| 53 | + chmod +x $collateral_path/k3s-cache/install-k3s.sh |
| 54 | + } |
| 55 | + |
| 56 | + [ -e $collateral_path/k3s-cache/${K3S_VERSION} ] && { |
| 57 | + echo "Using cached k3s binary" |
| 58 | + |
| 59 | + return 0 |
| 60 | + } |
| 61 | + |
| 62 | + local k3s_ver_encoded |
| 63 | + |
| 64 | + k3s_ver_encoded=$(echo "$K3S_VERSION" | sed -e 's/+/\%2B/') |
| 65 | + |
| 66 | + local k3s_url |
| 67 | + k3s_url="https://github.com/k3s-io/k3s/releases/download/${k3s_ver_encoded}/k3s" |
| 68 | + |
| 69 | + local k3s_images_url |
| 70 | + k3s_images_url="https://github.com/k3s-io/k3s/releases/download/${k3s_ver_encoded}/k3s-airgap-images-amd64.tar.zst" |
| 71 | + |
| 72 | + mkdir -p $collateral_path/k3s-cache/${K3S_VERSION} |
| 73 | + |
| 74 | + wget -q -O $collateral_path/k3s-cache/${K3S_VERSION}/k3s $k3s_url || { |
| 75 | + echo "Failed to download k3s binary from $k3s_url" |
| 76 | + return 1 |
| 77 | + } |
| 78 | + |
| 79 | + chmod +x $collateral_path/k3s-cache/${K3S_VERSION}/k3s |
| 80 | + |
| 81 | + wget -q -O $collateral_path/k3s-cache/${K3S_VERSION}/images.tar.zst $k3s_images_url || { |
| 82 | + echo "Failed to download k3s images from $k3s_images_url" |
| 83 | + return 1 |
| 84 | + } |
| 85 | + |
| 86 | + return 0 |
| 87 | +} |
| 88 | + |
| 89 | +print_large() { |
| 90 | + type figlet > /dev/null 2>&1 && { |
| 91 | + figlet "$@" |
| 92 | + } || { |
| 93 | + echo "========================================" |
| 94 | + echo "$@" |
| 95 | + echo "========================================" |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +wait_for_cluster_to_be_ready() { |
| 100 | + echo "Waiting for cluster to become accessible" |
| 101 | + |
| 102 | + for _ in $(seq 60); do |
| 103 | + kubectl get pods -A 2>&1 | grep -q "No resources found" || { |
| 104 | + echo "Cluster is accessible" |
| 105 | + break |
| 106 | + } |
| 107 | + |
| 108 | + echo -n "." |
| 109 | + sleep 1 |
| 110 | + done |
| 111 | + |
| 112 | + echo "Waiting for Pods to become ready" |
| 113 | + |
| 114 | + for _ in $(seq 60); do |
| 115 | + sleep 1 |
| 116 | + echo -n "." |
| 117 | + |
| 118 | + allcount=$(kubectl get pods -A --no-headers=true | wc -l) |
| 119 | + notreadycount=$(kubectl get pods -A --no-headers=true | grep -c -v -e Complete -e Running) |
| 120 | + |
| 121 | + if [ $allcount -lt 7 ]; then |
| 122 | + continue |
| 123 | + fi |
| 124 | + |
| 125 | + if [ $notreadycount -eq 0 ]; then |
| 126 | + echo "READY" |
| 127 | + |
| 128 | + return 0 |
| 129 | + fi |
| 130 | + done |
| 131 | + |
| 132 | + echo "" |
| 133 | + echo "Cluster did not become ready.." |
| 134 | + |
| 135 | + return 1 |
| 136 | +} |
| 137 | + |
| 138 | +prepare_cluster() { |
| 139 | + print_large "Prepare cluster" |
| 140 | + |
| 141 | + echo "Versions: $K3S_VERSION & $K8S_VERSION" |
| 142 | + |
| 143 | + [ -e /usr/local/bin/k3s-uninstall.sh ] && { |
| 144 | + echo "Found existing k3s install, removing it" |
| 145 | + |
| 146 | + k3s-uninstall.sh || return 1 |
| 147 | + } |
| 148 | + |
| 149 | + echo "prepare images" |
| 150 | + sudo mkdir -p /var/lib/rancher/k3s/agent/images/ && \ |
| 151 | + sudo cp $collateral_path/k3s-cache/$K3S_VERSION/images.tar.zst /var/lib/rancher/k3s/agent/images/k3s-airgap-images-amd64.tar.zst || return 1 |
| 152 | + sudo cp $collateral_path/k3s-cache/$K3S_VERSION/k3s /usr/local/bin/ || return 1 |
| 153 | + sudo chmod +x /usr/local/bin/k3s || return 1 |
| 154 | + |
| 155 | + echo "prepare kubelet config" |
| 156 | + cat <<EOF > /tmp/kubelet.conf |
| 157 | +apiVersion: kubelet.config.k8s.io/v1beta1 |
| 158 | +kind: KubeletConfiguration |
| 159 | +cpuManagerPolicy: static |
| 160 | +systemReserved: |
| 161 | + cpu: 2000m |
| 162 | + memory: 512M |
| 163 | +kubeReserved: |
| 164 | + cpu: 1000m |
| 165 | + memory: 256M |
| 166 | +EOF |
| 167 | + |
| 168 | + sudo mkdir -p /etc/rancher/k3s |
| 169 | + cat <<EOF > /tmp/k3s-config.yaml |
| 170 | +kubelet-arg: |
| 171 | + - config=/tmp/kubelet.conf |
| 172 | +EOF |
| 173 | + sudo mv /tmp/k3s-config.yaml /etc/rancher/k3s/config.yaml |
| 174 | + |
| 175 | + echo "prepare k3s cluster" |
| 176 | + INSTALL_K3S_SKIP_DOWNLOAD=true INSTALL_K3S_EXEC='--write-kubeconfig-mode=644' $collateral_path/k3s-cache/install-k3s.sh && \ |
| 177 | + sudo chmod +r /etc/rancher/k3s/k3s.yaml && \ |
| 178 | + sudo chmod o+rw /run/k3s/containerd/containerd.sock || { |
| 179 | + return 1 |
| 180 | + } |
| 181 | + |
| 182 | + export KUBECONFIG=/etc/rancher/k3s/k3s.yaml |
| 183 | + |
| 184 | + wait_for_cluster_to_be_ready || return 1 |
| 185 | + |
| 186 | + kubectl get nodes -o wide |
| 187 | + kubectl get pods -A |
| 188 | +} |
| 189 | + |
| 190 | +install_go() { |
| 191 | + local go_version |
| 192 | + go_version=$(grep "^go " $GITHUB_WORKSPACE/go.mod | head -1 | cut -c 4-) |
| 193 | + |
| 194 | + [ -e $collateral_path/go$go_version.linux-amd64.tar.gz ] || { |
| 195 | + wget https://go.dev/dl/go$go_version.linux-amd64.tar.gz -O $collateral_path/go$go_version.linux-amd64.tar.gz || exit 1 |
| 196 | + } |
| 197 | + |
| 198 | + mkdir -p ~/bin |
| 199 | + tar -xf $collateral_path/go$go_version.linux-amd64.tar.gz -C ~/bin || exit 1 |
| 200 | + |
| 201 | + export PATH=$PATH:~/bin/go/bin |
| 202 | + |
| 203 | + type go || { |
| 204 | + echo "Go installation failed" |
| 205 | + return 1 |
| 206 | + } |
| 207 | + |
| 208 | + go version || return 1 |
| 209 | +} |
| 210 | + |
| 211 | +install_k8s_deps() { |
| 212 | + print_large "Install cert-manager" |
| 213 | + kubectl apply --wait -f https://github.com/cert-manager/cert-manager/releases/download/v1.18.0/cert-manager.yaml || return 1 |
| 214 | +} |
| 215 | + |
| 216 | +prepare_to_build() { |
| 217 | + echo "Fetch vendor dependencies" |
| 218 | + make vendor || return 1 |
| 219 | + |
| 220 | + echo "Generate dummy licenses" |
| 221 | + |
| 222 | + for ldir in $(ls --ignore=internal cmd | xargs -I"{}" echo licenses/{}); do |
| 223 | + mkdir -p $GITHUB_WORKSPACE/$ldir |
| 224 | + touch $GITHUB_WORKSPACE/$ldir/dummy-license.txt |
| 225 | + done |
| 226 | +} |
| 227 | + |
| 228 | +# This should be executed only once per run. |
| 229 | +generate_tag() { |
| 230 | + # Extract the version from the reconciler. |
| 231 | + local BUILD_VERSION |
| 232 | + BUILD_VERSION=$(grep -r --include="*.go" 'ImageMinVersion =' ${GITHUB_WORKSPACE} | head -1 | sed -e 's/.*"\(.*\)".*/\1/') |
| 233 | + |
| 234 | + # Add random components to avoid collision with other images. |
| 235 | + BUILD_VERSION=$BUILD_VERSION-$GITHUB_RUN_NUMBER-$RANDOM |
| 236 | + |
| 237 | + export TAG=$BUILD_VERSION |
| 238 | +} |
| 239 | + |
| 240 | +cache_shared_images() { |
| 241 | + echo "Cache shared images" |
| 242 | + |
| 243 | + local SHARED_IMAGES="" |
| 244 | + |
| 245 | + echo $IMAGES | grep -q gpu && { |
| 246 | + SHARED_IMAGES="${SHARED_IMAGES}intel/intel-extension-for-pytorch:2.8.10-xpu " |
| 247 | + } |
| 248 | + |
| 249 | + for image in $SHARED_IMAGES; |
| 250 | + do |
| 251 | + echo "Downloading and caching $image" |
| 252 | + docker pull $image && docker save $image | sudo ctr -n k8s.io image import - || return 1 |
| 253 | + done |
| 254 | +} |
0 commit comments