Get started with Datakit
This feature is released as a tech preview (alpha-quality) and should not be depended upon in a production environment.
Enable the WASM engine
In kong.conf
, set:
Or export the setting via an environment variable:
Reload or start Kong Gateway to apply the configuration.
Create a service and a route
Add the following configuration to a kong.yaml
file:
_format_version: "3.0"
services:
- url: http://127.0.0.1:8001/
name: my-service
routes:
- name: my-route
paths:
- /
strip_path: true
- Create a service:
curl -i -X POST http://localhost:8001/services \
--data "name=my-service" \
--data "url=http://127.0.0.1:8001/"
- Create a route:
curl -i -X POST http://localhost:8001/services/my-service/routes \
--data "name=my-route" \
--data "paths[]=/" \
--data "strip_path=true"
Enable Datakit
Let’s test out Datakit by combining responses from two third party API calls, then returning directly to the client.
In the following example, replace SERVICE_NAME|ID
with my-service
, or with your own service name:
Kong Admin API
Konnect API
Kubernetes
Declarative (YAML)
Konnect Terraform
Make the following request:
curl -X POST http://localhost:8001/services/{serviceName|Id}/plugins \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--data '
{
"name": "datakit",
"config": {
"debug": true,
"nodes": [
{
"name": "CAT_FACT",
"type": "call",
"url": "https://catfact.ninja/fact"
},
{
"name": "DOG_FACT",
"type": "call",
"url": "https://dogapi.dog/api/v1/facts"
},
{
"name": "JOIN",
"type": "jq",
"inputs": [
{
"cat": "CAT_FACT.body"
},
{
"dog": "DOG_FACT.body"
}
],
"jq": "{\n \"cat_fact\": $cat.fact,\n \"dog_fact\": $dog.facts[0]\n}\n"
},
{
"name": "EXIT",
"type": "exit",
"inputs": [
{
"body": "JOIN"
}
],
"status": 200
}
]
}
}
'
Replace SERVICE_NAME|ID
with the id
or name
of the service that this plugin configuration will target.
Make the following request, substituting your own access token, region, control plane ID, and service ID:
curl -X POST \
https://{us|eu}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/services/{serviceId}/plugins \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer TOKEN" \
--data '{"name":"datakit","config":{"debug":true,"nodes":[{"name":"CAT_FACT","type":"call","url":"https://catfact.ninja/fact"},{"name":"DOG_FACT","type":"call","url":"https://dogapi.dog/api/v1/facts"},{"name":"JOIN","type":"jq","inputs":[{"cat":"CAT_FACT.body"},{"dog":"DOG_FACT.body"}],"jq":"{\n \"cat_fact\": $cat.fact,\n \"dog_fact\": $dog.facts[0]\n}\n"},{"name":"EXIT","type":"exit","inputs":[{"body":"JOIN"}],"status":200}]}}'
See the Konnect API reference to learn about region-specific URLs and personal access tokens.
First, create a KongPlugin
resource:
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: datakit-example
plugin: datakit
config:
debug: true
nodes:
- name: CAT_FACT
type: call
url: https://catfact.ninja/fact
- name: DOG_FACT
type: call
url: https://dogapi.dog/api/v1/facts
- name: JOIN
type: jq
inputs:
- cat: CAT_FACT.body
- dog: DOG_FACT.body
jq: |
{
"cat_fact": $cat.fact,
"dog_fact": $dog.facts[0]
}
- name: EXIT
type: exit
inputs:
- body: JOIN
status: 200
" | kubectl apply -f -
Next, apply the KongPlugin
resource to an ingress by annotating the service
as follows:
kubectl annotate service SERVICE_NAME konghq.com/plugins=datakit-example
Replace SERVICE_NAME
with the name of the service that this plugin configuration will target.
You can see your available ingresses by running kubectl get service
.
Note:
The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: datakit
service: SERVICE_NAME|ID
config:
debug: true
nodes:
- name: CAT_FACT
type: call
url: https://catfact.ninja/fact
- name: DOG_FACT
type: call
url: https://dogapi.dog/api/v1/facts
- name: JOIN
type: jq
inputs:
- cat: CAT_FACT.body
- dog: DOG_FACT.body
jq: |
{
"cat_fact": $cat.fact,
"dog_fact": $dog.facts[0]
}
- name: EXIT
type: exit
inputs:
- body: JOIN
status: 200
Replace SERVICE_NAME|ID
with the id
or name
of the service that this plugin configuration will target.
Prerequisite: Configure your Personal Access Token
terraform {
required_providers {
konnect = {
source = "kong/konnect"
}
}
}
provider "konnect" {
personal_access_token = "kpat_YOUR_TOKEN"
server_url = "https://us.api.konghq.com/"
}
Add the following to your Terraform configuration to create a Konnect Gateway Plugin:
resource "konnect_gateway_plugin_datakit" "my_datakit" {
enabled = true
config = {
debug = true
nodes = [
{
name = "CAT_FACT"
type = "call"
url = "https://catfact.ninja/fact"
},
{
name = "DOG_FACT"
type = "call"
url = "https://dogapi.dog/api/v1/facts"
},
{
name = "JOIN"
type = "jq"
inputs = [
{
cat = "CAT_FACT.body"
},
{
dog = "DOG_FACT.body"
} ]
jq = <<EOF
{
"cat_fact": $cat.fact,
"dog_fact": $dog.facts[0]
}
EOF
},
{
name = "EXIT"
type = "exit"
inputs = [
{
body = "JOIN"
} ]
status = 200
} ]
}
control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
service = {
id = konnect_gateway_service.my_service.id
}
}
Validate
Access the service via the route my-route
to test Datakit:
curl http://locahost:8000/my-route
You should get a 200
response with a random fact from each fact generator called in the config:
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 432
Content-Type: application/json
Date: Fri, 06 Dec 2024 18:26:48 GMT
Server: kong/3.9.0.0-enterprise-edition
Via: 1.1 kong/3.9.0.0-enterprise-edition
X-Kong-Proxy-Latency: 744
X-Kong-Request-Id: 878618103ee7137b7c2f914e107cb454
X-Kong-Upstream-Latency: 742
{
"cat_fact": "The longest living cat on record according to the Guinness Book belongs to the late Creme Puff of Austin, Texas who lived to the ripe old age of 38 years and 3 days!",
"dog_fact": "Greyhounds can reach a speed of up to 45 miles per hour."
}