Skip to content

Commit 5058106

Browse files
committed
setup_cache_for_template: add kernel and initrd cache support
refactor some common codes from `setup_cache_for_template` and `calculate-cache.sh` into `cache-common-inc.sh` Signed-off-by: Norio Nomura <[email protected]>
1 parent f8acc9d commit 5058106

File tree

4 files changed

+388
-217
lines changed

4 files changed

+388
-217
lines changed

.github/actions/setup_cache_for_template/action.yml

+23-112
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ inputs:
77
template:
88
description: template yaml file
99
required: true
10-
detect-containerd:
11-
description: detect containerd usage from template by using limactl validate
12-
required: false
13-
default: 'true'
1410
runs:
1511
using: "composite"
1612
steps:
@@ -29,120 +25,35 @@ runs:
2925
id: cache-params-from-template
3026
run: |
3127
set -eux
32-
arch="${{ inputs.arch }}"
33-
template="${{ inputs.template }}"
34-
detect_containerd="${{ inputs.detect-containerd }}"
35-
if [[ $detect_containerd == "true" ]] && ! command -v limactl &>/dev/null; then
36-
echo "containerd detection is disabled because limactl is not found" >&2
37-
detect_containerd="false"
38-
fi
39-
case "$template" in
40-
https://*)
41-
tmp_yaml=$(mktemp -d)/template.yaml
42-
curl -sSLf "$template" > "$tmp_yaml" || exit 1
43-
template=$tmp_yaml
44-
;;
45-
*)
46-
test -f "$template" || exit 1
47-
;;
48-
esac
49-
50-
# detect arch from template if not provided
51-
arch="${arch:-$(yq '.arch // ""' "$template")}"
52-
arch="${arch:-$(uname -m)}"
53-
# normalize arch. amd64 -> x86_64, arm64 -> aarch64
54-
case "$arch" in
55-
amd64) arch=x86_64 ;;
56-
arm64) arch=aarch64 ;;
57-
esac
58-
59-
# extract digest, location and size by parsing template using arch
60-
readonly yq_filter="
61-
[
62-
.images | map(select(.arch == \"${arch}\")) | [.[0,1].location, .[0,1].digest],
63-
.containerd|[.system or .user],
64-
.containerd.archives | map(select(.arch == \"${arch}\")) | [.[0].location, .[0].digest]
65-
]|flatten|.[]
66-
"
67-
if [[ $detect_containerd == "true" ]]; then
68-
parsed=$(LIMA_HOME=$(mktemp -d) limactl validate "$template" --fill 2>/dev/null | yq eval "${yq_filter}")
69-
else
70-
parsed=$(yq eval "${yq_filter}" "$template")
71-
fi
72-
# macOS earlier than 15.0 uses bash 3.2.57, which does not support readarray -t
73-
# readarray -t arr <<<"$parsed"
74-
while IFS= read -r line; do arr+=("$line"); done <<<"${parsed}"
75-
readonly locations=("${arr[@]:0:2}") digests=("${arr[@]:2:2}")
76-
readonly containerd="${arr[4]}" containerd_location="${arr[5]}" containerd_digest="${arr[6]}"
77-
for ((i = 0; i < ${#locations[@]}; i++)); do
78-
[[ ${locations[i]} != "null" ]] || continue
79-
http_code=$(curl -sIL -w "%{http_code}" "${locations[i]}" -o /dev/null)
80-
if [[ ${http_code} -eq 200 ]]; then
81-
location=${locations[i]}
82-
digest=${digests[i]}
83-
break
84-
fi
85-
done
86-
if [[ -z ${location} ]]; then
87-
echo "Failed to get the image location for ${template}" >&2
88-
exit 1
89-
fi
90-
91-
function location_to_sha256() {
92-
local location=$1
93-
if command -v sha256sum > /dev/null; then
94-
sha256="$(echo -n "$location" | sha256sum | cut -d' ' -f1)"
95-
elif command -v shasum > /dev/null; then
96-
sha256="$(echo -n "$location" | shasum -a 256 | cut -d' ' -f1)"
97-
else
98-
echo "sha256sum or shasum not found" >&2
99-
exit 1
100-
fi
101-
echo "$sha256"
102-
}
103-
104-
function location_to_cache_path() {
105-
local location=$1
106-
sha256=$(location_to_sha256 "$location") && echo ".download/by-url-sha256/$sha256"
107-
}
108-
109-
# path to cache
110-
image_cache_path=$(location_to_cache_path "$location")
111-
echo "path=$image_cache_path" >> "$GITHUB_OUTPUT"
28+
source hack/cache-common-inc.sh
29+
print_cache_informations_from_template "${{ inputs.template }}" "${{ inputs.arch }}" >> "$GITHUB_OUTPUT"
30+
shell: bash
11231

113-
# key for cache
114-
image_basename=$(basename "$location")
115-
if [[ "$digest" != "null" ]]; then
116-
key="image:$image_basename-$digest"
117-
else
118-
# use sha256 of location as key if digest is not available
119-
key="image:$image_basename-url-sha256:$(location_to_sha256 "$location")"
120-
fi
121-
echo "key=$key" >> "$GITHUB_OUTPUT"
32+
- name: "Cache ${{ steps.cache-params-from-template.outputs.image-path }}"
33+
if: ${{ steps.cache-params-from-template.outputs.image-key != '' }}
34+
# avoid using `~` in path that will be expanded to platform specific home directory
35+
uses: actions/cache@v4
36+
with:
37+
path: ${{ steps.cache-params-from-template.outputs.image-path }}
38+
key: ${{ steps.cache-params-from-template.outputs.image-key }}
39+
enableCrossOsArchive: true
12240

123-
# containerd path and key for cache
124-
if [[ $containerd == "true" && "$containerd_location" != "null" ]]; then
125-
containerd_basename=$(basename "$containerd_location")
126-
if [[ ${containerd_digest} != "null" ]]; then
127-
containerd_key="containerd:$containerd_basename-$containerd_digest"
128-
else
129-
containerd_key="containerd:$containerd_basename-url-sha256:$(sha256 "$containerd_location")"
130-
fi
131-
echo "containerd-key=$containerd_key" >> "$GITHUB_OUTPUT"
132-
containerd_cache_path=$(location_to_cache_path "$containerd_location")
133-
echo "containerd-path=$containerd_cache_path" >> "$GITHUB_OUTPUT"
134-
else
135-
echo "containerd-key=" >> "$GITHUB_OUTPUT"
136-
echo "containerd-path=" >> "$GITHUB_OUTPUT"
137-
fi
138-
shell: bash
41+
- name: "Cache ${{ steps.cache-params-from-template.outputs.kernel-path }}"
42+
if: ${{ steps.cache-params-from-template.outputs.kernel-key != '' }}
43+
# avoid using `~` in path that will be expanded to platform specific home directory
44+
uses: actions/cache@v4
45+
with:
46+
path: ${{ steps.cache-params-from-template.outputs.kernel-path }}
47+
key: ${{ steps.cache-params-from-template.outputs.kernel-key }}
48+
enableCrossOsArchive: true
13949

140-
- name: "Cache ${{ steps.cache-params-from-template.outputs.path }}"
50+
- name: "Cache ${{ steps.cache-params-from-template.outputs.initrd-path }}"
51+
if: ${{ steps.cache-params-from-template.outputs.initrd-key != '' }}
14152
# avoid using `~` in path that will be expanded to platform specific home directory
14253
uses: actions/cache@v4
14354
with:
144-
path: ${{ steps.cache-params-from-template.outputs.path }}
145-
key: ${{ steps.cache-params-from-template.outputs.key }}
55+
path: ${{ steps.cache-params-from-template.outputs.initrd-path }}
56+
key: ${{ steps.cache-params-from-template.outputs.initrd-key }}
14657
enableCrossOsArchive: true
14758

14859
- name: "Cache ${{ steps.cache-params-from-template.outputs.containerd-key }}"

.github/workflows/test.yml

+9-10
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,6 @@ jobs:
163163
run: make
164164
- name: Install
165165
run: make install
166-
- name: Cache image used by default.yaml
167-
uses: ./.github/actions/setup_cache_for_template
168-
with:
169-
template: templates/default.yaml
170166
- name: Validate templates
171167
run: find -L templates -name '*.yaml' | xargs limactl validate
172168
- name: Install test dependencies
@@ -180,11 +176,15 @@ jobs:
180176
# GitHub runners seem to have lima installed by brew already; we don't want/need it
181177
time brew uninstall --ignore-dependencies lima colima
182178
time brew install qemu bash coreutils curl jq
183-
- name: "Show cache"
184-
run: ./hack/debug-cache.sh
185179
- name: "Inject `no_timer_check` to kernel cmdline"
186180
# workaround to https://github.com/lima-vm/lima/issues/84
187181
run: ./hack/inject-cmdline-to-template.sh templates/default.yaml no_timer_check
182+
- name: Cache image used by default.yaml
183+
uses: ./.github/actions/setup_cache_for_template
184+
with:
185+
template: templates/default.yaml
186+
- name: "Show cache"
187+
run: ./hack/debug-cache.sh
188188
- name: "Test default.yaml"
189189
uses: nick-invision/retry@v3
190190
with:
@@ -328,6 +328,9 @@ jobs:
328328
run: make
329329
- name: Install
330330
run: make install
331+
- name: "Inject `no_timer_check` to kernel cmdline"
332+
# workaround to https://github.com/lima-vm/lima/issues/84
333+
run: ./hack/inject-cmdline-to-template.sh templates/vmnet.yaml no_timer_check
331334
- name: Cache image used by vmnet.yaml
332335
uses: ./.github/actions/setup_cache_for_template
333336
with:
@@ -350,9 +353,6 @@ jobs:
350353
- name: Unit test (pkg/networks) with socket_vmnet
351354
# Set -count=1 to disable cache
352355
run: go test -v -count=1 ./pkg/networks/...
353-
- name: "Inject `no_timer_check` to kernel cmdline"
354-
# workaround to https://github.com/lima-vm/lima/issues/84
355-
run: ./hack/inject-cmdline-to-template.sh templates/vmnet.yaml no_timer_check
356356
- name: Test socket_vmnet
357357
uses: nick-invision/retry@v3
358358
with:
@@ -381,7 +381,6 @@ jobs:
381381
uses: ./.github/actions/setup_cache_for_template
382382
with:
383383
template: https://raw.githubusercontent.com/lima-vm/lima/${{ matrix.oldver }}/examples/ubuntu-lts.yaml
384-
detect-containerd: "false"
385384
- name: Install test dependencies
386385
run: brew install qemu bash coreutils
387386
- name: Test

0 commit comments

Comments
 (0)