The OpenFaaS REST API along with the Kubernetes CRDs (Function, Profile, JwtIssuer, Policy, Role and Secret) are the primary way to interact with functions.
Learning with the faas-cli
The faas-cli is the original client for the REST API. The source code is available on GitHub at: openfaas/faas-cli.
One of the easiest ways to develop programs for OpenFaaS is to try out the faas-cli commands that you already know, with the FAAS_DEBUG=1 environment variable set.
It'll show the HTTP Path, Method and Query String, Body and any additional headers used so that you can build your own integration.
A quick note on TLS: when you expose OpenFaaS over the Internet, you should enable HTTPS to prevent snooping and tampering.
Note that multiple namespaces are not supported in the Community Edition or Standard Edition of OpenFaaS. There may be additional constraints with the Community Edition, so make sure you review the comparison table.
The goals of this SDK are to be lightweight, and intuitive to use, whilst using the same API types that the OpenFaaS gateway and provider use from the faas-provider's type package.
You can generate a client for Node.js, Python, Java and so forth by using the OpenAPI specification available on GitHub, or you could write simple HTTP calls.
The API is designed so that you can write HTTP calls by hand and move very quickly.
The OpenFaaS REST API can be accessed from within the cluster or from outside of the cluster.
When accessing the API for development, you may want to port-forward the OpenFaaS gateway to your local machine via kubectl port-forward. This talks to the Kubernetes API server so is encrypted.
For any code you deploy within the cluster you should always use http://gateway.openfaas:8080 as the URL. This will be resolved by the Kubernetes DNS service.
FAAS_DEBUG=1 faas-cli store deploy -n alex figlet
PUT http://127.0.0.1:8080/system/functions
Content-Type: [application/json]
{"service":"figlet","image":"ghcr.io/openfaas/figlet:latest","namespace":"alex","envProcess":"figlet","labels":{},"annotations":{}}
If no namespace is specified, then the default namespace is used for the installation, this is usually going to be openfaas-fn.
Functions can also be invoked asynchronously by adding the /async-function/NAME.NAMESPACE path to the gateway URL. In this case, a HTTP POST is required with the payload in the body.
If a function is being invoked from within Kubernetes, then the following URL should be used:
http://gateway.openfaas.svc.cluster.local.:8080
Note that in some clusters, the default DNS lookup of "svc.cluster.local" may be different, adapt this as required. The final "." at the end of the line helps to prevent unnecessary DNS lookups.
To cancel an asynchronous function invocation an HTTP DELETE request can be made to the async function endpoint with the call id of the invocation that needs to be cancelled as a path parameter: /async-function/<call-id>
To update the previously deployed env function with an additional label, run a PUT request with the same payload as before, but with the additional label.
Here we've added the label com.openfaas.scale.zero which will instruct the OpenFaaS auto-scaler to scale the function to zero replicas after 2 minutes of inactivity.
Note that there are 0 replicas (desired) and 0 available replicas (Pods) because the function is scaled to zero due to having been inactive for 2 minutes.
Secrets work in much the same way as functions, however you cannot retrieve the text or binary value of a secret after it has been created for security reasons.
To see the API calls for secrets, run FAAS_DEBUG=1 faas-cli secret create for instance.
POST http://127.0.0.1:8080/system/secrets
Content-Type: [application/json]
{"name":"api-key","namespace":"alex","value":"SECRET"}
List the functions within a namespace:
FAAS_DEBUG=1 faas-cli secret list -n alex
GET http://127.0.0.1:8080/system/secrets?namespace=alex
Logs are provided in streaming JSON format and come from running Pods.
Therefore if a function is scaled to zero, there will be no logs available. To work around this, you may want to consider an alternative logging provider like Grafana Loki.
You can run FAAS_DEBUG=1 faas-cli logs env to see the API call to query the logs for a function.
You can: list, create, update and delete namespaces with the REST API.
Note the difference in URL
The path for listing namespaces (read-only) is /system/namespaces, whilst mutating a single namespace will be either: /system/namespace, or /system/namespaces/NAME.
The field for the body for mutations is name, rather than namespace.