From 9dd084810fb9bca1475e56bf2fafe5a5b80f3a64 Mon Sep 17 00:00:00 2001 From: Rahul Bhati Date: Sun, 24 May 2020 00:16:03 +0530 Subject: [PATCH] Added support for setting bodyParser limit param via environment variable (#1618) In the NodeJs environment, the body-parser was set to 1MB, this change makes it configurable with an env variable. --- environments/nodejs/server.js | 10 ++-- .../hello/hello.js | 11 ++++ .../hello/package.json | 16 ++++++ .../specs/README | 42 +++++++++++++++ .../specs/env-node.yaml | 21 ++++++++ .../specs/fission-deployment-config.yaml | 7 +++ .../specs/function-hello.yaml | 54 +++++++++++++++++++ 7 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 examples/spec-example/nodejs-bodyParser-limit-example/hello/hello.js create mode 100644 examples/spec-example/nodejs-bodyParser-limit-example/hello/package.json create mode 100644 examples/spec-example/nodejs-bodyParser-limit-example/specs/README create mode 100644 examples/spec-example/nodejs-bodyParser-limit-example/specs/env-node.yaml create mode 100644 examples/spec-example/nodejs-bodyParser-limit-example/specs/fission-deployment-config.yaml create mode 100644 examples/spec-example/nodejs-bodyParser-limit-example/specs/function-hello.yaml diff --git a/environments/nodejs/server.js b/environments/nodejs/server.js index 8c042320..e864aa3d 100644 --- a/environments/nodejs/server.js +++ b/environments/nodejs/server.js @@ -99,10 +99,12 @@ function specialize(req, res) { // Request logger app.use(morgan('combined')); -app.use(bodyParser.urlencoded({ extended: false, limit: '1mb' })); -app.use(bodyParser.json({limit: '1mb'})); -app.use(bodyParser.raw({limit: '1mb'})); -app.use(bodyParser.text({ type : "text/*", limit: '1mb' })); +let bodyParserLimit = process.env.BODY_PARSER_LIMIT || '1mb'; + +app.use(bodyParser.urlencoded({ extended: false, limit: bodyParserLimit})); +app.use(bodyParser.json({limit: bodyParserLimit})); +app.use(bodyParser.raw({limit: bodyParserLimit})); +app.use(bodyParser.text({ type : "text/*", limit: bodyParserLimit })); app.post('/specialize', withEnsureGeneric(specialize)); app.post('/v2/specialize', withEnsureGeneric(specializeV2)); diff --git a/examples/spec-example/nodejs-bodyParser-limit-example/hello/hello.js b/examples/spec-example/nodejs-bodyParser-limit-example/hello/hello.js new file mode 100644 index 00000000..4b099700 --- /dev/null +++ b/examples/spec-example/nodejs-bodyParser-limit-example/hello/hello.js @@ -0,0 +1,11 @@ +const process = require("process"); + +module.exports = async function (context) { + let message = + "BODY_PARSER_LIMIT received from env variable " + + process.env.BODY_PARSER_LIMIT; + return { + status: 200, + body: message, + }; +}; diff --git a/examples/spec-example/nodejs-bodyParser-limit-example/hello/package.json b/examples/spec-example/nodejs-bodyParser-limit-example/hello/package.json new file mode 100644 index 00000000..605772b6 --- /dev/null +++ b/examples/spec-example/nodejs-bodyParser-limit-example/hello/package.json @@ -0,0 +1,16 @@ +{ + "name": "fission-nodejs-bodyParser-example", + "version": "0.1.0", + "author": "Rahul Bhati", + "contributors": [ + { + "name": "Rahul Bhati", + "email": "rjbhati009@gmail.com" + } + ], + "description": "Nodejs example for setting bodyParser limit param via environment variable", + "engines": { + "node": ">=7.6.0" + }, + "dependencies": {} +} diff --git a/examples/spec-example/nodejs-bodyParser-limit-example/specs/README b/examples/spec-example/nodejs-bodyParser-limit-example/specs/README new file mode 100644 index 00000000..1db3f9a5 --- /dev/null +++ b/examples/spec-example/nodejs-bodyParser-limit-example/specs/README @@ -0,0 +1,42 @@ + +Fission Specs +============= + +This is a set of specifications for a Fission app. This includes functions, +environments, and triggers; we collectively call these things "resources". + +How to use these specs +---------------------- + +These specs are handled with the 'fission spec' command. See 'fission spec --help'. + +'fission spec apply' will "apply" all resources specified in this directory to your +cluster. That means it checks what resources exist on your cluster, what resources are +specified in the specs directory, and reconciles the difference by creating, updating or +deleting resources on the cluster. + +'fission spec apply' will also package up your source code (or compiled binaries) and +upload the archives to the cluster if needed. It uses 'ArchiveUploadSpec' resources in +this directory to figure out which files to archive. + +You can use 'fission spec apply --watch' to watch for file changes and continuously keep +the cluster updated. + +You can add YAMLs to this directory by writing them manually, but it's easier to generate +them. Use 'fission function create --spec' to generate a function spec, +'fission environment create --spec' to generate an environment spec, and so on. + +You can edit any of the files in this directory, except 'fission-deployment-config.yaml', +which contains a UID that you should never change. To apply your changes simply use +'fission spec apply'. + +fission-deployment-config.yaml +------------------------------ + +fission-deployment-config.yaml contains a UID. This UID is what fission uses to correlate +resources on the cluster to resources in this directory. + +All resources created by 'fission spec apply' are annotated with this UID. Resources on +the cluster that are _not_ annotated with this UID are never modified or deleted by +fission. + diff --git a/examples/spec-example/nodejs-bodyParser-limit-example/specs/env-node.yaml b/examples/spec-example/nodejs-bodyParser-limit-example/specs/env-node.yaml new file mode 100644 index 00000000..35d79d0a --- /dev/null +++ b/examples/spec-example/nodejs-bodyParser-limit-example/specs/env-node.yaml @@ -0,0 +1,21 @@ +apiVersion: fission.io/v1 +kind: Environment +metadata: + creationTimestamp: null + name: node + namespace: default +spec: + builder: + command: build + image: fission/node-builder:latest + imagepullsecret: "" + keeparchive: false + poolsize: 3 + resources: {} + runtime: + image: fission/node-env:latest + container: + env: + - name: BODY_PARSER_LIMIT + value: 100mb + version: 2 diff --git a/examples/spec-example/nodejs-bodyParser-limit-example/specs/fission-deployment-config.yaml b/examples/spec-example/nodejs-bodyParser-limit-example/specs/fission-deployment-config.yaml new file mode 100644 index 00000000..32f59282 --- /dev/null +++ b/examples/spec-example/nodejs-bodyParser-limit-example/specs/fission-deployment-config.yaml @@ -0,0 +1,7 @@ +# This file is generated by the 'fission spec init' command. +# See the README in this directory for background and usage information. +# Do not edit the UID below: that will break 'fission spec apply' +apiVersion: fission.io/v1 +kind: DeploymentConfig +name: node-spec +uid: 7a2704fb-6cff-4b4a-b5f8-71fe73365538 diff --git a/examples/spec-example/nodejs-bodyParser-limit-example/specs/function-hello.yaml b/examples/spec-example/nodejs-bodyParser-limit-example/specs/function-hello.yaml new file mode 100644 index 00000000..b7d4c656 --- /dev/null +++ b/examples/spec-example/nodejs-bodyParser-limit-example/specs/function-hello.yaml @@ -0,0 +1,54 @@ +include: +- hello/* +kind: ArchiveUploadSpec +name: hello-dBc6 + +--- +apiVersion: fission.io/v1 +kind: Package +metadata: + creationTimestamp: null + name: hello-a98928ed-6b06-482f-9eb5-541540e714b1 + namespace: default +spec: + deployment: + checksum: {} + environment: + name: node + namespace: default + source: + checksum: {} + type: url + url: archive://hello-dBc6 +status: + buildstatus: pending + lastUpdateTimestamp: "2020-05-20T14:12:17Z" + +--- +apiVersion: fission.io/v1 +kind: Function +metadata: + creationTimestamp: null + name: hello + namespace: default +spec: + InvokeStrategy: + ExecutionStrategy: + ExecutorType: poolmgr + MaxScale: 0 + MinScale: 0 + SpecializationTimeout: 120 + TargetCPUPercent: 0 + StrategyType: execution + configmaps: null + environment: + name: node + namespace: default + functionTimeout: 60 + package: + functionName: hello + packageref: + name: hello-a98928ed-6b06-482f-9eb5-541540e714b1 + namespace: default + resources: {} + secrets: null