Invoking Knative services from SonataFlow

This document describes how to call Knative services using SonataFlow custom functions. The procedure described in this document is based on the serverless-workflow-custom-function-knative example application.

For more details about the Knative custom function, see Custom functions for your SonataFlow service.

Prerequisites
Procedure
  1. Add the knative-serving add-on dependency to your workflow project.

    <dependency>
        <groupId>org.kie.kogito</groupId>
        <artifactId>kogito-addons-quarkus-knative-serving</artifactId>
    </dependency>
  2. Enable the Service Discovery feature.

  3. Discover the name of the Knative service that your workflow will invoke. In a terminal window, run the following command:

    kn service list

    You should see an output like:

    NAME                              URL                                                                      LATEST                                  AGE     CONDITIONS   READY   REASON
    custom-function-knative-service   http://custom-function-knative-service.default.10.109.169.193.sslip.io   custom-function-knative-service-00001   3h16m   3 OK / 3     True

    Save the Knative service name (custom-function-knative-service) to use it in the next step.

  4. Declare the Knative SonataFlow custom function. In the functions section of your workflow, add the following:

    {
      "name": "greet", (1)
      "type": "custom", (2)
      "operation": "knative:services.v1.serving.knative.dev/custom-function-knative-service?path=/function", (3)
    }
    1 The name of the SonataFlow function
    2 Indicates that this function is a custom one
    3 The coordinates of the service you want to invoke
  5. Invoke the declared function. In an operation state, add an action that references the function you declared in the previous step like the following:

    "actions": [
      {
        "functionRef": {
          "refName": "greet", (1)
          "arguments": { (2)
            "name": ".name"
          }
        }
      }
    ]
    1 Function’s name
    2 The payload that should be sent in the request
  6. Deploy your workflow service to Knative. For more information on how to deploy a SonataFlow SonataFlow project to Knative, see the Deploying on Kubernetes.

  7. Submit a request to the workflow service

    curl -X 'POST' \
      '<URL>/plainJsonKnativeFunction' \ (1)
      -H 'accept: */*' \
      -H 'Content-Type: application/json' \
      -d '{ "name": "Kogito" }'
    1 Replace <URL> with the URL of your deployed workflow service

    You should see an output like (id will change):

    {"id":"87cf8275-782d-4e0b-a9be-a95f95c9c190","workflowdata":{"name":"Kogito","greeting":"Greetings from Serverless Workflow, Kogito"}}

Sending as CloudEvent

Knative functions support CloudEvent as the message protocol. SonataFlow can create and post CloudEvent messages in functionRef. For more information see core/custom-functions-support.adoc#sending-cloudevents

Found an issue?

If you find an issue or any misleading information, please feel free to report it here. We really appreciate it!