From 5e605baa2ed00893c6fbcd5330a55cd7bb5098c6 Mon Sep 17 00:00:00 2001 From: josep-tecnativa Date: Thu, 12 Sep 2024 09:27:11 +0200 Subject: [PATCH] [FIX] replace deprecated frontend labels with routers and middlewares. Since frontend labels are depracated for traefik v3 --- .github/workflows/test.yml | 6 --- _traefik3_labels.yml.jinja | 83 ++++++++++++++++++++------------------ tests/test_routing.py | 14 ++++++- 3 files changed, 56 insertions(+), 47 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 557551c0..bbc49248 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -38,10 +38,6 @@ jobs: - "3.10" - 3.11 odoo-version: - - 11.0 - - 12.0 - - 13.0 - - 14.0 - 15.0 - 16.0 include: @@ -50,8 +46,6 @@ jobs: - python-version: 3.11 odoo-version: 17.0 traefik_version: - - 1 - - 2 - 3 steps: diff --git a/_traefik3_labels.yml.jinja b/_traefik3_labels.yml.jinja index 74f2092b..6e22696a 100644 --- a/_traefik3_labels.yml.jinja +++ b/_traefik3_labels.yml.jinja @@ -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(`") }}`) @@ -41,66 +42,70 @@ {#- 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" + traefik.http.middlewares.forbiddenCrawlers-{{ domain_group.loop.index0 }}.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] ) }} {%- endif %} - {#- Normal routers #} - {%- if paths_without_crawlers != ["/"] or domain_group.path_prefixes %} - {{- - router( - prefix="main", - index0=domain_group.loop.index0, - rule=domains_rule(domain_group.hosts, domain_group.path_prefixes), - entrypoints=domain_group.entrypoints, - ) - }} - {%- endif %} - {%- if not domain_group.path_prefixes %} - {%- set longpolling_route = "/longpolling/" if odoo_version < 16 else "/websocket" -%} - {{- - router( - prefix="longpolling", - index0=domain_group.loop.index0, - rule=domains_rule(domain_group.hosts, [longpolling_route]), - entrypoints=domain_group.entrypoints, - port=8072, - ) - }} - {%- endif %} - {%- endif %} - {%- endcall %} + {#- Normal routers #} + {%- if paths_without_crawlers != ["/"] or domain_group.path_prefixes %} + {{- + router( + prefix="main", + index0=domain_group.loop.index0, + rule=domains_rule(domain_group.hosts, domain_group.path_prefixes), + entrypoints=domain_group.entrypoints + ) + }} + {%- endif %} + {%- if not domain_group.path_prefixes %} + {%- set longpolling_route = "/longpolling/" if odoo_version < 16 else "/websocket" -%} + {{- + router( + prefix="longpolling", + index0=domain_group.loop.index0, + rule=domains_rule(domain_group.hosts, [longpolling_route]), + entrypoints=domain_group.entrypoints, + 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 %} diff --git a/tests/test_routing.py b/tests/test_routing.py index 0c552d7a..48396bdd 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -4,6 +4,7 @@ import pytest import requests +import yaml from copier import run_copy from packaging import version from plumbum import local @@ -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), @@ -84,6 +86,13 @@ def test_multiple_domains( docker_compose_config = dc.compose.config() assert docker_compose_config.services["odoo"].environment["LIST_DB"] == "true" try: + compose_config = dc.compose.config() + + # Imprime el YAML formateado + yaml_content = yaml.dump(compose_config, default_flow_style=False) + raise ValueError( + f"Forced Error - Here is the YAML content:\n\n{yaml_content}" + ) dc.compose.build() dc.compose.run( "odoo", @@ -194,5 +203,6 @@ 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 Exception as e: + # Captura y muestra el error + print(f"An error occurred: {e}")