Envoy Data Plane API Python Dataclasses
The `envoy-data-plane` library provides Python dataclasses that represent the Envoy Data-Plane API. It is designed to be a helper for generating Envoy configuration using Python scripts, offering benefits like IDE autocompletion and basic validation for custom Envoy control planes. The library is a conversion of the official `envoyproxy/data-plane-api` definitions into Python dataclasses, typically leveraging a tool like `betterproto`.
Warnings
- breaking Envoy's xDS API v2 was deprecated in Envoy 1.17 and removed in Q1 2021. This library exclusively uses the v3 API definitions. Attempting to use v2 API concepts or older import paths will result in errors.
- gotcha This library provides Python *dataclass representations* of the Envoy Data Plane API. It is not a control plane implementation itself. Users are responsible for building their own control plane logic to generate, manage, and deliver this configuration to Envoy proxies.
- gotcha The `envoyproxy/data-plane-api` GitHub repository, from which this library is derived, is a read-only mirror of the API definitions within the main `envoyproxy/envoy` repository. Updates to the Python library are dependent on its maintainers tracking and converting upstream changes.
- gotcha The library typically uses `betterproto` for dataclass generation, meaning the objects often include methods like `to_dict()` and `to_json_string()` for serialization. Be aware of these methods for converting dataclass objects into a format suitable for Envoy, rather than expecting direct `protobuf` message handling.
Install
-
pip install envoy-data-plane
Imports
- Listener
from envoy.config.listener.v3.listener import Listener
- RouteConfiguration
from envoy.config.route.v3.route import RouteConfiguration
Quickstart
from envoy.config.listener.v3.listener import Listener
from envoy.config.listener.v3.listener_components import Filter, FilterChain
from envoy.extensions.filters.network.http_connection_manager.v3.http_connection_manager import HttpConnectionManager
# Define a simple HTTP connection manager filter
http_filter = HttpConnectionManager(
stat_prefix="ingress_http",
route_specifier=HttpConnectionManager.RouteSpecifier(route_config_name="local_route")
)
# Define a network filter chain with the HTTP connection manager
filter_chain = FilterChain(
filters=[
Filter(
name="envoy.filters.network.http_connection_manager",
typed_config=http_filter.to_json_string()
)
]
)
# Define an HTTP listener
listener = Listener(
name="listener_0",
address=Listener.Address(
socket_address=Listener.Address.SocketAddress(
address="0.0.0.0",
port_value=8080
)
),
filter_chains=[filter_chain]
)
# Print the generated Envoy Listener configuration (usually serialized to YAML or JSON for Envoy)
import json
print(json.dumps(listener.to_dict(), indent=2))