Skip to content

Commit c888716

Browse files
committed
bash: make the lk yaml templating via envsubst a bit less tedious
- introduce `hook_template_vars` dict (KEY=value) - with `HOOK_VERSION`, `HOOK_KERNEL_IMAGE` et al directly - remove the nameref from `build_hook_linuxkit_container`, instead set directly in the `hook_template_vars` dict (`HOOK_CONTAINER_BOOTKIT_IMAGE` et al) - derive the envsubst dollar-prefixed list and the actual vars from the dict - before: refer to the variable 3 times (settage, export, dollar) - after: add variable to dict (once) Signed-off-by: Ricardo Pardini <[email protected]>
1 parent 3e273d8 commit c888716

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

bash/hook-lk-containers.sh

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ function build_all_hook_linuxkit_containers() {
77
# when adding new container builds here you'll also want to add them to the
88
# `linuxkit_build` function in the linuxkit.sh file.
99
# # NOTE: linuxkit containers must be in the images/ directory
10-
build_hook_linuxkit_container hook-bootkit HOOK_CONTAINER_BOOTKIT_IMAGE
11-
build_hook_linuxkit_container hook-docker HOOK_CONTAINER_DOCKER_IMAGE
12-
build_hook_linuxkit_container hook-mdev HOOK_CONTAINER_MDEV_IMAGE
13-
build_hook_linuxkit_container hook-containerd HOOK_CONTAINER_CONTAINERD_IMAGE
14-
build_hook_linuxkit_container hook-runc HOOK_CONTAINER_RUNC_IMAGE
15-
build_hook_linuxkit_container hook-embedded HOOK_CONTAINER_EMBEDDED_IMAGE
10+
build_hook_linuxkit_container hook-bootkit "HOOK_CONTAINER_BOOTKIT_IMAGE"
11+
build_hook_linuxkit_container hook-docker "HOOK_CONTAINER_DOCKER_IMAGE"
12+
build_hook_linuxkit_container hook-mdev "HOOK_CONTAINER_MDEV_IMAGE"
13+
build_hook_linuxkit_container hook-containerd "HOOK_CONTAINER_CONTAINERD_IMAGE"
14+
build_hook_linuxkit_container hook-runc "HOOK_CONTAINER_RUNC_IMAGE"
15+
build_hook_linuxkit_container hook-embedded "HOOK_CONTAINER_EMBEDDED_IMAGE"
1616
}
1717

