Skip to content

Commit d934732

Browse files
committed
[srvls][SRVOCF-278] Adding Python functions docs
1 parent 4ad9f80 commit d934732

11 files changed

+179
-19
lines changed

_topic_map.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,15 +2942,17 @@ Topics:
29422942
- Name: Using Apache Kafka with OpenShift Serverless
29432943
File: serverless-kafka
29442944
# Functions - uncomment at tech preview
2945-
#- Name: Functions
2946-
# Dir: functions
2945+
# - Name: OpenShift Serverless Functions
2946+
# Dir: functions
29472947
# Topics:
2948-
# - Name: About Functions
2948+
# - Name: About OpenShift Serverless Functions
29492949
# File: serverless-functions-about
2950-
# - Name: Setting up Functions
2951-
# File: serverless-functions-setup
2952-
# - Name: Getting started with Functions
2950+
# - Name: Getting started
29532951
# File: serverless-functions-getting-started
2952+
# - Name: Setting up OpenShift Serverless Functions
2953+
# File: serverless-functions-setup
2954+
# - Name: Developing Python functions
2955+
# File: serverless-developing-python-functions
29542956
# Networking
29552957
- Name: Networking
29562958
Dir: networking

modules/serverless-create-func-kn.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ envVars: {}
2828

2929
.Procedure
3030

31-
* Create a {FunctionsProductShortName} project:
31+
* Create a function project:
3232
+
3333
[source,terminal]
3434
----

