Last updated Jan 2, 2025

Filtering context passed into templates

Background

Previously, node matching was used as a part of sources which are now deprecated.

When Sovereign received a discovery request, it would use a configured "node match key" to look at a value inside the nested discovery request.

It would then compare this value with a value in each item in the data retrieved via the source, using a configured "source match key".

If the two values matched, the item would be included in the context passed into templates.

Problems

This filtering and matching is computationally expensive, and makes caching difficult.

For this reason we now advise using a template context plugin.

Example

You need to do a few things to replicate the same behavior

  1. Retrieve the same data from the previously used source (example: a web api)
  2. Iterate over each item, in the plugin, grouping the items into a dictionary using the previous source match key
  3. In templates, use the previous node match key to access the correct group in the aforementioned dictionary
1
2
import requests
from typing import Any
from collections import defaultdict
from sovereign.dynamic_config.loaders import CustomLoader


class GroupedInstances(CustomLoader):
    def load(self, path: str) -> Any:
        ret = list()
        source_match_key = "service_clusters"
        response = requests.get(path).json()
        groups = defaultdict(list)
        for item in response:
            clusters = item[source_match_key]

            if len(clusters) == 0:
                raise ValueError("Cant group this item!")
            elif len(clusters) == 1:
                group = clusters[0]
                groups[group].append(item)
            else:
                for group in clusters:
                    groups[group].append(item)
        return ret

and then in your template

1
2
def call(discovery_request, items, **kwargs):
    node_key = discovery_request.node.cluster
    filtered = items[node_key]
    for item in filtered:
        yield ...

Rate this page: