Skip to content

Commit

Permalink
Use a transition to resolve the correct versions of crane and yq
Browse files Browse the repository at this point in the history
Bazel doesn't provide a very elegant way to specify that a toolchain
should be resolved in such a way that it can be executed on a *target*
platform:

bazelbuild/bazel#19645

To work around this, we can move the toolchains into a small helper rule
named oci_push_tools(). We can use an outgoing edge transition on the
oci_push() side to set --extra_execution_platforms equal to --platforms,
thereby causing the toolchains to be resolved the way we want.

Though I think using transitions for this is pretty slick, it's a shame
that this has to be done by patching up command line options.
  • Loading branch information
EdSchouten committed May 23, 2024
1 parent 49fbc76 commit 1a587ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
6 changes: 6 additions & 0 deletions oci/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(":push.bzl", "oci_push_tools")

exports_files(
glob(["*.bzl"]),
Expand Down Expand Up @@ -108,3 +109,8 @@ bzl_library(
"@bazel_skylib//lib:versions",
],
)

oci_push_tools(
name = "oci_push_tools",
visibility = ["//visibility:public"],
)
33 changes: 30 additions & 3 deletions oci/private/push.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ oci_push(
```
"""

# Helper rule for ensuring that the crane and yq toolchains are actually
# resolved for the architecture we are targeting.
def _transition_to_target_impl(settings, attr):
return {
"//command_line_option:extra_execution_platforms": settings["//command_line_option:platforms"],
}

_transition_to_target = transition(
implementation = _transition_to_target_impl,
inputs = ["//command_line_option:platforms"],
outputs = ["//command_line_option:extra_execution_platforms"],
)

_attrs = {
"image": attr.label(
allow_single_file = True,
Expand Down Expand Up @@ -128,15 +141,19 @@ _attrs = {
default = "push.sh.tpl",
allow_single_file = True,
),
"_tools": attr.label(
cfg = _transition_to_target,
default = "oci_push_tools",
),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}

def _quote_args(args):
return ["\"{}\"".format(arg) for arg in args]

def _impl(ctx):
crane = ctx.toolchains["@rules_oci//oci:crane_toolchain_type"]
yq = ctx.toolchains["@aspect_bazel_lib//lib:yq_toolchain_type"]
crane = ctx.attr._tools[0].crane
yq = ctx.attr._tools[0].yq

if ctx.attr.repository and ctx.attr.repository_file:
fail("must specify exactly one of 'repository_file' or 'repository'")
Expand Down Expand Up @@ -195,6 +212,16 @@ oci_push = rule(
doc = _DOC,
implementation = oci_push_lib.implementation,
attrs = oci_push_lib.attrs,
toolchains = oci_push_lib.toolchains,
executable = True,
)

def _oci_push_tools(ctx):
return struct(
crane = ctx.toolchains["@rules_oci//oci:crane_toolchain_type"],
yq = ctx.toolchains["@aspect_bazel_lib//lib:yq_toolchain_type"],
)

oci_push_tools = rule(
implementation = _oci_push_tools,
toolchains = oci_push_lib.toolchains,
)

0 comments on commit 1a587ec

Please sign in to comment.