|
| 1 | +// Module included in the following assemblies: |
| 2 | +// * about/serverless-logic-overview.adoc |
| 3 | + |
| 4 | + |
| 5 | +:_content-type: CONCEPT |
| 6 | +[id="serverless-logic-overview-custom-functions_{context}"] |
| 7 | += Custom functions |
| 8 | + |
| 9 | +OpenShift Serverless Logic supports the `custom` function type, which enables the implementation to extend the function definitions capability. By combining with the `operation` string, you can use a list of predefined function types. |
| 10 | + |
| 11 | +[NOTE] |
| 12 | +==== |
| 13 | +Custom function types might not be portable across other runtime implementations. |
| 14 | +==== |
| 15 | + |
| 16 | +[id="sysout-custom-function_{context}"] |
| 17 | +== Sysout custom function |
| 18 | + |
| 19 | +You can use the `sysout` function for logging, as shown in the following example: |
| 20 | + |
| 21 | +.Example of `sysout` function definition |
| 22 | +[source,json] |
| 23 | +---- |
| 24 | +{ |
| 25 | + "functions": [ |
| 26 | + { |
| 27 | + "name": "logInfo", |
| 28 | + "type": "custom", |
| 29 | + "operation": "sysout:INFO" |
| 30 | + } |
| 31 | + ] |
| 32 | +} |
| 33 | +---- |
| 34 | + |
| 35 | +The string after the `:` is optional and is used to indicate the log level. The possible values are `TRACE`, `DEBUG`, `INFO`, `WARN`, and `ERROR`. If the value is not present, `INFO` is the default. |
| 36 | + |
| 37 | +In the `state` definition, you can call the same `sysout` function as shown in the following example: |
| 38 | + |
| 39 | +.Example of a `sysout` function reference within a state |
| 40 | +[source,json] |
| 41 | +---- |
| 42 | +{ |
| 43 | + "states": [ |
| 44 | + { |
| 45 | + "name": "myState", |
| 46 | + "type": "operation", |
| 47 | + "actions": [ |
| 48 | + { |
| 49 | + "name": "printAction", |
| 50 | + "functionRef": { |
| 51 | + "refName": "logInfo", |
| 52 | + "arguments": { |
| 53 | + "message": "\"Workflow model is \\(.)\"" |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | + ] |
| 60 | +} |
| 61 | +---- |
| 62 | + |
| 63 | +In the previous example, the `message` argument can be a jq expression or a jq string using interpolation. |
| 64 | + |
| 65 | +[id="java-custom-function_{context}"] |
| 66 | +== Java custom function |
| 67 | + |
| 68 | +OpenShift Serverless Logic supports the `java` functions within an Apache Maven project, in which you define your workflow service. |
| 69 | + |
| 70 | +The following example shows the declaration of a `java` function: |
| 71 | + |
| 72 | +.Example of a `java` function declaration |
| 73 | +[source,json] |
| 74 | +---- |
| 75 | +{ |
| 76 | + "functions": [ |
| 77 | + { |
| 78 | + "name": "myFunction", <1> |
| 79 | + "type": "custom", <2> |
| 80 | + "operation": "service:java:com.acme.MyInterfaceOrClass::myMethod" <3> |
| 81 | + } |
| 82 | + ] |
| 83 | +} |
| 84 | +---- |
| 85 | + |
| 86 | +<1> `myFunction` is the function name. |
| 87 | +<2> `custom` is the function type. |
| 88 | +<3> `service:java:com.acme.MyInterfaceOrClass::myMethod` is the custom operation definition. In the custom operation definition, `service` is the reserved operation keyword, followed by the `java` keyword. `com.acme.MyInterfaceOrClass` is the FQCN (Fully Qualified Class Name) of the interface or implementation class, followed by the method name `myMethod`. |
| 89 | + |
| 90 | +//[id="camel-custom-function_{context}"] (I have commented out this section, as we have discussed with the Dev team to add this post-release) |
| 91 | +//== Camel custom function |
| 92 | +//OpenShift Serverless Logic supports the Camel Routes functions within an Apache Maven project, in which you define your workflow service. |
| 93 | +//The following example shows the declaration of a `Camel` function: |
| 94 | + |
| 95 | +//.Example of a `Camel` function declaration |
| 96 | +//[source,json] |
| 97 | +//---- |
| 98 | +//{ |
| 99 | +// "functions": [ |
| 100 | +// { |
| 101 | +// "name": "myCamelEndpoint", <1> |
| 102 | +// "type": "custom", <2> |
| 103 | +// "operation": "camel:direct:myendpoint" <3> |
| 104 | +// } |
| 105 | +// ] |
| 106 | +//} |
| 107 | +//---- |
| 108 | +
|
| 109 | +//<1> `myCamelEndpoint` is the function name. |
| 110 | +//<2> `custom` is the function type. |
| 111 | +//<3> `camel:direct:myendpoint` is the custom operation definition. In this definition, `camel` is the reserved keyword followed by the direct endpoint, and `myendpoint` is the endpoint URI name found in the route within your project. |
| 112 | +
|
| 113 | +[id="knative-custom-function_{context}"] |
| 114 | +== Knative custom function |
| 115 | +
|
| 116 | +OpenShift Serverless Logic provides an implementation of a custom function through the `knative-serving` add-on to invoke Knative services. It allows you to have a static URI, defining a Knative service, that is used to perform HTTP requests. The Knative service defined in the URI is queried in the current Knative cluster and translated to a valid URL. |
| 117 | +
|
| 118 | +The following example uses a deployed Knative service: |
| 119 | +
|
| 120 | +[source,bash] |
| 121 | +---- |
| 122 | +$ kn service list |
| 123 | +NAME URL LATEST AGE CONDITIONS READY REASON |
| 124 | +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 |
| 125 | +---- |
| 126 | +
|
| 127 | +You can declare a OpenShift Serverless Logic custom function using the Knative service name, as shown in the following example: |
| 128 | +[source,json] |
| 129 | +---- |
| 130 | + "functions": [ |
| 131 | + { |
| 132 | + "name": "greet", <1> |
| 133 | + "type": "custom", <2> |
| 134 | + "operation": "knative:services.v1.serving.knative.dev/custom-function-knative-service?path=/plainJsonFunction", <3> |
| 135 | + } |
| 136 | + ] |
| 137 | +---- |
| 138 | +
|
| 139 | +<1> `greet` is the function name. |
| 140 | +<2> `custom` is the function type. |
| 141 | +<3> In `operation`, you set the coordinates of the Knative service. |
| 142 | +
|
| 143 | +[NOTE] |
| 144 | +==== |
| 145 | +This function sends a `POST` request. If you do not specify a path, OpenShift Serverless Logic uses the root path (/). You can also send `GET` requests by setting `method=GET` in the operation. In this case, the arguments are forwarded over a query string. |
| 146 | +==== |
| 147 | +
|
| 148 | +[id="rest-custom-function_{context}"] |
| 149 | +== REST custom function |
| 150 | +
|
| 151 | +OpenShift Serverless Logic offers the `REST` custom type as a shortcut. When using custom rest, in the function definition, you specify the HTTP URI to be invoked and the HTTP method (get, post, patch, or put) to be used. This is done by using the `operation` string. When the function is invoked, you pass the request arguments as you do when using an OpenAPI function. |
| 152 | +
|
| 153 | +The following example shows the declaration of a `rest` function: |
| 154 | +
|
| 155 | +[source,json] |
| 156 | +---- |
| 157 | + { |
| 158 | + "functions": [ |
| 159 | + { |
| 160 | + "name": "multiplyAllByAndSum", <1> |
| 161 | + "type": "custom", <2> |
| 162 | + "operation": "rest:post:/numbers/{multiplier}/multiplyByAndSum" <3> |
| 163 | + } |
| 164 | + ] |
| 165 | +} |
| 166 | +---- |
| 167 | +
|
| 168 | +<1> `multiplyAllAndSum` is the function name. |
| 169 | +<2> `custom` is the function type. |
| 170 | +<3> `rest:post:/numbers/{multiplier}/multiplyByAndSum` is the custom operation definition. In the custom operation definition, `rest` is the reserved operation keyword that indicates this is a REST call, `post` is the HTTP method, and `/numbers/{multiplier}/multiplyByAndSum` is the relative endpoint. |
| 171 | +
|
| 172 | +When using the relative endpoints, you must specify the host as a property. The format of the host property is `kogito.sw.functions.<function_name>`.host. In this example, `kogito.sw.functions.multiplyAllByAndSum.host` is the host property key. You can override the default port (80) if needed by specifying the `kogito.sw.functions.multiplyAllAndSum.port` property. |
| 173 | +
|
| 174 | +This endpoint expects as body a JSON object whose field `numbers` is an array of integers, multiplies each item in the array by `multiplier` and returns the sum of all the multiplied items. |
0 commit comments