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 12, 2024
1 parent 25a63c3 commit a471d11
Showing 1 changed file with 49 additions and 42 deletions.
91 changes: 49 additions & 42 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 @@ -38,69 +39,75 @@
{%- macro odoo(domain_groups_list, paths_without_crawlers, odoo_version, traefik_version) %}
traefik.domain: {{ macros.first_main_domain(domain_groups_list)|tojson }}
{%- call(domain_group) macros.domains_loop_grouped(domain_groups_list) %}

{#- 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.routers.alt-{{ domain_group.loop.index0 }}.rule:
{{ domains_rule(domain_group.hosts, domain_group.path_prefixes) }}
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 #}
{%- if paths_without_crawlers and not domain_group.path_prefixes %}
traefik.forbiddenCrawlers-{{ domain_group.loop.index0 }}.frontend.headers.customResponseHeaders:
"X-Robots-Tag:noindex, nofollow"
{%- else -%}
{%- if paths_without_crawlers and not domain_group.path_prefixes %}
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 %}
{%- 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 %}

0 comments on commit a471d11

Please sign in to comment.