Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] Add option to use traefik 3.0 #455

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions _traefik2_labels.yml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@
{%- endif %}
{%- endmacro %}

{# Same on HostSNI #}
{%- macro domains_rule_sni(domain_group) -%}
HostSNI(
{%- for host in domain_group.hosts -%}
`{{ host }}`
{%- if not loop.last %}, {% endif %}
{%- endfor -%}
)
{%- if domain_group.path_prefixes %} && {{ path_prefix_rule(domain_group.path_prefixes) }}
{%- endif %}
{%- endmacro %}

{%- macro key(project_name, odoo_version, suffix) %}
{{- '%s-%.1f-%s'|format(project_name, odoo_version, suffix)|replace('.', '-') }}
{%- endmacro %}
Expand Down Expand Up @@ -211,8 +223,11 @@

{%- macro database(domain_groups_list, cidr_whitelist, key, port, project_name) %}
{#- Service #}
traefik.tcp.routers.{{ key }}-database.entrypoints: postgres-entrypoint
traefik.tcp.services.{{ key }}-database.loadbalancer.server.port: 5432

traefik.{{ key }}-database.port: 5432
traefik.tcp.routers.{{ key }}-database.tls: "true"
traefik.tcp.routers.{{ key }}-database.tls.certResolver: letsencrypt
{%- if cidr_whitelist %}
{#- Declare whitelist middleware #}
? traefik.tcp.middlewares.{{ key }}-whitelist.IPWhiteList.sourceRange
Expand All @@ -222,22 +237,12 @@
{%- endif %}

{%- call(domain_group) macros.domains_loop_grouped(domain_groups_list) %}
traefik.tcp.routers.{{ key }}-database.rule: {{ domains_rule_sni(domain_group) }}

{#- Remember basic middlewares for this domain group #}
{%- set _ns = namespace(basic_middlewares=[]) -%}
{%- if cidr_whitelist %}
{%- set _ns.basic_middlewares = _ns.basic_middlewares + ["whitelist"] %}
{%- endif %}

{#- database router #}
{{-
router_tcp(
domain_group=domain_group,
key=key,
suffix="database",
service="database",
middlewares=_ns.basic_middlewares,
port=port,
)
}}
{%- endcall %}
{%- endmacro %}
108 changes: 108 additions & 0 deletions _traefik3_labels.yml.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{%- 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(`") }}`)
{%- if path_prefixes or paths -%}
{{" "}}&& (
{%- if path_prefixes -%}
{{ path_prefix_rule(path_prefixes) }}
{%- if paths %} || {% endif %}
{%- endif %}
{%- if paths -%}
{{ path_rule(paths) }}
{%- endif -%}
)
{%- endif %}
{%- endmacro %}

{# Echo all path prefixes in a Traefik rule #}
{%- macro path_prefix_rule(path_prefixes) -%}
{%- for path in path_prefixes -%}
{%- if path.endswith("/") -%}
PathPrefix(`{{ path }}`)
{%- else -%}
PathPrefix(`{{ path }}`) || Path(`{{ path }}`)
{%- endif -%}
{%- if not loop.last %} || {% endif %}
{%- endfor %}
{%- endmacro %}

{# Echo all paths in a Traefik rule #}
{%- macro path_rule(paths) -%}
{%- for path in paths -%}
Path(`{{ path }}`)
{%- if not loop.last %} || {% endif %}
{%- endfor %}
{%- endmacro %}

{%- 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
{{-
router(
prefix="alt",
index0=domain_group.loop.index0,
rule=domains_rule(domain_group.hosts, domain_group.path_prefixes),
entrypoints=domain_group.entrypoints,
)
}}
{%- else %}

{#- Forbidden crawler routers #}
{%- if traefik_version < 3%}
{%- 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,
)
}}
{%- 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 %}
{%- endmacro %}
{#- Basic labels for a single router #}
{%- macro router(prefix, index0, rule, entrypoints=(), port=none) %}
traefik.{{ prefix }}-{{ index0 }}.frontend.rule: {{ rule }}
{%- if entrypoints %}
traefik.{{ prefix }}-{{ index0 }}.frontend.entryPoints:
{{ entrypoints|sort|join(",") }}
{%- endif %}
{%- if port %}
traefik.{{ prefix }}-{{ index0 }}.port: {{ port }}
{%- endif %}
{%- endmacro %}
11 changes: 11 additions & 0 deletions copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ odoo_proxy:

Which proxy will you use to deploy odoo?

traefik_version:
default: 3
type: int
when: &odoo_proxy_specific "{{ odoo_proxy == 'traefik' }}"
help: >-
Indicate traefik version
choices:
- 1
- 2
- 3

odoo_initial_lang:
default: en_US
type: str
Expand Down
1 change: 1 addition & 0 deletions docs/scaffolding2copier.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ copier $CUSTOM_COPIER_FLAGS \
-d smtp_relay_user="$SMTP_REAL_RELAY_USER" \
-d smtp_canonical_default="$SMTP_REAL_NON_CANONICAL_DEFAULT" \
-d smtp_canonical_domains="[$SMTP_REAL_CANONICAL_DOMAINS]" \
-d traefik_version="$TRAEFIK_VERSION" \
-d backup_dst="boto3+s3://$BACKUP_S3_BUCKET" \
-d backup_email_from="$BACKUP_EMAIL_FROM" \
-d backup_email_to="$BACKUP_EMAIL_TO" \
Expand Down
4 changes: 3 additions & 1 deletion prod.yaml.jinja
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{%- import "_macros.jinja" as macros -%}
{%- import "_traefik1_labels.yml.jinja" as traefik1_labels -%}
{%- import "_traefik2_labels.yml.jinja" as traefik2_labels -%}
{%- import "_traefik3_labels.yml.jinja" as traefik3_labels -%}

{%- set _key = traefik2_labels.key(project_name, odoo_version, "prod") -%}
version: "2.4"

Expand Down Expand Up @@ -37,7 +39,7 @@ services:
doodba.domain.main: {{ macros.first_main_domain(domains_prod)|tojson }}
{%- if odoo_proxy == "traefik" and domains_prod %}
traefik.enable: "true"
{{- traefik1_labels.odoo(domains_prod, paths_without_crawlers, odoo_version) }}
{{- (traefik3_labels.odoo(domains_prod, paths_without_crawlers, odoo_version, traefik_version) if traefik_version == 3 else traefik1_labels.odoo(domains_prod, paths_without_crawlers, odoo_version)) }}
{{- traefik2_labels.common_middlewares(_key, cidr_whitelist) }}
{{- traefik2_labels.odoo(
domains_prod,
Expand Down
1 change: 1 addition & 0 deletions test.yaml.jinja
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{%- import "_macros.jinja" as macros -%}
{%- import "_traefik1_labels.yml.jinja" as traefik1_labels -%}
{%- import "_traefik2_labels.yml.jinja" as traefik2_labels -%}
{%- import "_traefik3_labels.yml.jinja" as traefik3_labels -%}
{%- set _key = traefik2_labels.key(project_name, odoo_version, "test") -%}
version: "2.4"

Expand Down
Loading