Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 7. Developing Python functions
OpenShift Serverless Functions with Python is a Technology Preview feature only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.
For more information about the support scope of Red Hat Technology Preview features, see Technology Preview Features Support Scope.
After you have created a Python function project, you can modify the template files provided to add business logic to your function. This includes configuring function invocation and the returned headers and status codes.
7.1. Prerequisites Copier lienLien copié sur presse-papiers!
- Before you can develop functions, you must complete the steps in Setting up OpenShift Serverless Functions.
7.2. Python function template structure Copier lienLien copié sur presse-papiers!
When you create a Python function by using the Knative (kn) CLI, the project directory looks similar to a typical Python project. 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.
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.
Both http and event trigger functions have the same template structure:
Template structure
fn ├── func.py ├── func.yaml ├── requirements.txt └── test_func.py
fn
├── func.py
├── func.yaml
├── requirements.txt
└── test_func.py
7.3. About invoking Python functions Copier lienLien copié sur presse-papiers!
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:
-
The
requestattribute is always present, and contains the Flaskrequestobject. -
The second attribute,
cloud_event, is populated if the incoming request is aCloudEventobject.
Developers can access any CloudEvent data from the context object.
Example context object
7.4. Python function return values Copier lienLien copié sur presse-papiers!
Functions can return any value supported by Flask. This is because the invocation framework proxies these values directly to the Flask server.
Example
def main(context: Context):
body = { "message": "Howdy!" }
headers = { "content-type": "application/json" }
return body, 200, headers
def main(context: Context):
body = { "message": "Howdy!" }
headers = { "content-type": "application/json" }
return body, 200, headers
Functions can set both headers and response codes as secondary and tertiary response values from function invocation.
7.4.1. Returning CloudEvents Copier lienLien copié sur presse-papiers!
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.
Example
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 is set to the returned data variable. The event_source and event_type decorator attributes are both optional.
7.5. Testing Python functions Copier lienLien copié sur presse-papiers!
You can test Python functions locally on your computer. The default project contains a test_func.py file, which provides a simple unit test for functions.
The default test framework for Python functions is unittest. You can use a different test framework if you prefer.
Prerequisites
To run Python functions tests locally, you must install the required dependencies:
pip install -r requirements.txt
$ pip install -r requirements.txtCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Procedure
-
Navigate to the folder for your function that contains the
test_func.pyfile. Run the tests:
python3 test_func.py
$ python3 test_func.pyCopy to Clipboard Copied! Toggle word wrap Toggle overflow