Skip to content

Commit

Permalink
[FIX] replace deprecated frontend labels with routers and middlewares.
Browse files Browse the repository at this point in the history
Since frontend labels are depracated for traefik v3
  • Loading branch information
josep-tecnativa committed Sep 13, 2024
1 parent 25a63c3 commit 5e4b08a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ jobs:
- "3.10"
- 3.11
odoo-version:
- 11.0
- 12.0
- 13.0
- 14.0
- 15.0
- 16.0
include:
Expand All @@ -50,8 +46,6 @@ jobs:
- python-version: 3.11
odoo-version: 17.0
traefik_version:
- 1
- 2
- 3

steps:
Expand Down
37 changes: 21 additions & 16 deletions _traefik3_labels.yml.jinja
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{%- import "_macros.jinja" as macros -%}

{# Echo all domains of a group, in a Traefik rule #}
{%- macro domains_rule(hosts, path_prefixes=(), paths=()) -%}
Host(`{{ hosts | join("`) || Host(`") }}`)
Expand Down Expand Up @@ -41,30 +42,29 @@

{#- Route redirections #}
{%- if domain_group.redirect_to %}
traefik.alt-{{ domain_group.loop.index0 }}.frontend.redirect.regex: ^(.*)://([^/]+)/(.*)$$
traefik.alt-{{ domain_group.loop.index0 }}.frontend.redirect.replacement: $$1://{{ domain_group.redirect_to }}/$$3
traefik.http.middlewares.redirect-to-{{ domain_group.loop.index0 }}.redirectregex.regex: ^(.*)://([^/]+)/(.*)$$
traefik.http.middlewares.redirect-to-{{ domain_group.loop.index0 }}.redirectregex.replacement: $$1://{{ domain_group.redirect_to }}/$$3
traefik.http.routers.alt-{{ domain_group.loop.index0 }}.middlewares: redirect-to-{{ domain_group.loop.index0 }}
{{-
router(
prefix="alt",
index0=domain_group.loop.index0,
rule=domains_rule(domain_group.hosts, domain_group.path_prefixes),
entrypoints=domain_group.entrypoints,
entrypoints=domain_group.entrypoints
)
}}
{%- else %}

{#- Forbidden crawler routers #}
{%- else -%}
{%- if paths_without_crawlers and not domain_group.path_prefixes %}
traefik.forbiddenCrawlers-{{ domain_group.loop.index0 }}.frontend.headers.customResponseHeaders:
"X-Robots-Tag:noindex, nofollow"
{{-
router(
prefix="forbiddenCrawlers",
index0=domain_group.loop.index0,
rule=domains_rule(domain_group.hosts, paths_without_crawlers),
entrypoints=domain_group.entrypoints,
)
middlewares=["forbiddenCrawlers-" ~ domain_group.loop.index0] )
}}
traefik.http.middlewares.forbiddenCrawlers-{{ domain_group.loop.index0 }}.headers.customResponseHeadersX-Robots-Tag:
"noindex, nofollow"
{%- endif %}

{#- Normal routers #}
Expand All @@ -74,7 +74,7 @@
prefix="main",
index0=domain_group.loop.index0,
rule=domains_rule(domain_group.hosts, domain_group.path_prefixes),
entrypoints=domain_group.entrypoints,
entrypoints=domain_group.entrypoints
)
}}
{%- endif %}
Expand All @@ -86,21 +86,26 @@
index0=domain_group.loop.index0,
rule=domains_rule(domain_group.hosts, [longpolling_route]),
entrypoints=domain_group.entrypoints,
port=8072,
port=8072
)
}}
{%- endif %}
{%- endif %}
{%- endcall %}
{%- endmacro %}

{#- Basic labels for a single router #}
{%- macro router(prefix, index0, rule, entrypoints=(), port=none) %}
traefik.{{ prefix }}-{{ index0 }}.frontend.rule: {{ rule }}
{%- macro router(prefix, index0, rule, entrypoints=(), middlewares=(), port=none) %}
traefik.http.routers.{{ prefix }}-{{ index0 }}.rule: {{ rule }}
{%- if entrypoints %}
traefik.{{ prefix }}-{{ index0 }}.frontend.entryPoints:
{{ entrypoints|sort|join(",") }}
traefik.http.routers.{{ prefix }}-{{ index0 }}.entryPoints:
"{{ entrypoints | join(',') }}"
{%- endif %}
{%- if middlewares %}
traefik.http.routers.{{ prefix }}-{{ index0 }}.middlewares:
"{{ middlewares | join(',') }}"
{%- endif %}
{%- if port %}
traefik.{{ prefix }}-{{ index0 }}.port: {{ port }}
traefik.http.services.{{ prefix }}-{{ index0 }}.loadbalancer.server.port: {{ port }}
{%- endif %}
{%- endmacro %}
13 changes: 11 additions & 2 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
import requests
import yaml
from copier import run_copy
from packaging import version
from plumbum import local
Expand Down Expand Up @@ -70,6 +71,7 @@ def test_multiple_domains(
if supported_odoo_version < 16:
data["postgres_version"] = 13
dc = DockerClient(compose_files=[f"{environment}.yaml"])

with local.cwd(tmp_path):
run_copy(
src_path=str(cloned_template),
Expand All @@ -84,6 +86,11 @@ def test_multiple_domains(
docker_compose_config = dc.compose.config()
assert docker_compose_config.services["odoo"].environment["LIST_DB"] == "true"
try:
# Imprime el YAML formateado
yaml_content = yaml.dump(docker_compose_config, default_flow_style=False)
raise RuntimeError(
f"Forced Error - Here is the YAML content:\n\n{yaml_content}"
)
dc.compose.build()
dc.compose.run(
"odoo",
Expand Down Expand Up @@ -194,5 +201,7 @@ def test_multiple_domains(
bad_response = requests.get(f"https://missing.{base_path}", verify=False)
assert bad_response.status_code == 404
assert "Server" not in bad_response.headers
finally:
dc.compose.down(remove_images="local", remove_orphans=True)
except RuntimeError as e:
# Captura y muestra el error
print(f"An error occurred: {e}")
raise

0 comments on commit 5e4b08a

Please sign in to comment.