Skip to content

Commit

Permalink
refactor: remove templated json generation
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Sep 19, 2023
1 parent bb7e264 commit 2ceef28
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 53 deletions.
79 changes: 28 additions & 51 deletions oci/private/pull.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -326,23 +326,6 @@ load("@bazel_skylib//rules:write_file.bzl", "write_file")
package(default_visibility = ["//visibility:public"])
# Mimic the output of crane pull [image] layout --format=oci
write_file(
name = "write_layout",
out = "oci-layout",
content = [
"{{",
" \\"imageLayoutVersion\\": \\"1.0.0\\"",
"}}",
],
)
write_file(
name = "write_index",
out = "index.json",
content = [\"\"\"{index_content}\"\"\"],
)
copy_file(
name = "manifest",
src = "manifest.json",
Expand Down Expand Up @@ -425,45 +408,39 @@ def _oci_pull_impl(rctx):
if hash not in tars:
tars.append(hash)

# To make testing against `crane pull` simple, we take care to produce a byte-for-byte-identical
# index.json file, which means we can't use jq (it produces a trailing newline) or starlark
# json.encode_indent (it re-orders keys in the dictionary).

# create index.json manifest entry
index_json_manifest = {
"mediaType": image_mf["mediaType"],
"size": image_mf_len,
"digest": image_digest
}

if rctx.attr.platform:
os, arch = rctx.attr.platform.split("/", 1)
index_mf = """\
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "%s",
"size": %s,
"digest": "%s",
"platform": {
"architecture": "%s",
"os": "%s"
}
}
]
}""" % (image_mf["mediaType"], image_mf_len, image_digest, arch, os)
else:
index_mf = """\
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "%s",
"size": %s,
"digest": "%s"
}
]
}""" % (image_mf["mediaType"], image_mf_len, image_digest)
platform_parts = rctx.attr.platform.split("/", 2)
index_json_manifest["platform"] = {
"os": platform_parts[0],
"architecture": platform_parts[1],
}
# add variant if provided
if len(platform_parts) == 3:
index_json_manifest["platform"]["variant"] = platform_parts[1]

# create index.json
index_json = {
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [index_json_manifest],
}
oci_layout = {
"imageLayoutVersion": "1.0.0"
}

rctx.file("index.json", json.encode_indent(index_json, indent = " "))
rctx.file("oci-layout", json.encode_indent(oci_layout, indent = " "))
rctx.file("BUILD.bazel", content = _build_file.format(
target_name = rctx.attr.target_name,
tars = tars,
index_content = index_mf,
image_digest = _trim_hash_algorithm(image_digest),
config_file = image_config_file,
))
Expand Down
10 changes: 8 additions & 2 deletions oci/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ IMAGES_TO_TEST = {
genrule(
name = "pull_{}".format(repo_name),
outs = [repo_name],
cmd = "$(CRANE_BIN) pull {reference} $@ --format=oci --platform={platform}".format(
cmd = """
$(CRANE_BIN) pull {reference} $@ --format=oci --platform={platform}
cat "$@/index.json" | $(JQ_BIN) --sort-keys -j '.' > "$@/index-tmp.json"
rm "$@/index.json"
mv "$@/index-tmp.json" "$@/index.json"
""".format(
platform = _PLATFORM,
reference = reference,
),
Expand All @@ -29,6 +34,7 @@ IMAGES_TO_TEST = {
tags = ["requires-network"],
toolchains = [
"@oci_crane_toolchains//:current_toolchain",
"@jq_toolchains//:resolved_toolchain"
],
visibility = ["//visibility:public"],
)
Expand Down Expand Up @@ -62,4 +68,4 @@ build_test(
],
)

parse_image_test(name = "parse_image_test")
parse_image_test(name = "parse_image_test")

0 comments on commit 2ceef28

Please sign in to comment.