From 915bd62430e62a4e95453c082fa3af3297345c69 Mon Sep 17 00:00:00 2001 From: Luca Bello Date: Thu, 7 Dec 2023 16:26:55 +0100 Subject: [PATCH] fix nginx config when one or more roles are missing --- pyproject.toml | 2 +- src/nginx.py | 295 ++++++++++++++++++++++++++----------------------- 2 files changed, 160 insertions(+), 137 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index fea014b..ea3b304 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,7 +36,7 @@ extend-exclude = ["__pycache__", "*.egg_info"] per-file-ignores = {"tests/*" = ["D100","D101","D102","D103","D104"]} [tool.ruff.mccabe] -max-complexity = 10 +max-complexity = 15 [tool.pyright] include = ["src"] diff --git a/src/nginx.py b/src/nginx.py index f9211e5..d3e1c0d 100644 --- a/src/nginx.py +++ b/src/nginx.py @@ -11,6 +11,149 @@ logger = logging.getLogger(__name__) +LOCATIONS_DISTRIBUTOR: List[Dict] = [ + { + "directive": "location", + "args": ["/distributor"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://distributor"], + }, + ], + }, + { + "directive": "location", + "args": ["/api/v1/push"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://distributor"], + }, + ], + }, + { + "directive": "location", + "args": ["/otlp/v1/metrics"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://distributor"], + }, + ], + }, +] +LOCATIONS_ALERTMANAGER: List[Dict] = [ + { + "directive": "location", + "args": ["/alertmanager"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://alertmanager"], + }, + ], + }, + { + "directive": "location", + "args": ["/multitenant_alertmanager/status"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://alertmanager"], + }, + ], + }, + { + "directive": "location", + "args": ["/api/v1/alerts"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://alertmanager"], + }, + ], + }, +] +LOCATIONS_RULER: List[Dict] = [ + { + "directive": "location", + "args": ["/prometheus/config/v1/rules"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://ruler"], + }, + ], + }, + { + "directive": "location", + "args": ["/prometheus/api/v1/rules"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://ruler"], + }, + ], + }, + { + "directive": "location", + "args": ["/prometheus/api/v1/alerts"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://ruler"], + }, + ], + }, + { + "directive": "location", + "args": ["=", "/ruler/ring"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://ruler"], + }, + ], + }, +] +LOCATIONS_QUERY_FRONTEND: List[Dict] = [ + { + "directive": "location", + "args": ["/prometheus"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://query-frontend"], + }, + ], + }, + # Buildinfo endpoint can go to any component + { + "directive": "location", + "args": ["=", "/api/v1/status/buildinfo"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://query-frontend"], + }, + ], + }, +] +LOCATIONS_COMPACTOR: List[Dict] = [ + # Compactor endpoint for uploading blocks + { + "directive": "location", + "args": ["=", "/api/v1/upload/block/"], + "block": [ + { + "directive": "proxy_pass", + "args": ["http://compactor"], + }, + ], + }, +] + class Nginx: """Helper class to manage the nginx workload.""" @@ -44,6 +187,21 @@ def upstreams(addresses_by_role: Dict[str, Set[str]]) -> List[Dict]: return nginx_upstreams + def locations(addresses_by_role: Dict[str, Set[str]]) -> List[Dict]: + nginx_locations = [] + roles = addresses_by_role.keys() + if "distributor" in roles: + nginx_locations.extend(LOCATIONS_DISTRIBUTOR) + if "alertmanager" in roles: + nginx_locations.extend(LOCATIONS_ALERTMANAGER) + if "ruler" in roles: + nginx_locations.extend(LOCATIONS_RULER) + if "query-frontend" in roles: + nginx_locations.extend(LOCATIONS_QUERY_FRONTEND) + if "compactor" in roles: + nginx_locations.extend(LOCATIONS_COMPACTOR) + return nginx_locations + def log_verbose(verbose): if verbose: return [{"directive": "access_log", "args": ["/dev/stderr", "main"]}] @@ -142,142 +300,7 @@ def basic_auth(enabled): "directive": "proxy_set_header", "args": ["X-Scope-OrgID", "$ensured_x_scope_orgid"], }, - # Distributor endpoints - { - "directive": "location", - "args": ["/distributor"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://distributor"], - }, - ], - }, - { - "directive": "location", - "args": ["/api/v1/push"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://distributor"], - }, - ], - }, - { - "directive": "location", - "args": ["/otlp/v1/metrics"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://distributor"], - }, - ], - }, - # Alertmanager endpoints - { - "directive": "location", - "args": ["/alertmanager"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://alertmanager"], - }, - ], - }, - { - "directive": "location", - "args": ["/multitenant_alertmanager/status"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://alertmanager"], - }, - ], - }, - { - "directive": "location", - "args": ["/api/v1/alerts"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://alertmanager"], - }, - ], - }, - # Ruler endpoints - { - "directive": "location", - "args": ["/prometheus/config/v1/rules"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://ruler"], - }, - ], - }, - { - "directive": "location", - "args": ["/prometheus/api/v1/rules"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://ruler"], - }, - ], - }, - { - "directive": "location", - "args": ["/prometheus/api/v1/alerts"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://ruler"], - }, - ], - }, - { - "directive": "location", - "args": ["=", "/ruler/ring"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://ruler"], - }, - ], - }, - # Query frontend - { - "directive": "location", - "args": ["/prometheus"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://query-frontend"], - }, - ], - }, - # Buildinfo endpoint can go to any component - { - "directive": "location", - "args": ["=", "/api/v1/status/buildinfo"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://query-frontend"], - }, - ], - }, - # Compactor endpoint for uploading blocks - { - "directive": "location", - "args": ["=", "/api/v1/upload/block/"], - "block": [ - { - "directive": "proxy_pass", - "args": ["http://compactor"], - }, - ], - }, + *locations(addresses_by_role), ], }, ],