Skip to content

Commit

Permalink
refactor: tarball uses hermetic coreutils (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickvanprim authored Jun 26, 2024
1 parent 96062d4 commit 1979f79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
12 changes: 9 additions & 3 deletions oci/private/load.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ attrs = {
default = Label("//oci/private:load.sh.tpl"),
doc = """ \
The template used to load the container when using `bazel run` on this target.
See the `loader` attribute to replace the tool which is called.
Please reference the default template to see available substitutions.
Please reference the default template to see available substitutions.
""",
allow_single_file = True,
),
Expand All @@ -96,6 +96,7 @@ attrs = {

def _load_impl(ctx):
jq = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"]
coreutils = ctx.toolchains["@aspect_bazel_lib//lib:coreutils_toolchain_type"]
bsdtar = ctx.toolchains["@aspect_bazel_lib//lib:tar_toolchain_type"]

image = ctx.file.image
Expand All @@ -109,6 +110,7 @@ def _load_impl(ctx):
substitutions = {
"{{format}}": ctx.attr.format,
"{{jq_path}}": jq.jqinfo.bin.path,
"{{coreutils_path}}": coreutils.coreutils_info.bin.path,
"{{tar}}": bsdtar.tarinfo.binary.path,
"{{image_dir}}": image.path,
"{{output}}": mtree_spec.path,
Expand All @@ -134,7 +136,10 @@ def _load_impl(ctx):
executable = util.maybe_wrap_launcher_for_windows(ctx, executable),
inputs = mtree_inputs,
outputs = mtree_outputs,
tools = [jq.jqinfo.bin],
tools = [
jq.jqinfo.bin,
coreutils.coreutils_info.bin,
],
mnemonic = "OCITarballManifest",
)

Expand Down Expand Up @@ -194,6 +199,7 @@ oci_load = rule(
doc = doc,
toolchains = [
"@bazel_tools//tools/sh:toolchain_type",
"@aspect_bazel_lib//lib:coreutils_toolchain_type",
"@aspect_bazel_lib//lib:jq_toolchain_type",
"@aspect_bazel_lib//lib:tar_toolchain_type",
],
Expand Down
11 changes: 6 additions & 5 deletions oci/private/tarball.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ set -o pipefail -o errexit -o nounset

readonly FORMAT="{{format}}"
readonly JQ="{{jq_path}}"
readonly COREUTILS="{{coreutils_path}}"
readonly TAR="{{tar}}"
readonly IMAGE_DIR="{{image_dir}}"
readonly REPOTAGS=($(cat "{{tags}}"))
readonly REPOTAGS=($("${COREUTILS}" cat "{{tags}}"))
readonly INDEX_FILE="${IMAGE_DIR}/index.json"

readonly OUTPUT="{{output}}"
Expand All @@ -20,7 +21,7 @@ function add_to_tar() {
echo >>"${OUTPUT}" "${tar_path} uid=0 gid=0 mode=0755 time=1672560000 type=file content=${content}"
}

MANIFEST_DIGEST=$(${JQ} -r '.manifests[0].digest | sub(":"; "/")' "${INDEX_FILE}" | tr -d '"')
MANIFEST_DIGEST=$(${JQ} -r '.manifests[0].digest | sub(":"; "/")' "${INDEX_FILE}" | "${COREUTILS}" tr -d '"')

MANIFESTS_LENGTH=$("${JQ}" -r '.manifests | length' "${INDEX_FILE}")
if [[ "${MANIFESTS_LENGTH}" != 1 ]]; then
Expand Down Expand Up @@ -51,7 +52,7 @@ if [[ "${FORMAT}" == "oci" ]]; then

add_to_tar "${IMAGE_DIR}/oci-layout" oci-layout

INDEX_FILE_MANIFEST_DIGEST=$("${JQ}" -r '.manifests[0].digest | sub(":"; "/")' "${INDEX_FILE}" | tr -d '"')
INDEX_FILE_MANIFEST_DIGEST=$("${JQ}" -r '.manifests[0].digest | sub(":"; "/")' "${INDEX_FILE}" | "${COREUTILS}" tr -d '"')
INDEX_FILE_MANIFEST_BLOB_PATH="${IMAGE_DIR}/blobs/${INDEX_FILE_MANIFEST_DIGEST}"

add_to_tar "${INDEX_FILE_MANIFEST_BLOB_PATH}" "blobs/${INDEX_FILE_MANIFEST_DIGEST}"
Expand Down Expand Up @@ -83,7 +84,7 @@ if [[ "${FORMAT}" == "oci" ]]; then
exit 0
fi

MANIFEST_DIGEST=$(${JQ} -r '.manifests[0].digest | sub(":"; "/")' "${IMAGE_DIR}/index.json" | tr -d '"')
MANIFEST_DIGEST=$(${JQ} -r '.manifests[0].digest | sub(":"; "/")' "${IMAGE_DIR}/index.json" | "${COREUTILS}" tr -d '"')
MANIFEST_BLOB_PATH="${IMAGE_DIR}/blobs/${MANIFEST_DIGEST}"

CONFIG_DIGEST=$(${JQ} -r '.config.digest | sub(":"; "/")' ${MANIFEST_BLOB_PATH})
Expand All @@ -93,7 +94,7 @@ LAYERS=$(${JQ} -cr '.layers | map(.digest | sub(":"; "/"))' ${MANIFEST_BLOB_PATH

add_to_tar "${CONFIG_BLOB_PATH}" "blobs/${CONFIG_DIGEST}"

for LAYER in $(${JQ} -r ".[]" <<< $LAYERS); do
for LAYER in $(${JQ} -r ".[]" <<< $LAYERS); do
add_to_tar "${IMAGE_DIR}/blobs/${LAYER}" "blobs/${LAYER}.tar.gz"
done

Expand Down

0 comments on commit 1979f79

Please sign in to comment.