Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Output the image id of an oci_load target. #715

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions oci/private/image_id.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# Read the configuration json sha256 digest from the manifest.json file passed
# as the first positional argument. Output that sha256 to the
# file provided as the second positional argument.
set -o pipefail -o errexit -o nounset

readonly JQ="{{jq_path}}"

CONFIG_PATH=$("$JQ" -r '.[0].Config' "$1")
# CONFIG_PATH will be blobs/sha256/<digest>
SHA256_OF_CONFIG="${CONFIG_PATH#blobs/sha256/}"

if [[ "$SHA256_OF_CONFIG" == "$CONFIG_PATH" ]]; then
echo "Error: Failed to extract SHA256 digest from CONFIG_PATH" >&2
exit 1
fi

echo "$SHA256_OF_CONFIG" > "$2"
29 changes: 29 additions & 0 deletions oci/private/load.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ attrs = {
allow_single_file = True,
),
"_tarball_sh": attr.label(allow_single_file = True, default = "//oci/private:tarball.sh.tpl"),
"_image_id_sh": attr.label(allow_single_file = True, default = "//oci/private:image_id.sh.tpl"),
"_runfiles": attr.label(default = "@bazel_tools//tools/bash/runfiles"),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}
Expand All @@ -143,6 +144,7 @@ def _load_impl(ctx):

mtree_spec = ctx.actions.declare_file("{}/tarball.spec".format(ctx.label.name))
executable = ctx.actions.declare_file("{}/load.sh".format(ctx.label.name))
image_id_sh = ctx.actions.declare_file("{}/image_id.sh".format(ctx.label.name))
manifest_json = ctx.actions.declare_file("{}/manifest.json".format(ctx.label.name))

# Represents either manifest.json or index.json depending on the image format
Expand Down Expand Up @@ -181,6 +183,32 @@ def _load_impl(ctx):
],
mnemonic = "OCITarballManifest",
)
ctx.actions.expand_template(
template = ctx.file._image_id_sh,
output = image_id_sh,
is_executable = True,
substitutions = {
"{{jq_path}}": jq.jqinfo.bin.path,
},
)
image_id = ctx.actions.declare_file("{}/image_id.txt".format(ctx.label.name))
image_id_args = ctx.actions.args()
image_id_args.add(manifest_json)
image_id_args.add(image_id)
ctx.actions.run(
executable = image_id_sh,
inputs = [manifest_json],
outputs = [image_id],
arguments = [image_id_args],
tools = [
jq.jqinfo.bin,
],
env = {
"MANIFEST_JSON_PATH": manifest_json.path,
"OUTPUT_PATH": image_id.path,
},
mnemonic = "ImageId",
)

# This action produces a large output and should rarely be used as it puts load on the cache.
# It will only run if the "tarball" output_group is explicitly requested
Expand Down Expand Up @@ -228,6 +256,7 @@ def _load_impl(ctx):
DefaultInfo(
runfiles = runfiles,
executable = runnable_loader,
files = depset([image_id]),
),
OutputGroupInfo(tarball = depset([tarball])),
]
Expand Down
1 change: 1 addition & 0 deletions oci/private/load.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ runfiles_export_envvars
readonly TAR="$(rlocation "{{tar}}")"
readonly MTREE="$(rlocation "{{mtree_path}}")"
readonly LOADER="$(rlocation "{{loader}}")"
readonly JQ="$(rlocation "{{jq_path}}")"

if [ -f "$LOADER" ]; then
CONTAINER_CLI="$LOADER"
Expand Down
Loading