forked from Tecnativa/doodba-copier-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_traefik2_labels.yml.jinja
187 lines (171 loc) · 6.63 KB
/
_traefik2_labels.yml.jinja
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
{%- import "_macros.jinja" as macros -%}
{#
Note: indentation of 6 spaces is important because that's the depth level
of container labels in a docker-compose file.
#}
{# Echo all path prefixes in a Traefik rule #}
{%- macro path_prefix_rule(path_prefixes) %}
{%- set _ns = namespace(with_slash=[], without_slash=[]) %}
{%- for prefix in path_prefixes %}
{%- if prefix.endswith("/") %}
{%- set _ns.with_slash = _ns.with_slash + [prefix] %}
{%- else %}
{%- set _ns.with_slash = _ns.with_slash + ["%s/" % prefix] %}
{%- set _ns.without_slash = _ns.without_slash + [prefix] %}
{%- endif %}
{%- endfor -%}
(PathPrefix(
{%- for path in _ns.with_slash -%}
`{{ path }}`
{%- if not loop.last %}, {% endif %}
{%- endfor -%}
)
{%- if _ns.without_slash %} || Path(
{%- for path in _ns.without_slash -%}
`{{ path }}`
{%- if not loop.last %}, {% endif %}
{%- endfor -%}
)
{%- endif -%}
)
{%- endmacro %}
{# Echo all domains of a group, in a Traefik rule #}
{%- macro domains_rule(domain_group) -%}
Host(
{%- 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 %}
{#- Basic labels for a single router #}
{%- macro router(domain_group, key, suffix, rule=none, service=none, middlewares=()) %}
traefik.http.routers.{{ key }}-{{ suffix }}-{{ domain_group.loop.index0 }}.rule:
{{ rule|default(domains_rule(domain_group), true) }}
traefik.http.routers.{{ key }}-{{ suffix }}-{{ domain_group.loop.index0 }}.service:
{{ key }}-{{ service|default("main", true) }}
{%- if domain_group.entrypoints %}
traefik.http.routers.{{ key }}-{{ suffix }}-{{ domain_group.loop.index0 }}.entrypoints:
{{ domain_group.entrypoints|sort|join(", ") }}
{%- endif %}
{%- if middlewares %}
traefik.http.routers.{{ key }}-{{ suffix }}-{{ domain_group.loop.index0 }}.middlewares:
{{ key }}-{{ middlewares|sort|join(", %s-" % key) }}
{%- endif %}
{%- if domain_group.cert_resolver %}
{#- Add TLS configuraiton #}
{%- if suffix.endswith("-secure") %}
traefik.http.routers.{{ key }}-{{ suffix }}-{{ domain_group.loop.index0 }}.tls: "true"
{%- if domain_group.cert_resolver is string %}
traefik.http.routers.{{ key }}-{{ suffix }}-{{ domain_group.loop.index0 }}.tls.certResolver:
{{ domain_group.cert_resolver }}
{%- endif %}
{#- Create TLS-only router;
HACK https://github.com/containous/traefik/issues/7235 #}
{%- else %}
{{- router(domain_group, key, "%s-secure" % suffix, rule, service, middlewares) }}
{%- endif %}
{%- endif %}
{%- endmacro %}
{%- macro common_middlewares(key, cidr_whitelist) %}
{#- Common middlewares #}
traefik.http.middlewares.{{ key }}-buffering.buffering.retryExpression:
IsNetworkError() && Attempts() < 5
traefik.http.middlewares.{{ key }}-compress.compress: "true"
? traefik.http.middlewares.{{ key }}-forbid-crawlers.headers.customResponseHeaders.X-Robots-Tag
: "noindex, nofollow"
traefik.http.middlewares.{{ key }}-forceSecure.headers.forceSTSHeader: "true"
traefik.http.middlewares.{{ key }}-forceSecure.headers.sslRedirect: "true"
{%- if cidr_whitelist %}
{#- Declare whitelist middleware #}
? traefik.http.middlewares.{{ key }}-whitelist.IPWhiteList.sourceRange
: {% for cidr in cidr_whitelist -%}
{{ cidr }}{% if not loop.last %}, {% endif %}
{%- endfor %}
{%- endif %}
{%- endmacro %}
{%- macro odoo(domain_groups_list, cidr_whitelist, key, odoo_version,
paths_without_crawlers, project_name) %}
{#- Service #}
traefik.http.services.{{ key }}-main.loadbalancer.server.port: 8069
traefik.http.services.{{ key }}-longpolling.loadbalancer.server.port: 8072
{%- call(domain_group) macros.domains_loop_grouped(domain_groups_list) %}
{#- Remember basic middlewares for this domain group #}
{%- set _ns = namespace(basic_middlewares=[]) -%}
{%- if cidr_whitelist %}
{%- set _ns.basic_middlewares = _ns.basic_middlewares + ["whitelist"] %}
{%- endif %}
{%- if domain_group.cert_resolver %}
{%- set _ns.basic_middlewares = _ns.basic_middlewares + ["forceSecure"] %}
{%- endif %}
{%- if domain_group.redirect_to %}
{#- Redirections #}
traefik.http.middlewares.{{ key }}-redirect-{{ domain_group.loop.index0 }}.redirectRegex.regex: ^(.*)://([^/]+)/(.*)$$
traefik.http.middlewares.{{ key }}-redirect-{{ domain_group.loop.index0 }}.redirectRegex.replacement: $$1://{{ domain_group.redirect_to }}/$$3
{{-
router(
domain_group=domain_group,
key=key,
suffix="redirect",
middlewares=_ns.basic_middlewares + [
"compress",
"redirect-%d" % domain_group.loop.index0,
],
)
}}
{%- else %}
{#- When removing crawlers for /, this router is the same as forbiddenCrawlers, so no need to duplicate #}
{%- if paths_without_crawlers != ["/"] or domain_group.path_prefixes %}
{#- Main router #}
{{-
router(
domain_group=domain_group,
key=key,
suffix="main",
middlewares=_ns.basic_middlewares + [
"buffering",
"compress",
],
)
}}
{%- endif %}
{#- Longpolling router #}
{%- if not domain_group.path_prefixes %}
{{-
router(
domain_group=domain_group,
key=key,
suffix="longpolling",
rule="%s && PathPrefix(`/longpolling/`)" % domains_rule(domain_group),
service="longpolling",
middlewares=_ns.basic_middlewares,
)
}}
{%- endif %}
{#- Forbidden crawlers router #}
{%- if paths_without_crawlers and not domain_group.path_prefixes %}
{{-
router(
domain_group=domain_group,
key=key,
suffix="forbiddenCrawlers",
rule="%s && %s" % (
domains_rule(domain_group),
path_prefix_rule(paths_without_crawlers),
),
middlewares=_ns.basic_middlewares + [
"buffering",
"compress",
"forbid-crawlers",
],
)
}}
{%- endif %}
{%- endif %}
{%- endcall %}
{%- endmacro %}