Skip to content

Commit

Permalink
Add java_layer target that allows filtering JVM dependencies (#2122)
Browse files Browse the repository at this point in the history
  • Loading branch information
ns476 authored Aug 2, 2022
1 parent 55ffa70 commit a8aff40
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ py_image(
```

You can also implement more complex fine layering strategies by using the
`py_layer` rule and its `filter` attribute. For example:
`py_layer` or `java_layer` rules and their `filter` attribute. For example:

```python
# Suppose that we are synthesizing an image that depends on a complex set
Expand Down
6 changes: 6 additions & 0 deletions java/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ load(
)
load(
"//lang:image.bzl",
"filter_layer",
"layer_file_path",
lang_image = "image",
)
Expand Down Expand Up @@ -105,6 +106,11 @@ DEFAULT_JETTY_BASE = select({
"//conditions:default": "@jetty_image_base//image",
})

def java_layer(name, deps = [], runtime_deps = [], filter = "", **kwargs):
binary_name = name + ".layer-binary"
native.java_library(name = binary_name, runtime_deps = deps + runtime_deps, **kwargs)
filter_layer(name = name, dep = binary_name, filter = filter)

def java_files(f):
"""Filter out the list of java source files from the given list of runfiles.
Expand Down
26 changes: 21 additions & 5 deletions lang/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,14 @@ def _filter_aspect_impl(target, ctx):
# then take the filtered depset instead of descending further.
return [FilterAspectInfo(depset = target[FilterLayerInfo].filtered_depset)]

# Collect transitive deps from all children (propagating along "deps" attr).
target_deps = depset(transitive = [dep[FilterAspectInfo].depset for dep in getattr(ctx.rule.attr, "deps", [])])
# Collect transitive deps from all children (propagating along "deps" and
# "runtime_deps" (for the JVM) attrs).
target_deps = depset(
transitive =
[dep[FilterAspectInfo].depset for dep in getattr(ctx.rule.attr, "deps", [])] +
[dep[FilterAspectInfo].depset for dep in getattr(ctx.rule.attr, "runtime_deps", [])],
)

myself = struct(target = target, target_deps = target_deps)
return [
FilterAspectInfo(
Expand All @@ -307,7 +313,7 @@ def _filter_aspect_impl(target, ctx):

# Aspect for collecting dependency info.
_filter_aspect = aspect(
attr_aspects = ["deps"],
attr_aspects = ["deps", "runtime_deps"],
implementation = _filter_aspect_impl,
)

Expand All @@ -316,18 +322,28 @@ def _filter_layer_rule_impl(ctx):

runfiles = ctx.runfiles()
filtered_depsets = []
java_infos = []

for dep in transitive_deps.to_list():
if str(dep.target.label).startswith(ctx.attr.filter) and str(dep.target.label) != str(ctx.attr.dep.label):
runfiles = runfiles.merge(dep.target[DefaultInfo].default_runfiles)

if JavaInfo in dep.target:
java_infos.append(dep.target[JavaInfo])

filtered_depsets.append(dep.target_deps)

# Forward legacy builtin provider and PyInfo provider
# Forward legacy builtin provider and PyInfo/JavaInfo provider
maybe_pyinfo = [ctx.attr.dep[PyInfo]] if PyInfo in ctx.attr.dep else []
maybe_javainfo = \
[java_common.merge(java_infos)] if java_infos else []

return [
FilterLayerInfo(
runfiles = runfiles,
filtered_depset = depset(transitive = filtered_depsets),
),
] + ([ctx.attr.dep[PyInfo]] if PyInfo in ctx.attr.dep else [])
] + maybe_pyinfo + maybe_javainfo

# A rule that allows selecting a subset of transitive dependencies, and using
# them as a layer in an image.
Expand Down
22 changes: 21 additions & 1 deletion tests/container/java/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

load("//contrib:test.bzl", "container_test")
load("//java:image.bzl", "java_image")
load("//java:image.bzl", "java_image", "java_layer")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -52,6 +52,20 @@ java_image(
main_class = "examples.images.Binary",
)

java_layer(
name = "java_image_external_deps_layer",
filter = "@",
deps = [":java_image_library"],
)

java_image(
name = "java_image_complex_external_deps",
srcs = ["//testdata:Binary.java"],
layers = [":java_image_external_deps_layer"],
main_class = "examples.images.Binary",
deps = [":java_image_library"],
)

java_image(
name = "java_classpath_as_file_image",
srcs = ["//testdata:Binary.java"],
Expand Down Expand Up @@ -109,6 +123,12 @@ container_test(
image = ":java_image",
)

container_test(
name = "java_image_complex_external_deps_test",
configs = ["//tests/container/java/configs:java_image_complex_external_deps.yaml"],
image = ":java_image_complex_external_deps",
)

container_test(
name = "java_classpath_as_file_image_test",
configs = ["//tests/container/java/configs:java_classpath_as_file_image.yaml"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
schemaVersion: 2.0.0

metadataTest:
entrypoint: [
'/usr/bin/java',
'-cp',
'/app/io_bazel_rules_docker/../com_google_guava_guava/guava-18.0.jar:/app/io_bazel_rules_docker/tests/container/java/libjava_image_library.jar:/app/io_bazel_rules_docker/tests/container/java/java_image_complex_external_deps.binary.jar:/app/io_bazel_rules_docker/tests/container/java/java_image_complex_external_deps.binary',
'examples.images.Binary'
]

0 comments on commit a8aff40

Please sign in to comment.