Skip to content

Commit

Permalink
feat: add more convenience targets
Browse files Browse the repository at this point in the history
Add platform-aware :data and :control convenience targets at the top of
the package repo so that we can use `@<REPO>//<PACKAGE>:data` directly
(see changes to examples/debian_snapshot/BUILD).

Also, add :deps to complement :data.
  • Loading branch information
jjmaestro committed Dec 2, 2024
1 parent e867017 commit c6c71a1
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 47 deletions.
114 changes: 78 additions & 36 deletions apt/private/deb_translate_lock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ _DEB_IMPORT_TMPL = '''\
)
'''

_PACKAGE_TEMPLATE = """\
alias(
name = "{target_name}",
actual = select({data_targets}),
visibility = ["//visibility:public"],
)
alias(
name = "control",
actual = select({control_targets}),
visibility = ["//visibility:public"],
)
"""

_ROOT_BUILD_TMPL = """\
"Generated by rules_distroless. DO NOT EDIT."
Expand Down Expand Up @@ -107,6 +93,76 @@ filegroup(
)
"""

def _package_build(template, package, repo_name):
alias_data = '"@%s//:data"' % repo_name
alias_control = '"@%s//:control"' % repo_name

deps = [
"//%s/%s" % (dep["name"], package["arch"])
for dep in package["dependencies"]
]
deps = starlark_codegen_utils.to_list_attr(deps)

return template.format(
target_name = package["arch"],
src = "%s//:data" % repo_name,
deps = deps,
urls = [package["url"]],
name = package["name"],
arch = package["arch"],
sha256 = package["sha256"],
repo_name = repo_name,
alias_data = alias_data,
alias_control = alias_control,
)

def _target(package, arch, target_name):
return "//%s/%s:%s" % (package["name"], arch, target_name)

def _gen_alias(target_name, package, architectures):
if len(architectures) == 1 and architectures[0] == "all":
actual = '"%s"' % _target(package, "all", target_name)
else:
select = {
"//:linux_%s" % arch: _target(package, package["arch"], target_name)
for arch in architectures
}
actual = "select(%s)" % starlark_codegen_utils.to_dict_attr(select)

return actual

def _gen_filegroup(target_name, package, architectures):
if len(architectures) == 1 and architectures[0] == "all":
src = [_target(package, "all", target_name)]
src = starlark_codegen_utils.to_list_attr(src)
elif architectures:
src = {
"//:linux_%s" % arch: [_target(package, package["arch"], target_name)]
for arch in architectures
}
src = "select(%s)" % starlark_codegen_utils.to_dict_list_attr(src)
else:
src = "[]"

return src

def _package_aliases_build(template, package, architectures):
alias_data = _gen_alias("data", package, architectures)
alias_control = _gen_alias("control", package, architectures)

deps = _gen_filegroup("deps", package, architectures)

return template.format(
target_name = package["name"],
deps = deps,
urls = [package["url"]],
name = package["name"],
arch = package["arch"],
sha256 = package["sha256"],
alias_data = alias_data,
alias_control = alias_control,
)

def _deb_translate_lock_impl(rctx):
lock_content = rctx.attr.lock_content
package_template = rctx.read(rctx.attr.package_template)
Expand Down Expand Up @@ -148,35 +204,21 @@ def _deb_translate_lock_impl(rctx):

rctx.file(
"%s/%s/BUILD.bazel" % (package["name"], package["arch"]),
package_template.format(
target_name = package["arch"],
src = '"@%s//:data"' % repo_name,
deps = starlark_codegen_utils.to_list_attr([
"//%s/%s" % (dep["name"], package["arch"])
for dep in package["dependencies"]
]),
urls = [package["url"]],
name = package["name"],
arch = package["arch"],
sha256 = package["sha256"],
repo_name = "%s" % repo_name,
_package_build(
package_template,
package,
repo_name,
),
)

# TODO: rework lockfile to include architecure information and merge these two loops
for (package) in lockf.packages():
rctx.file(
"%s/BUILD.bazel" % (package["name"]),
_PACKAGE_TEMPLATE.format(
target_name = package["name"],
data_targets = starlark_codegen_utils.to_dict_attr({
"//:linux_%s" % arch: "//%s/%s" % (package["name"], arch)
for arch in architectures._values
}),
control_targets = starlark_codegen_utils.to_dict_attr({
"//:linux_%s" % arch: "//%s/%s:control" % (package["name"], arch)
for arch in architectures._values
}),
_package_aliases_build(
package_template,
package,
sets.to_list(architectures),
),
)

Expand Down
17 changes: 14 additions & 3 deletions apt/private/package.BUILD.tmpl
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
"""Generated by rules_distroless. DO NOT EDIT."""

alias(
name = "data",
actual = "@{repo_name}//:data",
actual = {alias_data},
visibility = ["//visibility:public"],
)

alias(
name = "control",
actual = "@{repo_name}//:control",
actual = {alias_control},
visibility = ["//visibility:public"],
)

filegroup(
name = "deps",
srcs = {deps},
visibility = ["//visibility:public"],
)

filegroup(
name = "{target_name}",
srcs = [
":deps",
":data",
],
visibility = ["//visibility:public"],
srcs = {deps} + [":data"]
)
5 changes: 1 addition & 4 deletions e2e/smoke/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ tar(

cacerts(
name = "cacerts",
package = select({
"@platforms//cpu:arm64": "@bullseye//ca-certificates/arm64:data",
"@platforms//cpu:x86_64": "@bullseye//ca-certificates/amd64:data",
}),
package = "@bullseye//ca-certificates:data",
)

oci_image(
Expand Down
5 changes: 1 addition & 4 deletions examples/debian_snapshot/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ tar(

cacerts(
name = "cacerts",
package = select({
"@platforms//cpu:arm64": "@bullseye//ca-certificates/arm64:data",
"@platforms//cpu:x86_64": "@bullseye//ca-certificates/amd64:data",
}),
package = "@bullseye//ca-certificates:data",
)

oci_image(
Expand Down

0 comments on commit c6c71a1

Please sign in to comment.