Skip to content

add layer for npm packages - #1346

Merged
nlopezgi merged 1 commit into
bazelbuild:masterfrom
Toxicable:nodejs-layering
Jan 20, 2020
Merged

nlopezgi merged 1 commit into
bazelbuild:masterfrom
Toxicable:nodejs-layering

Conversation

@Toxicable

@Toxicable Toxicable commented Dec 28, 2019 •

Copy link
Copy Markdown

Adds support for NodeRuntimeDepsInfo and NpmPackageInfo
This ensures that npm deps are placed in their own layer, improving caching and rebuilds
Also puts nodejs and nodejs args so that it can be cached between instances of nodejs_image targets.

based off work started here https://github.com/bazelbuild/rules_docker/pull/487/files

@Toxicable

Copy link
Copy Markdown
Author

/assign @smukherj1

@Toxicable

Toxicable commented Dec 28, 2019 •

Copy link
Copy Markdown
Author

Just tested these changes on our internal repository and looks like it's working as expected
More importantly the app is still running as expected, so we're not missing any files :P
Here's the output from docker
before

e5faf4d3e19c: Loading layer [==>]  255.7MB/255.7MB
84ff92691f90: Loading layer [==>]  10.24kB/10.24kB

after

62bba9724dca: Loading layer [==>]  10.24kB/10.24kB
02fa2ebc0496: Loading layer [==>]  41.12MB/41.12MB
32e8dde897ae: Loading layer [==>]  10.24kB/10.24kB
078828ab7718: Loading layer [==>]  213.1MB/213.1MB
f432715cf9ce: Loading layer [==>]  1.444MB/1.444MB
84ff92691f90: Loading layer [==>]  10.24kB/10.24kB

Comment thread container/image_test.py Resolved
Comment thread nodejs/image.bzl Resolved
Comment thread container/image_test.py Resolved
Comment thread nodejs/image.bzl Resolved
@gregmagolan

gregmagolan commented Jan 3, 2020 •

Copy link
Copy Markdown
Contributor

Nice. I'm glad to see the node_modules attribute removed as I'd like to remove it from rules_nodejs but this repo was blocking that.

I'd like to dig into this a bit further and understand it in detail next week as this was something the @Globegitter started working on here bazel-contrib/rules_nodejs#863 but that was before we had NodeRuntimeDepsInfo.

Fr the platform choice of node to put in the docker image I think that should be controlled with the --platform Bazel CLI option.

There's an example usage in https://github.com/bazelbuild/rules_nodejs/blob/master/examples/nestjs/src/BUILD.bazel#L48:

# bazel build --platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64 //src:docker
nodejs_image(
    name = "docker",
    data = [
        ":app",
    ],
    entry_point = ":main.ts",
    # TODO: switch to fine grained deps -- see bazelbuild/rules_docker#1000
    # and add test for docker image
    node_modules = "@npm//:node_modules",
)

Comment thread container/image_test.py Resolved
Comment thread nodejs/image.bzl Resolved
Comment thread nodejs/image.bzl Outdated
return depset(transitive = depsets)

def _app_layer_impl(ctx):
return lang_image.implementation(ctx, runfiles = _runfiles, emptyfiles = _emptyfiles)

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.

where is the entrypoint now being set since you're not calling app_layer any longer?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not sure what you're referring to as the entrypoint, do you mean the entry_point that exists on a nodejs_binary?
Because that gets collected via the DeafultInfo of the nodejs_binary on _app_layer_impl.
Note the ./app/testdata/nodejs_image_binary.runfiles/io_bazel_rules_docker/testdata/nodejs_image_binary.sh in the above test, that's the script called to start the process inside the container.

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.

Let me expand a little on my question.
Currently, if you run a nodehs_image target, the entrypoint to the container is set here. When I print its value for //container/nodejs:nodejs_image the entrypoint to the container is set to "/app/tests/container/nodejs/nodejs_image.binary"
Now in your change, you are replacing the call to app_layer in nodejs/image/bzl for a call to _app_layer. the rule _app_layer is implemented by _app_layer_impl which in turn is calling lang_image.implementation(ctx, runfiles = _runfiles, emptyfiles = _emptyfiles). This call to lang_image.implementation is not setting any value for the entrypoint of the container. So where is the value for the entrypoint for the nodejs_image now being set?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ah I think I understand, initally I though we'd need a app specific layer there. But turns out it's not needed. I've switched back to use the app_layer from //lang:image.bzl.
Which should fix up the entrypoint issues

Comment thread container/image_test.py Resolved
@nlopezgi

Copy link
Copy Markdown
Contributor

Added more details to my question. Do let me know when this is ready for another review (i.e., make sure buildkite tests are passing)

@Toxicable
Toxicable force-pushed the nodejs-layering branch 9 times, most recently from ad899ad to c7d26fa Compare January 17, 2020 07:33
@Toxicable

Copy link
Copy Markdown
Author

@nlopezgi Should be good for review again, addressed those points and CI is green.

@nlopezgi

Copy link
Copy Markdown
Contributor

/gcbrun

@nlopezgi

Copy link
Copy Markdown
Contributor

The nodejs image integration test is failing.
https://github.com/bazelbuild/rules_docker/blob/master/testing/e2e/nodejs_image.sh

This is the error message:
INFO: Build completed successfully, 55 total actions
Error: Cannot find module './nodejs_image_lib'. Please verify that the package.json has a valid "main" entry
    at Function.module.constructor._resolveFilename (tests/container/nodejs/nodejs_image.binary_require_patch.js:482:17)
    at Function.Module._load (internal/modules/cjs/loader.js:687:27)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (testdata/nodejs_image.js:2:13)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Object.<anonymous> (tests/container/nodejs/nodejs_image.binary_loader.js:32:24)
+ EXPECT_CONTAINS 'Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image' 'Hello World!'
+ local 'complete=Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image'
+ local 'substring=Hello World!'
+ local 'message=Expected '\''Hello World!'\'' not found in '\''Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image'\'''
+ echo Checking 'Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image' contains 'Hello World!'
+ CONTAINS 'Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image' 'Hello World!'
+ local 'complete=Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image'
+ local 'substring=Hello World!'
+ grep -Fsq -- 'Hello World!'
Checking Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image contains Hello World!
FAILURE: Expected 'Hello World!' not found in 'Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image'
+ echo 'Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image'
+ fail 'Expected '\''Hello World!'\'' not found in '\''Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image'\'''
+ echo 'FAILURE: Expected '\''Hello World!'\'' not found in '\''Loaded image ID: sha256:e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059
Tagging e7b6705db119ddb2f990e3fc6c79caa1c0f7729035fbb05583693493785f3059 as bazel/tests/container/nodejs:nodejs_image'\'''
+ exit 1

Failure corresponds to this line. You can probably repro it locally by running bazel run tests/container/nodejs:nodejs_image or running the nodejs_image.sh script directly.

@Toxicable
Toxicable force-pushed the nodejs-layering branch 5 times, most recently from 31faed1 to 52636be Compare January 17, 2020 20:04
@Toxicable

Copy link
Copy Markdown
Author

I ran bazel test tests/container/nodejs/... and all passes locally for me now. I adjusted the testdata to include the extra lib and fixed up those build targets to correctly include the lib outputs.

@Toxicable

Toxicable commented Jan 17, 2020 •

Copy link
Copy Markdown
Author

/gcburn Looks like I cant do that one :P

@nlopezgi

Copy link
Copy Markdown
Contributor

/gcbrun

@k8s-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: nlopezgi, Toxicable

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants