Matching API
The matching API is alpha and is currently under active development. Capabilities will be expanded over time and the configuration structures are likely to change.
Envoy makes use of a matching API to allow the various subsystems to express actions that should be performed based on incoming data.
The matching API is designed as a tree structure to allow for sublinear matching algorithms for better performance than the linear list matching as seen in Envoy’s HTTP routing. It makes heavy use of extension points to make it easy to extend to different inputs based on protocol or environment data as well as custom sublinear matchers and direct matchers.
Within supported environments (currently only HTTP filters), a wrapper proto can be used to instantiate a matching filter associated with the wrapped structure:
In the above example, we are specifying that we want to match on the incoming request header by setting the input
to and configuring the header key to use. Using the value contained by this header, the provided exact_match_map
specifies which values we care about: we’ve configured a single value () to match against. As a result, this config means that if we receive a request which contains some-header: some_value_to_match_on
as a header, the SkipFilter action will be resolved (causing the associated HTTP filter to be skipped). If no such header is present, no action will be resolved and the filter will be applied as usual.
Above is a slightly more complicated example which combines a top level tree matcher with a linear matcher. While the tree matchers provide very efficient matching, they are not very expressive. The list matcher can be used to provide a much richer matching API, and can be combined with the tree matcher in an arbitrary order. The example describes the following match logic: skip the filter if some-header: skip_filter
is present and is set to either foo
or bar
.
HTTP Filter Iteration Impact
The above example only demonstrates matching on request headers, which ends up being the simplest case due to it happening before the associated filter receives any data. Matching on other HTTP input sources is supported (e.g. response headers), but some discussion is warranted on how this works at a filter level.
Currently the match evaluation for HTTP filters does not impact control flow at all: if insufficient data is available to perform the match, callbacks will be sent to the associated filter as normal. Once sufficient data is available to match an action, this is provided to the filter. A consequence of this is that if the filter wishes to gate some behavior on a match result, it has to manage stopping the iteration on its own.
Match Tree Validation
As the match tree structure is very flexible, some filters might need to impose additional restrictions on what kind of match trees can be used. This system is somewhat inflexible at the moment, only supporting limiting the input sources to a specific set. For example, a filter might specify that it only works with request headers: in this case a match tree that attempts to match on request trailers or response headers will fail during configuration load, reporting back which data input was invalid.
This is done for example to limit the issues talked about in or to help users understand in what context a match tree can be used for a specific filter. Due to the limitations of the validations framework at the current time, it is not used for all filters.
For HTTP filters, the restrictions are specified by the filter implementation, so consult the individual filter documentation to understand whether there are restrictions in place.
For example, in the example below, the match tree could not be used with a filter that restricts the the match tree to only use HttpRequestHeaderMatchInput.