Files
computer-playbook/plugins/lookup/container_networks.py
kevinveenbirkenbach 10de64ff78 fix(networks): give MCP overlay membership a direction
A provider overlay keyed on services.mcp.enabled and .shared admitted every
MCP-capable role, servers included, because those flags carry no direction. Each
provider's network therefore collected every other provider and the result was a
mesh in which any sidecar could reach any application. The new mcp_client
consumer kind additionally requires services.mcp.direction to be client or both,
and the four provider overlays switch to it.

A sidecar rendered its networks with the same lookup as its provider and
inherited that provider's consumer attachments, which it neither needs nor
should have. own_network_only keeps it on the provider network alone. A provider
whose own topology is default_net contributes no attachment line at all, and a
bare networks: key is not a list, so the flag returns nothing in that case and
lets the sidecar fall back to the project default, which is that provider's own
network.
2026-08-06 14:27:07 +02:00

57 lines
2.0 KiB
Python

"""Lookup ``container_networks``: emit the per-service ``networks:`` attachment
block. Reads the service_registry, the rendering role's ``application_id`` and
``DEPLOYMENT_MODE`` from variables.
Usage in any service template:
{{ lookup('container_networks') | indent(4) }}
Pass ``provider_self_alias=False`` for a sidecar service that must not carry the
provider's own alias, and ``own_network_only=True`` for one that must not
inherit the networks its provider joins as a consumer.
"""
from __future__ import annotations
from typing import Any
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from utils.networks.lookup_context import build_context, resolve_var
from utils.networks.render import render_container_networks
from utils.roles.entity.name import get_entity_name
class LookupModule(LookupBase):
def run(
self,
terms: list[Any] | None,
variables: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[str]:
if terms:
raise AnsibleError("container_networks lookup takes no positional terms")
ctx = build_context(self, variables)
application_id = resolve_var(ctx.templar, ctx.vars.get("application_id"))
if not application_id:
raise AnsibleError(
"container_networks lookup: application_id is required in variables"
)
return [
render_container_networks(
application_id=str(application_id),
deployment_mode=ctx.deployment_mode,
registry=ctx.registry,
get_entity_name=get_entity_name,
lookup_config=ctx.lookup_config,
lookup_database=ctx.lookup_database,
provider_self_alias=bool(kwargs.get("provider_self_alias", True)),
node_local=bool(kwargs.get("node_local", False)),
own_network_only=bool(kwargs.get("own_network_only", False)),
)
]