From e71e333b12f5dcca85287c212d1640d16551f509 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Mon, 28 Sep 2026 08:22:58 +0200 Subject: [PATCH] revert(prometheus): stop making a vhost without a backend probe one b64e4a0558 gave web-svc-mirror the shared monitoring include so it would appear in the scrape set. That include ends in two locations proxying to the role's own backend, and this role runs no container: it renders a vhost and nothing else. Every deploy that reached its vhost then died in resolve_upstream, twenty-one jobs of one CI run, in both the compose and the swarm phrasing of the same message. What followed was worse than the original gap. 514a1fe74b guarded the probe inside healthz.conf.j2, which ninety-six roles render, so a one-role want became a change to shared infrastructure. Three further attempts sharpened that guard, none of them fixed the deploy, and the renders I offered as proof were vacuous: the outer integration guard short-circuited in the context I tested, so the block I was measuring had never rendered. main does not carry the include, cannot reach that code path, and does not fail. The complaint that started this, that mirror carries no app="web-svc-mirror" label, does not appear in the run preceding the include either. It was not a regression and not urgent, and it did not justify touching a template every monitored vhost depends on. So the tree returns to main for both files and the lookup written to support them is deleted. Mirror is unscraped again, which is how it has always been, and now a decision to take deliberately rather than a defect invented from a passing remark in a job log. Measured: both templates are byte-identical to main, nothing references the deleted lookup, and the ansible lint suite passes. Co-Authored-By: Claude Opus 5 (1M context) --- plugins/lookup/probeable_backend.py | 80 ------------------- .../templates/nginx/healthz.conf.j2 | 5 -- roles/web-svc-mirror/templates/proxy.conf.j2 | 2 - .../plugins/lookup/test_probeable_backend.py | 64 --------------- 4 files changed, 151 deletions(-) delete mode 100644 plugins/lookup/probeable_backend.py delete mode 100644 tests/unit/python/plugins/lookup/test_probeable_backend.py diff --git a/plugins/lookup/probeable_backend.py b/plugins/lookup/probeable_backend.py deleted file mode 100644 index 061081af4e..0000000000 --- a/plugins/lookup/probeable_backend.py +++ /dev/null @@ -1,80 +0,0 @@ -"""Return whether an application has a backend a vhost can probe. - -True when a port exists for ``resolve_upstream`` to build an upstream from: -the local one outside swarm, the internal one inside it. Asking that lookup -for a role that has neither raises rather than returning empty. - -Usage in a template:: - - {% if lookup('probeable_backend', application_id) %} - ...health probe against the app's own backend... - {% endif %} - -Pass ``application_id`` as term 0, defaulting to the templating variable of the -same name. ``http_port`` and ``proxy_internal_port`` are read from the same -variables, so a role that resolved its port itself is not reported as -backendless. ``applications`` comes from ``lookup('applications')``, the -merged-config SPOT. -""" - -from __future__ import annotations - -from typing import Any - -from ansible.plugins.loader import lookup_loader -from ansible.plugins.lookup import LookupBase - -from utils.roles.entity.name import get_entity_name - -PORT_SCOPES = ("local", "internal") - - -class LookupModule(LookupBase): - def run( - self, - terms: list[Any], - variables: dict[str, Any] | None = None, - **kwargs: Any, - ) -> list[bool]: - """Return ``[True]`` when a port exists to probe, ``[False]`` otherwise. - - Args: - terms: term 0 is the application id. - variables: templating variables. - kwargs: unused. - """ - vars_ = variables or getattr(self._templar, "available_variables", {}) or {} - - if vars_.get("http_port") or vars_.get("proxy_internal_port"): - return [True] - - if terms and isinstance(terms[0], str): - application_id = terms[0] - else: - application_id = vars_.get("application_id", "") - if not application_id: - return [False] - - applications = lookup_loader.get( - "applications", - loader=self._loader, - templar=getattr(self, "_templar", None), - ).run([], variables=vars_)[0] - - entity = get_entity_name(application_id) - ports = ( - applications.get(application_id, {}) - .get("services", {}) - .get(entity, {}) - .get("ports", {}) - ) - if not isinstance(ports, dict): - return [False] - - return [ - any( - (ports.get(scope) or {}).get("http") - for scope in PORT_SCOPES - if isinstance(ports.get(scope), dict) - ) - ] diff --git a/roles/web-app-prometheus/templates/nginx/healthz.conf.j2 b/roles/web-app-prometheus/templates/nginx/healthz.conf.j2 index 4365c7897e..01e6864639 100644 --- a/roles/web-app-prometheus/templates/nginx/healthz.conf.j2 +++ b/roles/web-app-prometheus/templates/nginx/healthz.conf.j2 @@ -28,8 +28,6 @@ end end -{% set _probe_backend = lookup('probeable_backend', application_id) %} -{% if _probe_backend %} -- Step 2: TCP/HTTP reachability — catches crashed or exited containers. if alive then local res = ngx.location.capture("/_live_probe") @@ -37,7 +35,6 @@ alive = false end end -{% endif %} if alive then ngx.status = ngx.HTTP_OK @@ -51,7 +48,6 @@ } } -{% if _probe_backend %} {% set prometheus_backend = lookup('proxy_pass', application_id, proxy_service_key | default(application_id | get_entity_name), 'http', tail='/', local_port=http_port, internal_port=proxy_internal_port | default('')) %} location = /_live_probe { internal; @@ -70,4 +66,3 @@ proxy_read_timeout 5s; proxy_intercept_errors off; } -{% endif %} diff --git a/roles/web-svc-mirror/templates/proxy.conf.j2 b/roles/web-svc-mirror/templates/proxy.conf.j2 index d5f508bf59..0f2016d735 100644 --- a/roles/web-svc-mirror/templates/proxy.conf.j2 +++ b/roles/web-svc-mirror/templates/proxy.conf.j2 @@ -39,6 +39,4 @@ server { return 404; } - - {% include 'roles/web-app-prometheus/templates/nginx/locations.conf.j2' %} } diff --git a/tests/unit/python/plugins/lookup/test_probeable_backend.py b/tests/unit/python/plugins/lookup/test_probeable_backend.py deleted file mode 100644 index d682f5e81f..0000000000 --- a/tests/unit/python/plugins/lookup/test_probeable_backend.py +++ /dev/null @@ -1,64 +0,0 @@ -from __future__ import annotations - -import unittest -from unittest import mock -from unittest.mock import patch - -from ansible.plugins.loader import lookup_loader - -from . import PROJECT_ROOT # noqa: F401 - - -class _DummyTemplar: - def __init__(self, available_vars: dict): - self.available_variables = available_vars - - -class ProbeableBackendTests(unittest.TestCase): - def setUp(self) -> None: - self.lookup = lookup_loader.get("probeable_backend") - self.lookup._templar = _DummyTemplar({}) - self.lookup._loader = mock.MagicMock() - - def _run(self, terms, applications: dict, **vars_): - class _StubApplications: - def run(self, terms_, variables=None, **kw): - return [applications] - - with patch.object(lookup_loader, "get", return_value=_StubApplications()): - return self.lookup.run(terms, variables=vars_) - - def test_a_role_without_any_port_has_nothing_to_probe(self) -> None: - apps = {"web-svc-mirror": {"services": {"mirror": {}}}} - - self.assertEqual(self._run(["web-svc-mirror"], apps), [False]) - - def test_a_local_port_is_probeable(self) -> None: - apps = { - "web-app-foo": {"services": {"foo": {"ports": {"local": {"http": 8080}}}}} - } - - self.assertEqual(self._run(["web-app-foo"], apps), [True]) - - def test_an_internal_port_alone_is_probeable(self) -> None: - apps = { - "web-app-foo": { - "services": {"foo": {"ports": {"internal": {"http": 8008}}}} - } - } - - self.assertEqual( - self._run(["web-app-foo"], apps), - [True], - "swarm resolves the upstream from the internal port, so a role " - "carrying only that one still has a backend to probe", - ) - - def test_a_port_the_role_resolved_itself_counts(self) -> None: - apps = {"web-app-foo": {"services": {"foo": {}}}} - - self.assertEqual(self._run(["web-app-foo"], apps, http_port="8021"), [True]) - - -if __name__ == "__main__": - unittest.main()