Skip to content

Commit 1f26b93

Browse files
authored
Fix e2e tutorial solution files and documentation. (#6429)
Both function templates are updated to reflect the most recent version of the function cli. The code was also updated to build and execute. The `solution/setup.sh` was removed because it is empty. Mentioned Kind/Minikube with a link to the local registry setup docs in `solution/README.md`. This is needed for Camel K operator installation. Add corrected Camel K operator installation to `slack-sink/README.md` and to `solution.sh`. Also remove `kamel` cli installation check. Fixed typo and whitespace in the Javascript code of the node-server. Signed-off-by: Stanislav Jakuschevskij <[email protected]>
1 parent 6087691 commit 1f26b93

File tree

29 files changed

+460
-590
lines changed

29 files changed

+460
-590
lines changed

code-samples/eventing/bookstore-sample-app/solution/ML-bad-word-filter/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
# generally not be tracked in source control. To instruct the system to track
44
# .func in source control, comment the following line (prefix it with '# ').
55
/.func
6+
/.s2i

code-samples/eventing/bookstore-sample-app/solution/ML-bad-word-filter/Procfile

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Python CloudEvents Function
2+
3+
Welcome to your new Python Function! A minimal Function implementation can
4+
be found in `./function/func.py`.
5+
6+
For more, run `func --help` or read the [Python Functions doc](https://github.com/knative/func/blob/main/docs/function-templates/python.md)
7+
8+
## Usage
9+
10+
Knative Functions allow for the deployment of your source code directly to a
11+
Kubernetes cluster with Knative installed.
12+
13+
### Function Structure
14+
15+
Python functions must implement a `new()` method that returns a function
16+
instance. The function class can optionally implement:
17+
- `handle()` - Process CloudEvent requests
18+
- `start()` - Initialize the function with configuration
19+
- `stop()` - Clean up resources when the function stops
20+
- `alive()` / `ready()` - Health and readiness checks
21+
22+
See the default implementation in `./function/func.py`
23+
24+
### Running Locally
25+
26+
Use `func run` to test your Function locally before deployment.
27+
For development environments where Python is installed, it's suggested to use
28+
the `host` builder as follows:
29+
30+
```bash
31+
# Run function on the host (outsdie of a container)
32+
func run --builder=host
33+
```
34+
35+
### Deploying
36+
37+
Use `func deploy` to deploy your Function to a Knative-enabled cluster:
38+
39+
```bash
40+
# Deploy with interactive prompts (recommended for first deployment)
41+
func deploy --registry ghcr.io/myuser
42+
```
43+
44+
Functions are automatically built, containerized, and pushed to a registry
45+
before deployment. Subsequent deployments will update the existing function.
46+
47+
## Roadmap
48+
49+
Our project roadmap can be found: https://github.com/orgs/knative/projects/49
50+

code-samples/eventing/bookstore-sample-app/solution/ML-bad-word-filter/app.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

code-samples/eventing/bookstore-sample-app/solution/ML-bad-word-filter/func.py

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
# $schema: https://raw.githubusercontent.com/knative/func/d04ff0a3/schema/func_yaml-schema.json
2+
# yaml-language-server: $schema=https://raw.githubusercontent.com/knative/func/d04ff0a3/schema/func_yaml-schema.json
13
specVersion: 0.36.0
24
name: bad-word-filter
35
runtime: python
4-
created: 2024-03-27T23:12:06.178272+08:00
6+
registry: localhost:5000
7+
namespace: default
8+
created: 2025-09-20T18:38:53.634514281+02:00
9+
invoke: cloudevent
510
build:
611
builder: s2i
712
deploy:
813
namespace: default
14+
image: localhost:5000/bad-word-filter@sha256:690a4eb5aaae56dfbfabcb8a3f0943a48b97851e2b90ce40397091bb0866655c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .func import new
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
from cloudevents.http import CloudEvent
3+
from profanity_check import predict
4+
5+
def new():
6+
return Function()
7+
8+
class Function:
9+
async def handle(self, scope, receive, send):
10+
""" Handle all HTTP requests to this Function. The incoming CloudEvent is in scope["event"]. """
11+
logging.info("Request Received")
12+
13+
# 1. Extract the CloudEvent from the scope
14+
request_event = scope["event"]
15+
16+
# 2. Extract the data payload from the event, analyze and create CloudEvent
17+
response_event = self.inappropriate_language_filter(request_event.data)
18+
19+
# 3. Send the response
20+
logging.info(f"Sending response: {response_event.data}")
21+
await send(response_event)
22+
23+
def create_cloud_event(self, inputText, data):
24+
attributes = {
25+
"type": "new-review-comment",
26+
"source": "book-review-broker",
27+
"datacontenttype": "application/json",
28+
"badwordfilter": data,
29+
}
30+
31+
# Put the bad word filter result into a dictionary
32+
data = {"reviewText": inputText, "badWordResult": data}
33+
34+
# Create a CloudEvent object
35+
return CloudEvent(attributes, data)
36+
37+
def inappropriate_language_filter(self, text):
38+
review_text = text.get("reviewText", "")
39+
profanity_result = predict([review_text])
40+
result = "good"
41+
if profanity_result[0] == 1:
42+
result = "bad"
43+
44+
return self.create_cloud_event(review_text, result)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[project]
2+
name = "function"
3+
description = ""
4+
version = "0.1.0"
5+
requires-python = ">=3.9"
6+
readme = "README.md"
7+
license = "MIT"
8+
dependencies = [
9+
"httpx",
10+
"cloudevents",
11+
"pytest",
12+
"pytest-asyncio",
13+
"alt-profanity-check==1.4.1.post1"
14+
]
15+
authors = [
16+
{ name="Your Name", email="[email protected]"},
17+
]
18+
19+
[build-system]
20+
requires = ["hatchling"]
21+
build-backend = "hatchling.build"
22+
23+
[tool.pytest.ini_options]
24+
asyncio_mode = "strict"
25+
asyncio_default_fixture_loop_scope = "function"
26+
27+

code-samples/eventing/bookstore-sample-app/solution/ML-bad-word-filter/requirements.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)