Writing a Function
The TriggerMesh Function
API provides opportunities to implement custom event flow logic and may act as a source, transformation, or a target. Currently, Python, NodeJS, and Ruby runtimes are supported.
Tip
You can verify that the API is available with the following command:
$ kubectl get crd functions.extensions.triggermesh.io
NAME CREATED AT
functions.extensions.triggermesh.io 2021-10-06T09:01:33Z
You can also explore the API specification with:
$ kubectl explain functions
Warning
The TriggerMesh Function
API is an opinionated, simple to consume, Function as a Service (FaaS) system. It is aimed to be used for event processing and does not support external dependencies. Functions that may need external dependencies are best served with Knative Serving.
Example: A Python function
As an example, let's write a Python function which reads a name from an incoming payload and returns a "Hello" message.
Writing a function requires two steps:
- Writing a function manifest
- Applying the manifest to your Kubernetes cluster
The Function object spec requires a minimal amount of configuration:
- The
runtime
, here we choosepython
- Whether the function is publicly accessible or not using the
public
keyword. - The
entrypoint
, which specifies the name of the function - The
code
, written in-line with the function manifest
Save the YAML manifest below in a file called function.yaml
apiVersion: extensions.triggermesh.io/v1alpha1
kind: Function
metadata:
name: python-function-hello
spec:
runtime: python
public: true
entrypoint: endpoint
code: |
def endpoint(event, context):
return "Hello " + event['name']
You can then create the function with:
kubectl apply -f function.yaml
You can find the public endpoint of your function and test it:
$ kubectl get function
NAME ADDRESS READY REASON
python-function-hello https://python-function-hello-mvf2bk.sebgoa.dev.triggermesh.io True
$ curl -ks -d '{"name":"seb"}' https://python-function-hello-mvf2bk.sebgoa.dev.triggermesh.io |jq
{
"id": "62402f5a-0a82-48e8-8e67-db68d57efdf9",
"type": "io.triggermesh.function.python",
"source": "source.py",
"specversion": "1.0",
"time": "2021-10-11T16:26:49Z",
"datacontenttype": "text/plain",
"data": "Hello seb"
}
Note
The returned event adheres to the CloudEvent specification.
More about Functions
Learn more about Functions on the Concepts page.