1818
function build_hook_linuxkit_container() {
1919
declare container_dir="${1}"
20-
declare -n output_var="${2}" # bash name reference, kind of an output var but weird
20+
declare template_var="${2}" # bash name reference, kind of an output var but weird
2121
declare container_base_dir="images"
2222

2323
# Lets hash the contents of the directory and use that as a tag
@@ -28,8 +28,7 @@ function build_hook_linuxkit_container() {
2828

2929
declare container_oci_ref="${HOOK_LK_CONTAINERS_OCI_BASE}${container_dir}:${container_files_hash_short}-${DOCKER_ARCH}"
3030
log info "Consider building LK container ${container_oci_ref} from ${container_base_dir}/${container_dir} for platform ${DOCKER_ARCH}"
31-
output_var="${container_oci_ref}" # the the name reference
32-
echo "${output_var}" > /dev/null # no-op; just to avoid shellcheck SC2034 (unused var; but it is actually a bash nameref)
31+
hook_template_vars["${template_var}"]="${container_oci_ref}" # set the template var for envsubst
3332

3433
# If the image is in the local docker cache, skip building
3534
log debug "Checking if image ${container_oci_ref} exists in local registry"

bash/linuxkit.sh

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,37 @@ function linuxkit_build() {
4949
fi
5050
fi
5151

52+
# A dictionary (bash associative array) with variables and their values, for templating using envsubst.
53+
declare -A -g hook_template_vars=(
54+
["HOOK_VERSION"]="${HOOK_VERSION}"
55+
["HOOK_KERNEL_IMAGE"]="${kernel_oci_image}"
56+
["HOOK_KERNEL_ID"]="${inventory_id}"
57+
["HOOK_KERNEL_VERSION"]="${kernel_oci_version}"
58+
)
59+
5260
# Build the containers in this repo used in the LinuxKit YAML;
53-
build_all_hook_linuxkit_containers # sets HOOK_CONTAINER_BOOTKIT_IMAGE, HOOK_CONTAINER_DOCKER_IMAGE, HOOK_CONTAINER_MDEV_IMAGE, HOOK_CONTAINER_CONTAINERD_IMAGE
61+
build_all_hook_linuxkit_containers # sets HOOK_CONTAINER_BOOTKIT_IMAGE, HOOK_CONTAINER_DOCKER_IMAGE and others in the hook_template_vars dict
5462

5563
# Template the linuxkit configuration file.
5664
# - You'd think linuxkit would take --build-args or something by now, but no.
5765
# - Linuxkit does have @pkg but that's only useful in its own repo (with pkgs/ dir)
5866
# - envsubst doesn't offer a good way to escape $ in the input, so we pass the exact list of vars to consider, so escaping is not needed
59-
6067
log info "Using Linuxkit template '${kernel_info['TEMPLATE']}'..."
6168

62-
# HOOK_VERSION is read-only & already exported so is not listed in the env vars here, but is included in the dollar-sign list for envsubst to process
63-
# shellcheck disable=SC2002 # Again, no, I love my cat, leave me alone
64-
# shellcheck disable=SC2016 # I'm using single quotes to avoid shell expansion, envsubst wants the dollar signs.
65-
cat "linuxkit-templates/${kernel_info['TEMPLATE']}.template.yaml" |
66-
HOOK_KERNEL_IMAGE="${kernel_oci_image}" HOOK_KERNEL_ID="${inventory_id}" HOOK_KERNEL_VERSION="${kernel_oci_version}" \
67-
HOOK_CONTAINER_BOOTKIT_IMAGE="${HOOK_CONTAINER_BOOTKIT_IMAGE}" \
68-
HOOK_CONTAINER_DOCKER_IMAGE="${HOOK_CONTAINER_DOCKER_IMAGE}" \
69-
HOOK_CONTAINER_MDEV_IMAGE="${HOOK_CONTAINER_MDEV_IMAGE}" \
70-
HOOK_CONTAINER_CONTAINERD_IMAGE="${HOOK_CONTAINER_CONTAINERD_IMAGE}" \
71-
HOOK_CONTAINER_RUNC_IMAGE="${HOOK_CONTAINER_RUNC_IMAGE}" \
72-
HOOK_CONTAINER_EMBEDDED_IMAGE="${HOOK_CONTAINER_EMBEDDED_IMAGE}" \
73-
envsubst '$HOOK_VERSION $HOOK_KERNEL_IMAGE $HOOK_KERNEL_ID $HOOK_KERNEL_VERSION $HOOK_CONTAINER_BOOTKIT_IMAGE $HOOK_CONTAINER_DOCKER_IMAGE $HOOK_CONTAINER_MDEV_IMAGE $HOOK_CONTAINER_CONTAINERD_IMAGE $HOOK_CONTAINER_RUNC_IMAGE $HOOK_CONTAINER_EMBEDDED_IMAGE' \
74-
> "hook.${inventory_id}.yaml"
69+
# Calculate, from hook_template_vars dictionary:
70+
# envsubst_arg_string: a space separated list of dollar-prefixed variables name to be substituted
71+
# envusbst_env: the environment variables to be passed to envsubst (array of KEY=var) to be used via 'env'
72+
declare envsubst_arg_string=""
73+
declare -a envsubst_envs=()
74+
for key in "${!hook_template_vars[@]}"; do
75+
envsubst_arg_string+="\$${key} " # extra space at the end doesn't hurt
76+
envsubst_envs+=("${key}=${hook_template_vars["${key}"]}")
77+
done
78+
log debug "envsubst_arg_string: ${envsubst_arg_string}"
79+
log debug "envsubst_envs: ${envsubst_envs[*]}"
80+
81+
# Run envsubst on the template file, output to a new file; pass the envs and the arg string
82+
env "${envsubst_envs[@]}" envsubst "${envsubst_arg_string}" < "linuxkit-templates/${kernel_info['TEMPLATE']}.template.yaml" > "hook.${inventory_id}.yaml"
7583

7684
declare -g linuxkit_bin=""
7785
obtain_linuxkit_binary_cached # sets "${linuxkit_bin}"
@@ -101,7 +109,7 @@ function linuxkit_build() {
101109
"${linuxkit_bin}" build "${lk_iso_args[@]}"
102110
return 0
103111
fi
104-
112+
105113
declare -a lk_args=(
106114
"--docker"
107115
"--arch" "${kernel_info['DOCKER_ARCH']}"

0 commit comments

Comments
 (0)