Skip to content

Commit

Permalink
use templates
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Sep 19, 2023
1 parent ab42fea commit ef55aff
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 50 deletions.
39 changes: 8 additions & 31 deletions oci/private/pull.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ copy_to_directory(
"index.json",
],
)
"""

def _find_platform_manifest(image_mf, platform_wanted):
Expand Down Expand Up @@ -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,
Expand Down
41 changes: 40 additions & 1 deletion oci/private/util.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
35 changes: 17 additions & 18 deletions oci/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,41 @@ 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
[
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()
]

[
Expand All @@ -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
Expand All @@ -68,4 +67,4 @@ build_test(
],
)

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

0 comments on commit ef55aff

Please sign in to comment.