OpenFunction
The openfunction plugin makes a synchronous OpenFunction endpoint the effective upstream for a route. It forwards the client method, headers, query parameters, and body and returns the function response.
Examples
The examples show how to invoke a synchronous function and preserve a wildcard route suffix.
Install OpenFunction by following its installation guide. Deploy the official Hello World Go sample by following its Deployment instructions. The sample accepts a greeting at /<greeting> and returns Hello, <greeting>!.
Obtain the function's internal or external base URL from its status. Remove any trailing slash, then export the base URL and the exact endpoint used by the first example:
export OPENFUNCTION_BASE_URI="http://function-sample.default.svc.cluster.local"
export OPENFUNCTION_URI="${OPENFUNCTION_BASE_URI}/API7"
If the function endpoint requires HTTP Basic authentication, configure authorization.service_token. The plugin replaces any client Authorization header with the configured credentials. Protect the token in declarative workflows, use HTTPS, and restrict network access to the function endpoint.
Invoke a Synchronous Function
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes/openfunction-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"uri": "/openfunction",
"plugins": {
"openfunction": {
"function_uri": "${OPENFUNCTION_URI}"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}
EOF
services:
- name: openfunction-service
labels:
docs-example: openfunction
routes:
- name: openfunction-route
uris:
- /openfunction
plugins:
openfunction:
function_uri: "${OPENFUNCTION_URI}"
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
Preview the scoped change:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=openfunction
Review diff.yaml, then synchronize the labeled service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=openfunction
Set up the Ingress Controller and gateway in the aic namespace before continuing. Replace the function URI with the deployed sample's exact /API7 endpoint.
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: openfunction-route
spec:
plugins:
- name: openfunction
config:
function_uri: http://function-sample.default.svc.cluster.local/API7
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: openfunction-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /openfunction
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: openfunction-route
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
---
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: openfunction-route
spec:
ingressClassName: apisix
plugins:
- name: openfunction
enable: true
config:
function_uri: http://function-sample.default.svc.cluster.local/API7
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: openfunction-route
spec:
ingressClassName: apisix
http:
- name: openfunction-route
match:
paths:
- /openfunction
backends:
- serviceName: httpbin-external-domain
servicePort: 80
plugin_config_name: openfunction-route
Apply the configuration:
kubectl apply -f openfunction-ic.yaml
The configurations include a conventional fallback backend. The plugin returns the function response before that backend is contacted.
Invoke the exact function endpoint:
curl "http://127.0.0.1:9080/openfunction"
The gateway returns the function response:
Hello, API7!
Preserve a Wildcard Sub-Path
Update the route to match wildcard sub-paths. The plugin appends the matched suffix to the configured function URI.
- Admin API
- ADC
- Ingress Controller
Update the route path:
curl "http://127.0.0.1:9180/apisix/admin/routes/openfunction-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{"uri":"/openfunction/*"}'
Replace the plugin configuration with the function service base:
curl "http://127.0.0.1:9180/apisix/admin/routes/openfunction-route/plugins/openfunction" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"function_uri": "${OPENFUNCTION_BASE_URI}"
}
EOF
In adc.yaml, replace the route URI and plugin configuration:
uris:
- /openfunction/*
plugins:
openfunction:
function_uri: "${OPENFUNCTION_BASE_URI}"
Preview the updated service:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=openfunction
Review diff.yaml, then synchronize the labeled service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=openfunction
Update the existing plugin and route with the Kubernetes API used by the deployment:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: openfunction-route
spec:
plugins:
- name: openfunction
config:
function_uri: http://function-sample.default.svc.cluster.local
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: openfunction-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: PathPrefix
value: /openfunction/
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: openfunction-route
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: openfunction-route
spec:
ingressClassName: apisix
plugins:
- name: openfunction
enable: true
config:
function_uri: http://function-sample.default.svc.cluster.local
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: openfunction-route
spec:
ingressClassName: apisix
http:
- name: openfunction-route
match:
paths:
- /openfunction/*
backends:
- serviceName: httpbin-external-domain
servicePort: 80
plugin_config_name: openfunction-route
Apply the updated configuration:
kubectl apply -f openfunction-ic.yaml
Send a request whose suffix identifies the function path:
curl "http://127.0.0.1:9080/openfunction/API7"
The plugin appends /API7 to the function service base and returns the function response:
Hello, API7!