Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ d_repositories()

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "9b72bb0aea72d7cbcfc82a01b1e25bf3d85f791e790ddec16c65e2d906382ee0",
strip_prefix = "rules_nodejs-0.16.2",
urls = ["https://github.com/bazelbuild/rules_nodejs/archive/0.16.2.zip"],
sha256 = "54ce360f02e2078150135fb139c482da702d5814294fb58e25d8536a66bba5eb",
strip_prefix = "rules_nodejs-0f6201a9c70e7051881211c3855df9d493b06f02",
urls = ["https://github.com/bazelbuild/rules_nodejs/archive/0f6201a9c70e7051881211c3855df9d493b06f02.tar.gz"],
)

load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "npm_install")
Expand Down
1 change: 1 addition & 0 deletions container/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ TEST_TARGETS = [
# Re-enable once https://github.com/bazelbuild/rules_d/issues/14 is fixed.
# "d_image",
":java_image",
":nodejs_image",
":py_image",
":py_image_with_symlinks_in_data",
":py_image_complex",
Expand Down
92 changes: 92 additions & 0 deletions container/image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,98 @@ def test_war_image(self):
'./jetty/webapps/ROOT/WEB-INF/lib/javax.servlet-api-3.0.1.jar',
])


def test_nodejs_image(self):
self.maxDiff = None
with TestImage('nodejs_image') as img:
# Check the application layer (top layer), which also contains symlinks to the bottom layers.
self.assertTopLayerContains(img, ['.',
'./app',
'./app/testdata',
'./app/testdata/nodejs_image.binary_bin.runfiles',
'./app/testdata/nodejs_image.binary_bin.runfiles/io_bazel_rules_docker',
'./app/testdata/nodejs_image.binary_bin.runfiles/io_bazel_rules_docker/testdata',
'./app/testdata/nodejs_image.binary_bin.runfiles/io_bazel_rules_docker/testdata/nodejs_image.js',
'./app/testdata/nodejs_image.binary_bin.runfiles/bazel_tools',
'./app/testdata/nodejs_image.binary_bin.runfiles/bazel_tools/tools',
'./app/testdata/nodejs_image.binary_bin.runfiles/bazel_tools/tools/bash',
'./app/testdata/nodejs_image.binary_bin.runfiles/bazel_tools/tools/bash/runfiles',
'./app/testdata/nodejs_image.binary_bin.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash',
'./app/testdata/nodejs_image.binary_bin.runfiles/io_bazel_rules_docker/testdata/nodejs_image.binary_bin_loader.js',
'./app/testdata/nodejs_image.binary_bin.runfiles/io_bazel_rules_docker/testdata/nodejs_image.binary_bin.sh',
'/app',
'/app/testdata',
'/app/testdata/nodejs_image.binary_bin',
'/app/testdata/nodejs_image.binary_bin.runfiles',
'/app/testdata/nodejs_image.binary_bin.runfiles/io_bazel_rules_docker',
'/app/testdata/nodejs_image.binary_bin.runfiles/io_bazel_rules_docker/external',
])

# Check that the next layer contains node_modules
layerOneFiles = [
'.',
'./app',
'./app/testdata',
'./app/testdata/nodejs_image.binary_bin.runfiles',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps/node_modules',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps/node_modules/jsesc',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps/node_modules/jsesc/LICENSE-MIT.txt',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps/node_modules/jsesc/README.md',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps/node_modules/jsesc/bin',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps/node_modules/jsesc/bin/jsesc',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps/node_modules/jsesc/jsesc.js',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps/node_modules/jsesc/man',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps/node_modules/jsesc/man/jsesc.1',
'./app/testdata/nodejs_image.binary_bin.runfiles/npm_deps/node_modules/jsesc/package.json',
'/app',
'/app/testdata',
'/app/testdata/nodejs_image.binary_bin',
'/app/testdata/nodejs_image.binary_bin.runfiles',
'/app/testdata/nodejs_image.binary_bin.runfiles/io_bazel_rules_docker',
'/app/testdata/nodejs_image.binary_bin.runfiles/io_bazel_rules_docker/external',
]
self.assertLayerNContains(img, 1, layerOneFiles)

