Skip to content

Commit 3d033ce

Browse files
committed
Bazel rule: detect "oci_layout" and "oci_tarball" output groups
With rules_oci, the output of "oci_image" and "oci_image_index" is always a tree artifact containing the full oci layout. rules_img is an upcoming Bazel ruleset for building container images that tries to avoid unnecessary work. To that end, rules_img will only materialize a full oci layout tree artifact if a downstream action needs it. This is done through the use of two special output groups: - oci_layout contains a tree artifact of the full oci layout - oci_tarball contains is a tar file containing the oci layout With this change, container_structure_test is compatible with both rulesets. Users can continue to pass a single file or can choose to use the aforementioned output groups. This results in a better user experience for rules_img users who otherwise need to create a special intermediate filegroup to convert from OutputGroupInfo to DefaultInfo.
1 parent 8935313 commit 3d033ce

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

bazel/container_structure_test.bzl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ load("@aspect_bazel_lib//lib:windows_utils.bzl", "create_windows_native_launcher
55

66
_attrs = {
77
"image": attr.label(
8-
allow_single_file = True,
8+
allow_files = True,
99
doc = "Label of an oci_image or oci_tarball target.",
1010
),
1111
"configs": attr.label_list(allow_files = True, mandatory = True),
@@ -48,7 +48,25 @@ def _structure_test_impl(ctx):
4848
test_bin = ctx.toolchains["@container_structure_test//bazel:structure_test_toolchain_type"].st_info.binary
4949
jq_bin = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].jqinfo.bin
5050

51-
image_path = to_rlocation_path(ctx, ctx.file.image)
51+
default_info = ctx.attr.image[DefaultInfo] if DefaultInfo in ctx.attr.image else None
52+
output_group_info = ctx.attr.image[OutputGroupInfo] if OutputGroupInfo in ctx.attr.image else None
53+
if output_group_info and "oci_layout" in output_group_info:
54+
image_files = output_group_info["oci_layout"].to_list()
55+
if len(image_files) != 1:
56+
fail("the 'image' attribute contains the 'oci_layout' output group but it does not have exactly one file")
57+
image = image_files[0]
58+
elif output_group_info and "oci_tarball" in output_group_info:
59+
image_files = output_group_info["oci_tarball"].to_list()
60+
if len(image_files) != 1:
61+
fail("the 'image' attribute contains the 'oci_tarball' output group but it does not have exactly one file")
62+
image = image_files[0]
63+
elif default_info and len(default_info.files.to_list()) == 1:
64+
image_files = default_info.files.to_list()
65+
image = image_files[0]
66+
else:
67+
fail("the 'image' attribute must be a target that produces exactly one file or contain either the 'oci_layout' or 'oci_tarball' output groups")
68+
69+
image_path = to_rlocation_path(ctx, image)
5270

5371
# Prefer to use a tarball if we are given one, as it works with more 'driver' types.
5472
if image_path.endswith(".tar"):
@@ -81,7 +99,7 @@ def _structure_test_impl(ctx):
8199
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
82100
launcher = create_windows_native_launcher_script(ctx, bash_launcher) if is_windows else bash_launcher
83101
runfiles = ctx.runfiles(
84-
files = ctx.files.image + ctx.files.configs + [
102+
files = image_files + ctx.files.configs + [
85103
bash_launcher,
86104
test_bin,
87105
jq_bin,

0 commit comments

Comments
 (0)