Skip to content

Commit

Permalink
feat: add custom no_match_error to oci_alias
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Sep 18, 2023
1 parent 2dc52de commit 83320d6
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions oci/private/pull.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ _MULTI_PLATFORM_IMAGE_ALIAS_TMPL = """\
alias(
name = "{target_name}",
actual = select(
{platform_map}
{platform_map},
no_match_error = \"\"\"could not find an image matching the target platform. \\navailable platforms are {available_platforms} \"\"\",
),
visibility = ["//visibility:public"],
)
Expand All @@ -493,22 +494,25 @@ def _oci_alias_impl(rctx):

downloader = _create_downloader(rctx)

available_platforms = []

manifest, _ = downloader.download_manifest(rctx.attr.identifier, "mf.json")

if manifest["mediaType"] in _SUPPORTED_MEDIA_TYPES["index"]:
for submanifest in manifest["manifests"]:
parts = [submanifest["platform"]["os"], submanifest["platform"]["architecture"]]
if "variant" in submanifest["platform"]:
parts.append(submanifest["platform"]["variant"])
available_platforms.append('"{}"'.format("/".join(parts)))

if _is_tag(rctx.attr.identifier) and rctx.attr.reproducible:
manifest, _ = downloader.download_manifest(rctx.attr.identifier, "mf.json")
is_bzlmod = hasattr(rctx.attr, "bzlmod_repository") and rctx.attr.bzlmod_repository
digest = util.sha256(rctx, "mf.json")

optional_platforms = ""

if manifest["mediaType"] in _SUPPORTED_MEDIA_TYPES["index"]:
platforms = []
for submanifest in manifest["manifests"]:
parts = [submanifest["platform"]["os"], submanifest["platform"]["architecture"]]
if "variant" in submanifest["platform"]:
parts.append(submanifest["platform"]["variant"])
platforms.append('"{}"'.format("/".join(parts)))
optional_platforms = "'add platforms {}'".format(" ".join(platforms))
if len(available_platforms):
optional_platforms = "'add platforms {}'".format(" ".join(available_platforms))

is_bzlmod = hasattr(rctx.attr, "bzlmod_repository") and rctx.attr.bzlmod_repository
util.warning(rctx, """\
for reproducible builds, a digest is recommended.
Either set 'reproducible = False' to silence this warning,
Expand All @@ -529,6 +533,7 @@ buildozer 'set digest "sha256:{digest}"' 'remove tag' 'remove platforms' {option
build = _MULTI_PLATFORM_IMAGE_ALIAS_TMPL.format(
name = rctx.attr.name,
target_name = rctx.attr.target_name,
available_platforms = ", ".join(available_platforms),
platform_map = {
str(k): v
for k, v in rctx.attr.platforms.items()
Expand Down

0 comments on commit 83320d6

Please sign in to comment.