From 2581ccb36d59ccea4640f1ab410df14990dfe992 Mon Sep 17 00:00:00 2001 From: Ian Serlin Date: Tue, 18 Sep 2018 16:11:52 +0200 Subject: [PATCH] require() deployed nodejs function instead of executing inline (#789) * require() deployed nodejs function instead of executing inline * add instructions and sample configs for webpack users to runtimes.md * Update NodeJS images --- docker/runtime/nodejs/kubeless.js | 2 +- docs/runtimes.md | 61 +++++++++++++++++++++++++++++++ kubeless-non-rbac.jsonnet | 4 +- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/docker/runtime/nodejs/kubeless.js b/docker/runtime/nodejs/kubeless.js index 6f6acf2b7..13cdd8b6a 100644 --- a/docker/runtime/nodejs/kubeless.js +++ b/docker/runtime/nodejs/kubeless.js @@ -47,7 +47,7 @@ const context = { 'memory-limit': process.env.FUNC_MEMORY_LIMIT }; -const script = new vm.Script(fs.readFileSync(modPath) + '\nrequire(\'kubeless\')(module.exports);\n', { +const script = new vm.Script('\nrequire(\'kubeless\')(require(\''+ modPath +'\'));\n', { filename: modPath, displayErrors: true, }); diff --git a/docs/runtimes.md b/docs/runtimes.md index 1b2ffe1d3..b0b6ecb61 100644 --- a/docs/runtimes.md +++ b/docs/runtimes.md @@ -57,6 +57,67 @@ $ kubeless function deploy myFunction --runtime nodejs6 \ --from-file test.js ``` +**For Webpack Users** + +Your webpacked functions will be `require()`-d in so your bundle should work out of the box. However, if your bundle size is approaching 1mb you should take advantage of Kubeless' ability to install dependencies for you instead of bundling them all into your payload. + +You will need to customize your webpack config to suit your own project, but below is an sample config of how to achieve this in Webpack 4.x: + +_webpack.config.js_ +```js +const path = require("path"); +const nodeExternals = require("webpack-node-externals"); +const CopyWebpackPlugin = require("copy-webpack-plugin"); + +module.exports = { + entry: { + handlers: "./handlers.js" + }, + node: { + __filename: true, + __dirname: true + }, + target: "node", + // do not include dependencies in the bundle + externals: [nodeExternals()], + devtool: "source-map", + module: { + rules: [ + { + test: /\.js$/, + use: "babel-loader", + // do not transpile the depedencies + exclude: /node_modules/ + } + ] + }, + plugins: [ + // do include the project's `package.json` in the bundle + new CopyWebpackPlugin([ + { + from: path.join(__dirname, "path", "to", "your", "package.json"), + to: "package.json" + } + ]) + ] +}; +``` + +Additionally, in your babel config, you can specify the transpile target to be the version of node you're using for your runtime. This is an example for Babel 7.x: + +```js +module.exports = { + plugins: [ + "@babel/plugin-proposal-class-properties", + "@babel/plugin-proposal-object-rest-spread", + "@babel/plugin-syntax-dynamic-import", + "@babel/plugin-transform-runtime" + ], + // note the target node version here for nodejs8 + presets: [["@babel/preset-env", { targets: { node: "8.10" } }]] +}; +``` + #### Server implementation For the Node.js runtime we start an [Express](http://expressjs.com) server and we include the routes for serving the health check and exposing the monitoring metrics. Apart from that we enable [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) requests and [Morgan](https://github.com/expressjs/morgan) for handling the logging in the server. Monitoring is supported if the function is synchronous or if it uses promises. diff --git a/kubeless-non-rbac.jsonnet b/kubeless-non-rbac.jsonnet index 0cf58d29e..bdfac21b4 100644 --- a/kubeless-non-rbac.jsonnet +++ b/kubeless-non-rbac.jsonnet @@ -113,13 +113,13 @@ local runtime_images ='[ { "name": "node6", "version": "6", - "runtimeImage": "kubeless/nodejs@sha256:f2a338c62d010687137c0880d1b68bea926f71a7111251a4622db8ae8c036898", + "runtimeImage": "kubeless/nodejs@sha256:556ff930c7a609d1ad90322d41c8b562cb42313898486fed9674fb2647e4b42f", "initImage": "node:6.10" }, { "name": "node8", "version": "8", - "runtimeImage": "kubeless/nodejs@sha256:3b5180a9e0bdce043f0f455758561cf4ad62406fcc80140c2393a2c3a1ff88ac", + "runtimeImage": "kubeless/nodejs@sha256:5c9c5e36f9845f2cf8e9e0d55993796d82e34a2b8c0f8a508c9d3c04b2041076", "initImage": "node:8" } ],