From 2ceef28c0c19b7a8ed913298aa167e28b7a2af8b Mon Sep 17 00:00:00 2001 From: thesayyn Date: Tue, 19 Sep 2023 14:38:04 -0700 Subject: [PATCH] refactor: remove templated json generation --- oci/private/pull.bzl | 79 +++++++++++++++---------------------------- oci/tests/BUILD.bazel | 10 ++++-- 2 files changed, 36 insertions(+), 53 deletions(-) diff --git a/oci/private/pull.bzl b/oci/private/pull.bzl index bbfda2ca..908a64cb 100644 --- a/oci/private/pull.bzl +++ b/oci/private/pull.bzl @@ -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", @@ -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, )) diff --git a/oci/tests/BUILD.bazel b/oci/tests/BUILD.bazel index 882e6d3a..ed4fd146 100644 --- a/oci/tests/BUILD.bazel +++ b/oci/tests/BUILD.bazel @@ -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, ), @@ -29,6 +34,7 @@ IMAGES_TO_TEST = { tags = ["requires-network"], toolchains = [ "@oci_crane_toolchains//:current_toolchain", + "@jq_toolchains//:resolved_toolchain" ], visibility = ["//visibility:public"], ) @@ -62,4 +68,4 @@ build_test( ], ) -parse_image_test(name = "parse_image_test") +parse_image_test(name = "parse_image_test") \ No newline at end of file