Write plugins in Python
Install
To install the plugin server and PDK globally, use :
A Kong Gateway Python plugin implementation has following attributes:
Schema = (
{ "message": { "type": "string" } },
)
version = '0.1.0'
class Plugin(object):
pass
- A class named
Plugin
defines the class that implements this plugin. - A dictionary called
Schema
that defines expected values and data types of the plugin. - The variables
version
andpriority
that define the version number and priority of execution respectively.
Phase handlers
You can implement custom logic during the following phases using the same function signature:
certificate
access
response
preread
log
The presence of the response
handler automatically enables the buffered proxy mode.
Support for is available. To use type hints and autocomplete in an IDE, add the kong
parameter to the phase handler function:
import kong_pdk.pdk.kong as kong
class Plugin(object):
self.config = config
def access(self, kong: kong.kong):
host, err = kong.request.get_header("host")
The first argument to start_dedicated_server
defines the plugin name and must be unique.
Example configuration
To load plugins using the configuration file, you have to map existing Kong Gateway properties to aspects of your plugin. Below are two examples of loading plugins within kong.conf
.
pluginserver_names = my-plugin,other-one
pluginserver_my_plugin_socket = /usr/local/kong/my-plugin.socket
pluginserver_my_plugin_start_cmd = /path/to/my-plugin.py
pluginserver_my_plugin_query_cmd = /path/to/my-plugin.py --dump
pluginserver_other_one_socket = /usr/local/kong/other-one.socket
pluginserver_other_one_start_cmd = /path/to/other-one.py
pluginserver_other_one_query_cmd = /path/to/other-one.py -dump
The socket and start command settings coincide with their defaults and can be omitted:
If your workload is IO intensive, you can use the Gevent model by passing the -g
flag to start_cmd
in kong.conf
. If your workload is CPU intensive, consider the multi-processing model by passing the -m
flag to start_cmd
in .