# Check that the next layer contains node_args
layerTwoFiles = [
'.',
'./app',
'./app/testdata',
'./app/testdata/nodejs_image.binary_bin.runfiles',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs/bin',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs/bin/node_args.sh'
]
self.assertLayerNContains(img, 2, layerTwoFiles)

# Check that the next layer contains node runfiles
layerThreeFiles = [
'.',
'./app',
'./app/testdata',
'./app/testdata/nodejs_image.binary_bin.runfiles',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs/bin',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs/bin/nodejs',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs/bin/nodejs/bin',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs/bin/nodejs/bin/node'
]
self.assertLayerNContains(img, 3, layerThreeFiles)

# Check that the next layer contains node
layerFourFiles = [
'.',
'./app',
'./app/testdata',
'./app/testdata/nodejs_image.binary_bin.runfiles',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs/bin',
'./app/testdata/nodejs_image.binary_bin.runfiles/nodejs/bin/node'
]
self.assertLayerNContains(img, 4, layerFourFiles)


# Re-enable once https://github.com/bazelbuild/rules_d/issues/14 is fixed.
# def test_d_image_args(self):
# with TestImage('d_image') as img:
Expand Down
53 changes: 27 additions & 26 deletions lang/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -217,33 +217,34 @@ def app_layer_impl(ctx, runfiles = None, emptyfiles = None):
null_cmd = args == [],
)

app_layer_attrs = dicts.add(_container.image.attrs, {
# The base image on which to overlay the dependency layers.
"base": attr.label(mandatory = True),
# The binary target for which we are synthesizing an image.
# If specified, the layer will not be "image agnostic", meaning
# that the runfiles required by "dep" will be created (or symlinked,
# if already found in an agnostic path from the base image) under
# the runfiles dir.
"binary": attr.label(
executable = True,
cfg = "target",
),
"data": attr.label_list(allow_files = True),

# Override the defaults.
"data_path": attr.string(default = "."),
# The dependency whose runfiles we're appending.
# If not specified, then the layer will be treated as the top layer,
# and all remaining deps of "binary" will be added under runfiles.
"dep": attr.label(providers = [DefaultInfo]),
"directory": attr.string(default = "/app"),
"entrypoint": attr.string_list(default = []),
"legacy_run_behavior": attr.bool(default = False),
"workdir": attr.string(default = ""),
})

