From ef55aff12dfedf48acf85cad7b6c2798a130944c Mon Sep 17 00:00:00 2001 From: thesayyn Date: Tue, 19 Sep 2023 16:36:17 -0700 Subject: [PATCH] use templates --- oci/private/pull.bzl | 39 ++++++++------------------------------- oci/private/util.bzl | 41 ++++++++++++++++++++++++++++++++++++++++- oci/tests/BUILD.bazel | 35 +++++++++++++++++------------------ 3 files changed, 65 insertions(+), 50 deletions(-) diff --git a/oci/private/pull.bzl b/oci/private/pull.bzl index 82bfeeec..347008dd 100644 --- a/oci/private/pull.bzl +++ b/oci/private/pull.bzl @@ -356,7 +356,6 @@ copy_to_directory( "index.json", ], ) - """ def _find_platform_manifest(image_mf, platform_wanted): @@ -410,36 +409,14 @@ def _oci_pull_impl(rctx): if hash not in tars: tars.append(hash) - - # create index.json manifest entry - index_json_manifest = { - "mediaType": image_mf["mediaType"], - "size": image_mf_len, - "digest": image_digest - } - - if rctx.attr.platform: - 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("index.json", util.build_manifest_json( + media_type = image_mf["mediaType"], + size = image_mf_len, + digest = image_digest, + platform = rctx.attr.platform + )) + rctx.file("oci-layout", json.encode_indent({"imageLayoutVersion": "1.0.0"}, indent = " ")) + rctx.file("BUILD.bazel", content = _build_file.format( target_name = rctx.attr.target_name, tars = tars, diff --git a/oci/private/util.bzl b/oci/private/util.bzl index 0662c30f..f3d9f675 100644 --- a/oci/private/util.bzl +++ b/oci/private/util.bzl @@ -118,10 +118,49 @@ def _file_exists(rctx, path): return result.return_code == 0 +_INDEX_JSON_TMPL="""\ +{{ + "schemaVersion": 2, + "mediaType": "application/vnd.oci.image.index.v1+json", + "manifests": [ + {{ + "mediaType": "{}", + "size": {}, + "digest": "{}"{optional_platform} + }} + ] +}}""" + +def _build_manifest_json(media_type, size, digest, platform): + + optional_platform = "" + + if platform: + platform_parts = platform.split("/", 3) + + optional_variant = "" + if len(platform_parts) == 3: + optional_variant = ''', + "variant": "{}"'''.format(platform_parts[2]) + + optional_platform = """, + "platform": {{ + "architecture": "{}", + "os": "{}"{optional_variant} + }}""".format(platform_parts[1], platform_parts[0], optional_variant = optional_variant) + + return _INDEX_JSON_TMPL.format( + media_type, + size, + digest, + optional_platform = optional_platform + ) + util = struct( parse_image = _parse_image, sha256 = _sha256, warning = _warning, maybe_wrap_launcher_for_windows = _maybe_wrap_launcher_for_windows, - file_exists = _file_exists + file_exists = _file_exists, + build_manifest_json = _build_manifest_json ) diff --git a/oci/tests/BUILD.bazel b/oci/tests/BUILD.bazel index ed4fd146..00e0ba7d 100644 --- a/oci/tests/BUILD.bazel +++ b/oci/tests/BUILD.bazel @@ -2,13 +2,16 @@ load("@aspect_bazel_lib//lib:diff_test.bzl", "diff_test") load("@bazel_skylib//rules:build_test.bzl", "build_test") load(":pull_tests.bzl", "parse_image_test") -_PLATFORM = "linux/amd64" - IMAGES_TO_TEST = { - "distroless_java": "gcr.io/distroless/java17@sha256:161a1d97d592b3f1919801578c3a47c8e932071168a96267698f4b669c24c76d", - "distroless_static_linux_amd64": "gcr.io/distroless/static@sha256:c3c3d0230d487c0ad3a0d87ad03ee02ea2ff0b3dcce91ca06a1019e07de05f12", - "fluxcd_flux_single": "docker.io/fluxcd/flux:1.25.4", - "chainguard_static_linux_amd64": "cgr.dev/chainguard/static:latest", + "linux/amd64": { + "distroless_java": "gcr.io/distroless/java17@sha256:161a1d97d592b3f1919801578c3a47c8e932071168a96267698f4b669c24c76d", + "distroless_static_linux_amd64": "gcr.io/distroless/static@sha256:c3c3d0230d487c0ad3a0d87ad03ee02ea2ff0b3dcce91ca06a1019e07de05f12", + "fluxcd_flux_single": "docker.io/fluxcd/flux:1.25.4", + "chainguard_static_linux_amd64": "cgr.dev/chainguard/static:latest", + }, + "linux/arm64/v8": { + "ubuntu_linux_arm64_v8": "ubuntu@sha256:67211c14fa74f070d27cc59d69a7fa9aeff8e28ea118ef3babc295a0428a6d21" + } } # Use crane to pull images as a comparison for our oci_pull repository rule @@ -16,29 +19,24 @@ IMAGES_TO_TEST = { genrule( name = "pull_{}".format(repo_name), outs = [repo_name], - 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, + cmd = "$(CRANE_BIN) pull {reference} $@ --format=oci --platform={platform}".format( + platform = platform, reference = reference, ), local = True, # needs to run locally to able to use credential helpers message = "Pulling {reference} for {platform}".format( - platform = _PLATFORM, + platform = platform, reference = reference, ), output_to_bindir = True, tags = ["requires-network"], toolchains = [ "@oci_crane_toolchains//:current_toolchain", - "@jq_toolchains//:resolved_toolchain" ], visibility = ["//visibility:public"], ) - for repo_name, reference in IMAGES_TO_TEST.items() + for platform in IMAGES_TO_TEST.keys() + for repo_name, reference in IMAGES_TO_TEST[platform].items() ] [ @@ -49,7 +47,8 @@ mv "$@/index-tmp.json" "$@/index.json" repo_name, ), ) - for repo_name, reference in IMAGES_TO_TEST.items() + for platform in IMAGES_TO_TEST.keys() + for repo_name, reference in IMAGES_TO_TEST[platform].items() ] # assert than we don't break fetching these @@ -68,4 +67,4 @@ build_test( ], ) -parse_image_test(name = "parse_image_test") \ No newline at end of file +parse_image_test(name = "parse_image_test")