This repository has been archived by the owner on Dec 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathkubeless.js
171 lines (151 loc) · 5.46 KB
/
kubeless.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
const vm = require('vm');
const path = require('path');
const Module = require('module');
const bodyParser = require('body-parser');
const client = require('prom-client');
const express = require('express');
const helper = require('./lib/helper');
const morgan = require('morgan');
const bodySizeLimit = Number(process.env.REQ_MB_LIMIT || '1');
const app = express();
app.use(morgan('combined'));
const bodParserOptions = {
type: req => !req.is('multipart/*'),
limit: `${bodySizeLimit}mb`,
};
app.use(bodyParser.raw(bodParserOptions));
app.use(bodyParser.json({ limit: `${bodySizeLimit}mb` }));
app.use(bodyParser.urlencoded({ limit: `${bodySizeLimit}mb`, extended: true }));
const modName = process.env.MOD_NAME;
const funcHandler = process.env.FUNC_HANDLER;
const timeout = Number(process.env.FUNC_TIMEOUT || '180');
const funcPort = Number(process.env.FUNC_PORT || '8080');
const modKubeless = require.main.filename;
const modRootPath = path.join(modKubeless, '..', '..', 'kubeless');
const modPath = path.join(modRootPath, `${modName}.js`);
const libPath = path.join(modRootPath, 'node_modules');
const pkgPath = path.join(modRootPath, 'package.json');
const libDeps = helper.readDependencies(pkgPath);
const { timeHistogram, callsCounter, errorsCounter } = helper.prepareStatistics('method', client);
helper.routeLivenessProbe(app);
helper.routeMetrics(app, client);
const context = {
'function-name': funcHandler,
'timeout' : timeout,
'runtime': process.env.FUNC_RUNTIME,
'memory-limit': process.env.FUNC_MEMORY_LIMIT
};
const script = new vm.Script('\nrequire(\'kubeless\')(require(\''+ modPath +'\'));\n', {
filename: modPath,
displayErrors: true,
});
function modRequire(p, req, res, end) {
if (p === 'kubeless')
return (handler) => modExecute(handler, req, res, end);
else if (libDeps.includes(p))
return require(path.join(libPath, p));
else if (p.indexOf('./') === 0)
return require(path.join(path.dirname(modPath), p));
else
return require(p);
}
function modExecute(handler, req, res, end) {
let func = null;
switch (typeof handler) {
case 'function':
func = handler;
break;
case 'object':
if (handler) func = handler[funcHandler];
break;
}
if (func === null)
throw new Error(`Unable to load ${handler}`);
try {
let data = req.body;
if (!req.is('multipart/*') && req.body.length > 0) {
if (req.is('application/json')) {
data = JSON.parse(req.body.toString('utf-8'))
} else {
data = req.body.toString('utf-8')
}
}
const event = {
'event-type': req.get('event-type'),
'event-id': req.get('event-id'),
'event-time': req.get('event-time'),
'event-namespace': req.get('event-namespace'),
data,
'extensions': { request: req, response: res },
};
Promise.resolve(func(event, context))
// Finalize
.then(rval => modFinalize(rval, res, end))
// Catch asynchronous errors
.catch(err => handleError(err, res, funcLabel(req), end))
;
} catch (err) {
// Catch synchronous errors
handleError(err, res, funcLabel(req), end);
}
}
function modFinalize(result, res, end) {
if (!res.finished) switch(typeof result) {
case 'string':
res.end(result);
break;
case 'object':
res.json(result); // includes res.end(), null also handled
break;
case 'undefined':
res.end();
break;
default:
res.end(JSON.stringify(result));
}
end();
}
function handleError(err, res, label, end) {
errorsCounter.labels(label).inc();
res.status(500).send('Internal Server Error');
console.error(`Function failed to execute: ${err.stack}`);
end();
}
function funcLabel(req) {
return modName + '-' + req.method;
}
app.all('*', (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
if (req.method === 'OPTIONS') {
// CORS preflight support (Allow any method or header requested)
res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']);
res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
res.end();
} else {
const label = funcLabel(req);
const end = timeHistogram.labels(label).startTimer();
callsCounter.labels(label).inc();
const sandbox = Object.assign({}, global, {
__filename: modPath,
__dirname: modRootPath,
module: new Module(modPath, null),
require: (p) => modRequire(p, req, res, end),
});
try {
script.runInNewContext(sandbox, { timeout : timeout * 1000 });
} catch (err) {
if (err.toString().match('Error: Script execution timed out')) {
res.status(408).send(err);
// We cannot stop the spawned process (https://github.com/nodejs/node/issues/3020)
// we need to abruptly stop this process
console.error('CRITICAL: Unable to stop spawned process. Exiting');
process.exit(1);
} else {
handleError(err, res, funcLabel, end);
}
}
}
});
const server = app.listen(funcPort);
helper.configureGracefulShutdown(server);