Skip to content

Commit

Permalink
tarball uses hermetic coreutils
Browse files Browse the repository at this point in the history
  • Loading branch information
rickvanprim committed Jun 25, 2024
1 parent 0d90696 commit 21ba7de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
16 changes: 11 additions & 5 deletions oci/private/tarball.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ attrs = {
used to load the image into the local engine when using `bazel run` on this oci_tarball.
By default, we look for `docker` or `podman` on the PATH, and run the `load` command.
> Note that rules_docker has an "incremental loader" which is faster than oci_tarball by design.
> Something similar can be done for oci_tarball.
> Something similar can be done for oci_tarball.
> See [loader.sh](/examples/incremental_loader/loader.sh) and explanation about [how](/examples/incremental_loader/README.md) it works.
See the _run_template attribute for the script that calls this loader tool.
Expand All @@ -87,9 +87,9 @@ attrs = {
default = Label("//oci/private:tarball_run.sh.tpl"),
doc = """ \
The template used to load the container when using `bazel run` on this oci_tarball.
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 @@ -100,6 +100,7 @@ attrs = {

def _tarball_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 @@ -113,6 +114,7 @@ def _tarball_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 @@ -138,7 +140,10 @@ def _tarball_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 @@ -198,6 +203,7 @@ oci_tarball = 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 21ba7de

Please sign in to comment.