_app_layer = rule(
attrs = dicts.add(_container.image.attrs, {

# The base image on which to overlay the dependency layers.
"base": attr.label(mandatory = True),
# The binary target for which we are synthesizing an image.
# If specified, the layer will not be "image agnostic", meaning
# that the runfiles required by "dep" will be created (or symlinked,
# if already found in an agnostic path from the base image) under
# the runfiles dir.
"binary": attr.label(
executable = True,
cfg = "target",
),
"data": attr.label_list(allow_files = True),

# Override the defaults.
"data_path": attr.string(default = "."),
# The dependency whose runfiles we're appending.
# If not specified, then the layer will be treated as the top layer,
# and all remaining deps of "binary" will be added under runfiles.
"dep": attr.label(providers = [DefaultInfo]),
"directory": attr.string(default = "/app"),
"entrypoint": attr.string_list(default = []),
"legacy_run_behavior": attr.bool(default = False),
"workdir": attr.string(default = ""),
}),
attrs = app_layer_attrs,
executable = True,
outputs = _container.image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
Expand Down
74 changes: 62 additions & 12 deletions nodejs/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ The signature of this rule is compatible with nodejs_binary.
"""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")
load("@build_bazel_rules_nodejs//:defs.bzl", "NodeJSSourcesInfo", "nodejs_binary")
load(
"//container:container.bzl",
"container_pull",
_container = "container",
)
load(
"//lang:image.bzl",
"app_layer",
"app_layer_attrs",
"app_layer_impl",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you alias these with lang_ so that they are visibly distinct from the ones declared here

)
load(
Expand Down Expand Up @@ -64,6 +64,15 @@ DEFAULT_BASE = select({
"@io_bazel_rules_docker//:optimized": "@nodejs_image_base//image",
})

def _app_runfiles(dep):
if NodeJSSourcesInfo in dep:
return dep[NodeJSSourcesInfo].data
else:
return _runfiles(dep)

def _node_module_runfiles(dep):
return dep[NodeJSSourcesInfo].node_modules

def _runfiles(dep):
return depset(transitive = [dep.default_runfiles.files, dep.data_runfiles.files, dep.files])

Expand Down Expand Up @@ -98,12 +107,34 @@ _dep_layer = rule(
implementation = _dep_layer_impl,
)

def _app_layer_impl(ctx):
return app_layer_impl(ctx, runfiles = _app_runfiles, emptyfiles = _emptyfiles)

_app_layer = rule(
attrs = app_layer_attrs,
executable = True,
outputs = _container.image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
implementation = _app_layer_impl,
)

def _node_module_layer_impl(ctx):
return app_layer_impl(ctx, runfiles = _node_module_runfiles, emptyfiles = _emptyfiles)

_node_module_layer = rule(
attrs = app_layer_attrs,
executable = True,
outputs = _container.image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
implementation = _node_module_layer_impl,
)

def nodejs_image(
name,
base = None,
data = [],
layers = [],
node_modules = "//:node_modules",
node_modules = None,
**kwargs):
"""Constructs a container image wrapping a nodejs_binary target.

Expand All @@ -114,12 +145,8 @@ def nodejs_image(
"""
binary_name = name + ".binary"

layers = [
# Put the Node binary into its own layer.
"@nodejs//:node",
# node_modules can get large, it should be in its own layer.
node_modules,
] + layers
# Note: We need to get the underlying node binary to be able to access the NodeJSSourcesInfo provider
node_binary_name = binary_name + "_bin"
Comment thread
Globegitter marked this conversation as resolved.
Resolved

nodejs_binary(
name = binary_name,
Expand All @@ -128,20 +155,43 @@ def nodejs_image(
**kwargs
)

# Move layers after nodejs_binary as it already gets these dependencies per default
layers = [
Comment thread
Globegitter marked this conversation as resolved.
Resolved
# Put the Node binary into its own layer.
"@nodejs//:node",
"@nodejs//:node_runfiles",
"@nodejs//:bin/node_args.sh",
# Note: The empty string is a placeholder layer for the node modules so we can special case it below
"",
] + layers

# TODO(mattmoor): Consider making the directory into which the app
# is placed configurable.
base = base or DEFAULT_BASE
for index, dep in enumerate(layers):
this_name = "%s.%d" % (name, index)
_dep_layer(name = this_name, base = base, dep = dep, binary = binary_name, testonly = kwargs.get("testonly"))
if dep == "":
# node_modules are deprecated but still support it:
if node_modules:
_dep_layer(name = this_name, base = base, dep = node_modules, binary = node_binary_name, testonly = kwargs.get("testonly"))
else:
# We need to get the node_modules from the NodeJSSourcesInfo provider from the node binary
_node_module_layer(
name = this_name,
base = base,
binary = node_binary_name,
testonly = kwargs.get("testonly"),
)
else:
_dep_layer(name = this_name, base = base, dep = dep, binary = node_binary_name, testonly = kwargs.get("testonly"))
base = this_name

visibility = kwargs.get("visibility", None)
tags = kwargs.get("tags", None)
app_layer(
_app_layer(
name = name,
base = base,
binary = binary_name,
binary = node_binary_name,
visibility = visibility,
tags = tags,
args = kwargs.get("args"),
Expand Down
14 changes: 14 additions & 0 deletions testdata/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ load(
"java_image",
"war_image",
)
load("//nodejs:image.bzl", "nodejs_image")
load("//oci:oci.bzl", "oci_push")
load("//python3:image.bzl", "py3_image")
load("//python:image.bzl", "py_image", "py_layer")
Expand Down Expand Up @@ -903,6 +904,19 @@ container_image(
# ],
#)

nodejs_image(
name = "nodejs_image",
args = [
"arg0",
"arg1",
],
data = [
":nodejs_image.js",
"@npm_deps//jsesc",
],
entry_point = "io_bazel_rules_docker/testdata/nodejs_image.js",
)

generate_deb(
name = "pkg1",
)
Expand Down