modules/serverless-document-attributes.adoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
:ServerlessProductShortName: Serverless
1212
:ServerlessOperatorName: OpenShift Serverless Operator
1313
:FunctionsProductName: OpenShift Serverless Functions
14-
:FunctionsProductShortName: Functions
1514
//
1615
// Documentation publishing attributes used in the master-docinfo.xml file
1716
// Note that the DocInfoProductName generates the URL for the product page.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Module included in the following assemblies
2+
//
3+
// * /serverless/functions/serverless-developing-python-functions.adoc
4+
5+
[id="serverless-invoking-python-functions_{context}"]
6+
= About invoking Python functions
7+
8+
Python functions can be invoked with a simple HTTP request. When an incoming request is received, functions are invoked with a `context` object as the first parameter. The `context` object is a Python class with two attributes:
9+
10+
* The `request` attribute is always present, and contains the Flask `request` object.
11+
* The second attribute, `cloud_event`, is populated if the incoming request is a `CloudEvent`.
12+
13+
Developers can access any `CloudEvent` data from the context object.
14+
15+
.Example context object
16+
[source,python]
17+
----
18+
def main(context: Context):
19+
"""
20+
The context parameter contains the Flask request object and any
21+
CloudEvent received with the request.
22+
"""
23+
print(f"Method: {context.request.method}")
24+
print(f"Event data {context.cloud_event.data}")
25+
# ... business logic here
26+
----
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Module included in the following assemblies
2+
//
3+
// * /serverless/functions/serverless-developing-python-functions.adoc
4+
5+
[id="serverless-python-function-return-values_{context}"]
6+
= Python function return values
7+
8+
Functions can return any value supported by https://flask.palletsprojects.com/en/1.1.x/quickstart/#about-responses[Flask] because the invocation framework proxies these values directly to the Flask server.
9+
10+
.Example
11+
[source,python]
12+
----
13+
def main(context: Context):
14+
body = { "message": "Howdy!" }
15+
headers = { "content-type": "application/json" }
16+
return body, 200, headers
17+
----
18+
19+
Functions can set both headers and response codes as secondary and tertiary response values from function invocation.
20+
21+
[id="serverless-python-function-return-values-returning-events_{context}"]
22+
== Returning CloudEvents
23+
24+
Developers can use the `@event` decorator to tell the invoker that the function return value must be converted to a CloudEvent before sending the response.
25+
26+
.Example
27+
[source,python]
28+
----
29+
@event("event_source"="/my/function", "event_type"="my.type")
30+
def main(context):
31+
# business logic here
32+
data = do_something()
33+
# more data processing
34+
return data
35+
----
36+
37+
This example sends a CloudEvent as the response value, with a type of `"my.type"` and a source of `"/my/function"`. The CloudEvent [`data` property](https://github.com/cloudevents/spec/blob/v1.0.1/spec.md#event-data) is set to the returned `data` variable. The `event_source` and `event_type` decorator attributes are both optional.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Module included in the following assemblies
2+
//
3+
// * /serverless/functions/serverless-developing-python-functions.adoc
4+
5+
[id="serverless-python-template_{context}"]
6+
= Python function template structure
7+
8+
When you create a Python function by using the `kn func` CLI, the project directory looks similar to a typical Python project.
9+
10+
Python functions have very few restrictions. The only requirements are that your project contains a `func.py` file that contains a `main()` function, and a `func.yaml` configuration file.
11+
12+
Developers are not restricted to the dependencies provided in the template `requirements.txt` file. Additional dependencies can be added as they would be in any other Python project. When the project is built for deployment, these dependencies will be included in the created runtime container image.
13+
14+
Both `http` and `event` trigger functions have the same template structure:
15+
16+
.Template structure
17+
[source,terminal]
18+
----
19+
fn
20+
├── func.py <1>
21+
├── func.yaml <2>
22+
├── requirements.txt <3>
23+
└── test_func.py <4>
24+
----
25+
<1> Contains a `main()` function.
26+
<2> Used to determine the image name and registry.
27+
<3> Additional dependencies can be added to the `requirements.txt` file as they are in any other Python project.
28+
<4> Contains a simple unit test that can be used to test your function locally.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Module included in the following assemblies
2+
//
3+
// * /serverless/functions/serverless-developing-python-functions.adoc
4+
5+
[id="serverless-testing-python-functions_{context}"]
6+
= Testing Python functions
7+
8+
Python functions can be tested locally on your computer. The default project contains a `test_func.py` file, which provides a simple unit test for functions.
9+
10+
.Prerequisites
11+
12+
* To run Python functions tests locally, you must install the required dependencies:
13+
+
14+
[source,terminal]
15+
----
16+
$ pip install -r requirements.txt
17+
----
18+
19+
.Procedure
20+
21+
. After you have installed the dependencies, run the tests:
22+
+
23+
[source,terminal]
24+
----
25+
$ python3 test_func.py
26+
----
27+
28+
[NOTE]
29+
====
30+
The default test framework for Python functions is `unittest`. You can use a different test framework if you prefer.
31+
====
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
include::modules/serverless-document-attributes.adoc[]
2+
[id="serverless-developing-python-functions"]
3+
= Developing Python functions
4+
:context: serverless-developing-python-functions
5+
include::modules/common-attributes.adoc[]
6+
7+
toc::[]
8+
9+
:FeatureName: {FunctionsProductName}
10+
include::modules/technology-preview.adoc[leveloffset=+2]
11+
12+
After you have xref:../../serverless/functions/serverless-functions-getting-started.adoc#serverless-functions-getting-started[created a Python function project], you can modify the template files provided to add business logic to your function.
13+
14+
[id="prerequisites_serverless-developing-python-functions"]
15+
== Prerequisites
16+
17+
* Before you can develop functions, you must complete the set up steps in xref:../../serverless/functions/serverless-functions-setup.adoc#serverless-functions-setup[Setting up {FunctionsProductName}].
18+
19+
include::modules/serverless-python-template.adoc[leveloffset=+1]
20+
include::modules/serverless-invoking-python-functions.adoc[leveloffset=+1]
21+
include::modules/serverless-python-function-return-values.adoc[leveloffset=+1]
22+
include::modules/serverless-testing-python-functions.adoc[leveloffset=+1]
23+
24+
[id="next-steps_serverless-developing-python-functions"]
25+
== Next steps
26+
27+
* xref:../../serverless/functions/serverless-functions-getting-started.adoc#serverless-functions-getting-started[Build and deploy a function].

serverless/functions/serverless-functions-about.adoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
include::modules/serverless-document-attributes.adoc[]
22
[id="serverless-functions-about"]
3-
= About {FunctionsProductShortName}
3+
= About {FunctionsProductName}
44
:context: serverless-functions-about
55
include::modules/common-attributes.adoc[]
66

@@ -20,7 +20,12 @@ The `kn func` CLI is provided as a plug-in for the Knative `kn` CLI. {FunctionsP
2020

2121
// add xref links to docs once added
2222
* Node.js
23-
* Python
23+
* xref:../../serverless/functions/serverless-developing-python-functions.adoc#serverless-developing-python-functions[Python]
2424
* Golang
2525
* Quarkus
2626
//* SpringBoot - TBC
27+
28+
[id="next-steps_serverless-functions-about"]
29+
== Next steps
30+
31+
* See xref:../../serverless/functions/serverless-functions-getting-started.adoc#serverless-functions-getting-started[Getting started].

serverless/functions/serverless-functions-getting-started.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
include::modules/serverless-document-attributes.adoc[]
22
[id="serverless-functions-getting-started"]
3-
= Getting started with {FunctionsProductShortName}
3+
= Getting started
44
:context: serverless-functions-getting-started
55
include::modules/common-attributes.adoc[]
66

@@ -12,10 +12,10 @@ include::modules/technology-preview.adoc[leveloffset=+2]
1212
This guide explains how you can create and manage a function on an {ServerlessProductName} installation by using the `kn` CLI.
1313
// TODO: add info about developer console at 4.8 when this is supported.
1414

15-
[id="prerequisites_serverless-functions-getting-started]
15+
[id="prerequisites_serverless-functions-getting-started"]
1616
== Prerequisites
1717

18-
Before you can complete the following procedures, you must ensure that you have completed all of the prerequisite tasks in xref:../../serverless/functions/serverless-functions-setup.adoc#serverless-functions-setup[Setting up {FunctionsProductShortName}].
18+
Before you can complete the following procedures, you must ensure that you have completed all of the prerequisite tasks in xref:../../serverless/functions/serverless-functions-setup.adoc#serverless-functions-setup[Setting up {FunctionsProductName}].
1919

2020
include::modules/serverless-create-func-kn.adoc[leveloffset=+1]
2121
include::modules/serverless-build-func-kn.adoc[leveloffset=+1]

0 commit comments

Comments
 (0)