Skip to content

Commit 12f2519

Browse files
committed
feature: Add Documentation for CueX usage and examples
Signed-off-by: Brian Kane <briankane1@gmail.com>
1 parent 52a72ac commit 12f2519

File tree

3 files changed

+283
-0
lines changed

3 files changed

+283
-0
lines changed
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
---
2+
title: External CUE Packages
3+
---
4+
5+
# Extending CUE with External Packages
6+
7+
CueX extends the CUE templating system by allowing users to define and execute custom functions as external packages. This enables integration with external services, dynamic function execution and reusability within workflow steps.
8+
9+
With CueX external packages, you can:
10+
- Define reusable functions for workflow steps.
11+
- Dynamically fetch data from APIs, databases, or external services.
12+
- Avoid redundant logic by centralizing function definitions into reusable packages.
13+
- Implement function providers in any language, as long as they expose endpoints that conform to CueX requirements.
14+
15+
> **Note:** External packages are available for use only within `WorkflowStepDefinitions`.
16+
17+
---
18+
19+
## Structure of a Package
20+
A `Package` in CueX is a Kubernetes CustomResource (CRD) that defines an external function provider and the functions it exposes. The key attributes of `Packages` are:
21+
22+
#### metadata.name
23+
- The name of the `Package`, used to reference it in the `#provider` field.
24+
- This must match the `#provider` value within function templates.
25+
26+
#### spec.path
27+
- Defines the import path of the package within CUE definitions.
28+
- In the below example, `ext/example` means functions from this package can be referenced as:
29+
30+
```cue
31+
import "ext/utils"
32+
33+
result: utils.#ExampleFunctionName & {
34+
$params: { example_parameter: "value" }
35+
}
36+
```
37+
38+
#### spec.provider
39+
Specifies where CueX should send function calls.
40+
- `spec.provider.protocol`: Defines the communication method (currently only `"http"`).
41+
- `spec.provider.endpoint`: The root endpoint of the function provider (e.g. http://my-cuex-server:8443/api/v1)
42+
> e.g. If `#do: "api/example-fn-name"` is defined in a function, CueX will send `POST {provider_endpoint}/api/example-fn-name`.
43+
44+
#### spec.templates
45+
- Defines CUE function templates that describe available functions.
46+
- Each function includes:
47+
- `#do`: The function path that CueX will call (appended to `spec.provider.endpoint` at execution time).
48+
- `#provider`: The name of the package (must match `metadata.name`).
49+
- `$params`: The expected input format.
50+
- `$returns`: The expected output format.
51+
52+
### Example
53+
```
54+
apiVersion: cue.oam.dev/v1alpha1
55+
kind: Package
56+
metadata:
57+
name: external-package # Unique package name
58+
spec:
59+
path: ext/example # Import path for CueX to reference
60+
provider:
61+
protocol: http # Currently, only `http` is supported
62+
endpoint: http://my-cuex-server:8443 # Root endpoint of the provider service
63+
templates:
64+
utils.cue: | # The filename inside the package
65+
package example // Recommended to match the final segment of the {spec.path} value
66+
67+
#ExampleFunctionName: { // Available in Cue as example.#ExampleFunctionName
68+
#do: "api/example-fn-name" // CueX will call `{spec.provider.endpoint}/api/example-fn-name`
69+
#provider: "external-package" // Must match metadata.name
70+
$params: {
71+
example_param: string // Structure should match the parameters of the Fn
72+
}
73+
$returns: {
74+
example_result: number // Structure should match the return structure of the Fn
75+
}
76+
}
77+
```
78+
79+
---
80+
81+
## How CueX Resolves Calls
82+
For the above package, when CueX encounters:
83+
```cue
84+
import "ext/example"
85+
86+
result: example.#ExampleFunctionName & {
87+
$params: { example_param: "A string value.." }
88+
}
89+
```
90+
91+
It will:
92+
1. Look up the `#provider` (_"external-package"_).
93+
2. Identify that `#do: "api/example-fn-name"` maps to the provider’s POST _{endpoint}/api/example-fn-name_.
94+
3. Send a POST _http://my-cuex-server:8443/api/example-fn-name_ request with this payload:
95+
```json
96+
{ "example_param": "A string value.." }
97+
```
98+
4. Expect a response like:
99+
```json
100+
{ "example_result": "A returned string value.."}
101+
```
102+
5. Populate `$returns` in the CUE template with the received result.
103+
104+
---
105+
106+
## Implementing a Provider
107+
108+
External function providers can be written in any programming language, as long as they expose HTTP POST endpoints that match CueX’s expected request/response format.
109+
110+
### Provider Requirements
111+
1. **Expose an HTTP API with matching paths**: Each function must be available at an HTTP POST endpoint, and the value specified in **#do** within the CUE Package must match the request path on the provider’s service.
112+
113+
For example, if `#do: "example"` is specified in the CUE package, the provider must expose a corresponding `/example` HTTP endpoint that handles the request.
114+
115+
2. **Accept JSON input**: The request payload should be a JSON object with a structure matching `$params`.
116+
117+
3. **Return JSON output**: The response should be a JSON object with a structure matching `$returns`.
118+
119+
![cuex-usage.png](../../resources/cuex-usage.png)
120+
121+
---
122+
123+
## Example: Simple CUE Provider & Package
124+
Lets define a very simple function `"sum"` which accepts two inputs and returns the sum.
125+
The function definition will look this:
126+
```cue
127+
#Sum: {
128+
#do: "sum"
129+
#provider: "external-cuex-package"
130+
$params: {
131+
x: number
132+
y: number
133+
}
134+
$returns: {
135+
result: number
136+
}
137+
}
138+
```
139+
140+
### Implementing the Provider
141+
#### Implementing the Provider in Go
142+
Here’s an example of a very simple external CueX Provider written as a Go service.
143+
144+
It implements the logic for the `"sum"` function and exposes it at `/sum`.
145+
146+
```go
147+
package main
148+
149+
import (
150+
"encoding/json"
151+
"net/http"
152+
)
153+
154+
type input struct {
155+
X int `json:"x"` // Matches the $params structure
156+
Y int `json:"y"`
157+
}
158+
159+
type output struct {
160+
Result int `json:"result"` // Matches the $returns structure
161+
}
162+
163+
func sumHandler(w http.ResponseWriter, r *http.Request) {
164+
var in input
165+
json.NewDecoder(r.Body).Decode(&in)
166+
167+
out := output{Result: in.X + in.Y}
168+
w.Header().Set("Content-Type", "application/json")
169+
json.NewEncoder(w).Encode(out)
170+
}
171+
172+
func main() {
173+
http.HandleFunc("/sum", sumHandler) // Path must match `#do: "sum"`
174+
http.ListenAndServe(":8443", nil)
175+
}
176+
```
177+
178+
> **Note**: KubeVela provides a [simple server package](https://github.com/kubevela/pkg/blob/main/cue/cuex/externalserver/server.go) that can be utilised.
179+
180+
#### Implementing the Provider in Python
181+
For comparison, here’s the same CueX Provider implemented as a simple Python service:
182+
183+
```python
184+
from flask import Flask, request, jsonify
185+
186+
app = Flask(__name__)
187+
188+
@app.route("/sum", methods=["POST"]) # Path must match `#do: "sum"`
189+
def sum_numbers():
190+
data = request.json
191+
x = data.get("x", 0) # Extract values specified in $params
192+
y = data.get("y", 0)
193+
return jsonify({"result": x + y}) # Return JSON response as structure matching $returns
194+
195+
if __name__ == "__main__":
196+
app.run(host="0.0.0.0", port=8443)
197+
```
198+
199+
### Registering the Provider as a Package
200+
Once the provider is running, it can be registered in Kubernetes as a `Package`:
201+
202+
```
203+
apiVersion: cue.oam.dev/v1alpha1
204+
kind: Package
205+
metadata:
206+
name: external-cuex-package
207+
spec:
208+
path: ext/utils # Import path for CueX to reference
209+
provider:
210+
protocol: http
211+
endpoint: http://my-cuex-server:8443 # URL of the running provider service
212+
templates:
213+
utils.cue: |
214+
package utils
215+
216+
#Sum: { // Function name exposed to templates
217+
#do: "sum" // Function path to the provider services endpoint
218+
#provider: "external-cuex-package" // The function provider (should match `metadata.name`)
219+
$params: {
220+
x: number
221+
y: number
222+
}
223+
$returns: {
224+
result: number
225+
}
226+
}
227+
228+
```
229+
230+
### Using the Package
231+
With the Package registered, the `utils.#Sum` function is now available for use with WorkflowStepDefinition templates by importing the `ext/utils` provider.
232+
233+
```cue
234+
ˇimport (
235+
"ext/utils" // The external provider
236+
"vela/op"
237+
)
238+
239+
"example-workflow-step": {
240+
type: "workflow-step"
241+
description: "Workflow Step using external package"
242+
}
243+
244+
template: {
245+
parameter: {}
246+
247+
sum: utils.#Sum & { // The exposed package & function
248+
$params: {
249+
"x": 2,
250+
"y": 5
251+
}
252+
} @step(1)
253+
254+
msg: op.#Message & {
255+
message: "The result is \(sum.$returns.result)" // sum.$returns = { "result": 7 }
256+
} @step(2)
257+
}
258+
```
259+
260+
---
261+
262+
## Configuration Flags
263+
The following KubeVela flags control the behavior of external Package handling in CueX:
264+
265+
| Flag | Type | Default | Description |
266+
|-------------------------------------------------------|------|----------|-------------------------------------------------------------------------------|
267+
| `enable-external-package-for-default-compiler` | bool | `true` | Enables the use of external packages. |
268+
| `enable-external-package-watch-for-default-compiler` | bool | `false` | Allows automatic reloading of external packages without restarting KubeVela. |
269+
270+
---
271+
272+
## Summary
273+
274+
CueX external packages provide a **powerful, reusable, and modular** way to extend workflow templating in KubeVela. By defining external functions as Kubernetes resources, you can:
275+
276+
- Extend CueX with custom logic, like API calls or database queries.
277+
- Reuse functions across multiple workflow steps, reducing duplication.
278+
- Dynamically update functions without modifying individual workflow definitions.
279+
- Maintain consistency across teams by centralizing function logic.
280+
- Implement function providers in any language, as long as they expose HTTP endpoints matching CueX’s requirements.
281+
282+
With external packages, CueX makes it easier to build scalable, maintainable, and extensible workflow steps in KubeVela.

docs/resources/cuex-usage.png

891 KB
Loading

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ module.exports = {
321321
},
322322
'platform-engineers/system-operation/velaql',
323323
'platform-engineers/x-def-version',
324+
'platform-engineers/cue/external-packages'
324325
],
325326
},
326327
{

0 commit comments

Comments
 (0)