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