Envoy Data Plane API Python Dataclasses

2.0.0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to construct a basic Envoy HTTP Listener configuration using the Python dataclasses provided by the `envoy-data-plane` library. It defines an HTTP connection manager filter and wraps it in a network filter chain, then attaches it to a listener bound to port 8080. The resulting configuration is then serialized to JSON.

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))

view raw JSON →