Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Add response object to function event (#745)
Browse files Browse the repository at this point in the history
* Add response object to function event

* Add docs
  • Loading branch information
andresmgot authored May 11, 2018
1 parent 8e0e006 commit 8b266a2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions docker/runtime/golang/kubeless.tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
EventNamespace: r.Header.Get("event-namespace"),
Extensions: functions.Extension{
Request: r,
Response: w,
Context: ctx,
},
}
Expand Down
2 changes: 1 addition & 1 deletion docker/runtime/nodejs/kubeless.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function modExecute(handler, req, res, end) {
'event-time': req.get('event-time'),
'event-namespace': req.get('event-namespace'),
data,
'extensions': { request: req },
'extensions': { request: req, response: res },
};
Promise.resolve(func(event, context))
// Finalize
Expand Down
10 changes: 5 additions & 5 deletions docker/runtime/php/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct()
*
* @return void
*/
private function runFunction(Request $request)
private function runFunction(Request $request, Response $response)
{
set_time_limit($this->timeout);
ob_start();
Expand All @@ -63,12 +63,14 @@ private function runFunction(Request $request)
'event-namespace' => $_SERVER['HTTP_EVENT_NAMESPACE'],
'extensions' => (object) array(
'request' => $request,
'response' => $response,
)
);
$res = call_user_func($this->function, $event, $this->functionContext);
$event->extensions->response->getBody()->write($res);
ob_end_clean();
chdir($this->currentDir);
return $res;
return $event->extensions->response;
} else {
sleep($this->timeout);
posix_kill($pid, SIGKILL);
Expand Down Expand Up @@ -106,10 +108,8 @@ public function root(Request $request, Response $response, array $args)
{
try {
$this->validate();
$ret = $this->runFunction($request);
$response->getBody()->write($ret);

return $response;
return $this->runFunction($request, $response);
} catch (\Kubeless\TimeoutFunctionException $e) {
$res = $response->withStatus(408);

Expand Down
1 change: 1 addition & 0 deletions docs/kubeless-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ event:
event-namespace: "kafkatriggers.kubeless.io" # Event emitter
extensions: # Optional parameters
request: ... # Reference to the request received
response: ... # Reference to the response to send
# (specific properties will depend on the function language)
context:
function-name: "pubsub-nodejs"
Expand Down
8 changes: 4 additions & 4 deletions kubeless-non-rbac.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ local runtime_images ='[
{
"name": "node6",
"version": "6",
"runtimeImage": "kubeless/nodejs@sha256:61c5a10aacb709c4575a09a4aa28f822b2d008c0dbf4aa0b124705ee9ca143f9",
"runtimeImage": "kubeless/nodejs@sha256:0a8a72af4cc3bfbfd4fe9bd309cbf486e7493d0dc32a691673b3f0d3fae07487",
"initImage": "node:6.10"
},
{
"name": "node8",
"version": "8",
"runtimeImage": "kubeless/nodejs@sha256:fc1aa96e55116400ee13d664a655dfb2025ded91858ebfd5fc0c8f0d6b923eba",
"runtimeImage": "kubeless/nodejs@sha256:76ee28dc7e3613845fface2d1c56afc2e6e2c6d6392c724795a7ccc2f5e60582",
"initImage": "node:8"
}
],
Expand Down Expand Up @@ -140,7 +140,7 @@ local runtime_images ='[
{
"name": "php72",
"version": "7.2",
"runtimeImage": "kubeless/php@sha256:4e44ab60f597e93097bf9f5ea91d58bd9c308bf206043db2a9809ec16a8ff2f4",
"runtimeImage": "kubeless/php@sha256:b605bb6b5ae3b1a2a93570939296618904259d7767a14002fa9733e66d59849b",
"initImage": "composer:1.6"
}
],
Expand All @@ -155,7 +155,7 @@ local runtime_images ='[
"name": "go1.10",
"version": "1.10",
"runtimeImage": "kubeless/go@sha256:bf72622344a54e4360f31d3fea5eb9dca2c96fbedc6f0ad7c54f3eb8fb7bd353",
"initImage": "kubeless/go-init@sha256:e262f70639594b3a9e3481843171ecbbe82e84b786825ebe28bc1a3ae89310d3"
"initImage": "kubeless/go-init@sha256:d0812c4e8351bfd95d0574efd23613cff2664d6a57af4ed0a20ebc651382d476"
}
],
"depName": "Gopkg.toml",
Expand Down
5 changes: 3 additions & 2 deletions pkg/functions/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (

// Extension includes a reference to the Event request and its Context (to handle timeouts)
type Extension struct {
Request *http.Request
Context context.Context
Request *http.Request
Response http.ResponseWriter
Context context.Context
}

// Event includes information about the event source
Expand Down

0 comments on commit 8b266a2

Please sign in to comment.