POST
data to one sync functionFeature introduced in OpenFunction 0.6.0 along with Node.js Function Framework 0.5.0 ↩︎
Exactly the same async function signature.
function (ctx, data) {}
data
: Request body data, parsed as JSON by default
New type openfunction
ready to work.
apiVersion: core.openfunction.io/v1beta1
kind: Function
metadata:
name: func-name
spec:
serving:
# default to knative
runtime: knative
params:
# default to http
FUNCTION_SIGNATURE_TYPE: openfunction
--signature-type=openfunction
FUNCTION_SIGNATURE_TYPE=openfunction
Another plain forwarder
// Standard Express style HTTP sync function
export const tryKnative = (req, res) => {
res.send(`Hello, ${req.query.u || 'World'}!`);
};
// OpenFunction standard function which could
// set bridge between sync and async function
export const tryKnativeAsync = async (ctx, data) => {
console.log('Data received: %o', data);
// Forward HTTP body to Dapr outputs
await ctx.send(data);
// Send something back as HTTP response
// when necessary
// ctx.res.send(data);
};
Remember to build the function, either when applying Function CR manifest or directly via Pack CLI tool
Check out full content in http-trigger.yml
spec:
image: '<image-repo>/<image-name>:<image-tag>'
serving:
runtime: knative
params:
FUNCTION_TARGET: tryKnativeAsync
FUNCTION_SIGNATURE_TYPE: openfunction
outputs:
- name: mqtt-output
component: mqtt-out
operation: create
bindings:
mqtt-out:
type: bindings.mqtt
version: v1
metadata:
- name: consumerID
value: '{uuid}'
- name: url
value: tcp://admin:public@emqx:1883
- name: topic
value: out
Apply function manifest, and check running states
$ kubectl apply -f http-trigger.yaml
function.core.openfunction.io/sample-node-http-trigger created
$ kubectl get fn
NAME BUILDSTATE SERVINGSTATE BUILDER SERVING URL AGE
sample-node-http-trigger Skipped Running serving-7d75d http://openfunction.io/default/sample-node-http-trigger 6d
$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
...
serving-7d75d-ksvc-xn768-v200-deployment 0/0 0 0 6d14h
...
Knative runtime would autoscale the deployment replica to
0
, if no incoming request for a period of time.
Send request to domain ingress.
POST
data to target function URL:
curl -d '{"hello":"ofn"}' \
-H 'Content-Type: application/json' \
http://openfunction.io/default/sample-node-http-trigger
Wait a second (may be few more seconds for cold start), you would get results:
See also: OpenFunction 202 - Set up MQTT Broker ↩︎