Skip to content

Commit

Permalink
fix nginx config when one or more roles are missing (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucabello authored Dec 7, 2023
1 parent a21d735 commit 45ec7be
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 137 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
295 changes: 159 additions & 136 deletions src/nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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"]}]
Expand Down Expand Up @@ -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),
],
},
],
Expand Down

0 comments on commit 45ec7be

Please sign in to comment.