generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #212
- Loading branch information
Showing
7 changed files
with
179 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,57 @@ | ||
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"), | ||
) | ||
|
||
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", | ||
}), | ||
) | ||
|
||
container_structure_test( | ||
name = "test", | ||
configs = ["test.yaml"], | ||
image = ":platform_image", | ||
) | ||
|
||
# $ 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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# See https://github.com/GoogleContainerTools/container-structure-test#command-tests | ||
schemaVersion: 2.0.0 | ||
metadataTest: | ||
entrypoint: ["/examples/py_binary/say_hello"] | ||
commandTests: | ||
- name: run | ||
command: /examples/py_binary/say_hello | ||
expectedOutput: ["hello py_binary!"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"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): | ||
"""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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters