-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: py_image_layers #349
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,51 @@ | ||
load("//py:defs.bzl", "py_binary") | ||
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") | ||
load("//py:defs.bzl", "py_binary", "py_image_layers") | ||
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball") | ||
|
||
py_binary( | ||
name = "py_binary", | ||
name = "say_hello", | ||
srcs = ["say.py"], | ||
deps = [ | ||
"@pypi_cowsay//:pkg", | ||
], | ||
) | ||
|
||
oci_image( | ||
name = "say_image", | ||
base = "@ubuntu", | ||
entrypoint = ["/examples/py_binary/say_hello"], | ||
tars = py_image_layers("say_image_layers", "say_hello"), | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this instead produce a |
||
|
||
platform( | ||
name = "aarch64_linux", | ||
constraint_values = [ | ||
"@platforms//os:linux", | ||
"@platforms//cpu:aarch64", | ||
], | ||
) | ||
|
||
platform( | ||
name = "x86_64_linux", | ||
constraint_values = [ | ||
"@platforms//os:linux", | ||
"@platforms//cpu:x86_64", | ||
], | ||
) | ||
|
||
platform_transition_filegroup( | ||
name = "platform_image", | ||
srcs = [":say_image"], | ||
target_platform = select({ | ||
"@platforms//cpu:arm64": ":aarch64_linux", | ||
"@platforms//cpu:x86_64": ":x86_64_linux", | ||
}), | ||
) | ||
|
||
# $ bazel run //examples/py_binary:load | ||
# $ docker run --rm gcr.io/oci_python_hello_world:latest | ||
oci_tarball( | ||
name = "load", | ||
image = ":platform_image", | ||
repo_tags = ["bazel/say:latest"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
"Helper function to make three separate layers for python applications" | ||
|
||
load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar") | ||
|
||
# match *only* external repositories that have the string "python" | ||
# e.g. this will match | ||
# `/hello_world/hello_world_bin.runfiles/rules_python~0.21.0~python~python3_9_aarch64-unknown-linux-gnu/bin/python3` | ||
# but not match | ||
# `/hello_world/hello_world_bin.runfiles/_main/python_app` | ||
PY_INTERPRETER_REGEX = "\\.runfiles/.*python.*-.*" | ||
|
||
# match *only* external pip like repositories that contain the string "site-packages" | ||
SITE_PACKAGES_REGEX = "\\.runfiles/.*/site-packages/.*" | ||
|
||
def py_image_layers(name, binary, interpreter_regex = PY_INTERPRETER_REGEX, site_packages_regex = SITE_PACKAGES_REGEX): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"""Create three layers for a py_binary target: interpreter, third-party packages, and application code. | ||
|
||
This allows a container image to have smaller uploads, since the application layer usually changes more | ||
than the other two. | ||
|
||
> [!NOTE] | ||
> The middle layer may duplicate other py_image_layers which have a disjoint set of dependencies. | ||
> Follow https://github.com/aspect-build/rules_py/issues/244 | ||
|
||
Args: | ||
name: prefix for generated targets, to ensure they are unique within the package | ||
binary: a py_binary target | ||
interpreter_regex: a regular expression for use by `grep` which extracts the interpreter and related files from the binary runfiles tree | ||
site_packages_regex: a regular expression for use by `grep` which extracts installed packages from the binary runfiles tree | ||
|
||
Returns: | ||
a list of labels for the layers, which are tar files | ||
""" | ||
|
||
# Produce layers in this order, as the app changes most often | ||
layers = ["interpreter", "packages", "app"] | ||
|
||
# Produce the manifest for a tar file of our py_binary, but don't tar it up yet, so we can split | ||
# into fine-grained layers for better docker performance. | ||
mtree_spec( | ||
name = name + ".mf", | ||
srcs = [binary], | ||
) | ||
|
||
native.genrule( | ||
name = name + ".interpreter_tar_manifest", | ||
srcs = [name + ".mf"], | ||
outs = [name + ".interpreter_tar_manifest.spec"], | ||
cmd = "grep '{}' $< >$@".format(PY_INTERPRETER_REGEX), | ||
) | ||
|
||
native.genrule( | ||
name = name + ".packages_tar_manifest", | ||
srcs = [name + ".mf"], | ||
outs = [name + ".packages_tar_manifest.spec"], | ||
cmd = "grep '{}' $< >$@".format(SITE_PACKAGES_REGEX), | ||
) | ||
|
||
# Any lines that didn't match one of the two grep above | ||
native.genrule( | ||
name = name + ".app_tar_manifest", | ||
srcs = [name + ".mf"], | ||
outs = [name + ".app_tar_manifest.spec"], | ||
cmd = "grep -v '{}' $< | grep -v '{}' >$@".format(SITE_PACKAGES_REGEX, PY_INTERPRETER_REGEX), | ||
) | ||
|
||
result = [] | ||
for layer in layers: | ||
layer_target = "{}.{}_layer".format(name, layer) | ||
result.append(layer_target) | ||
tar( | ||
name = layer_target, | ||
srcs = [binary], | ||
mtree = "{}.{}_tar_manifest".format(name, layer), | ||
) | ||
|
||
return result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users will also need this toolchain? Is the expectation that they register too, or should this ruleset be doing that in the setup