Skip to content

Commit

Permalink
fix: router service pointer to allow multiple endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Morriz committed Mar 27, 2024
1 parent 7e70e99 commit a417e7e
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 57 deletions.
1 change: 0 additions & 1 deletion db.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ projects:
env:
MINIO_ROOT_USER: root
MINIO_ROOT_PASSWORD: 83b01a6b8f210b5f5862943f3ebe257d
MINIO_DEFAULT_BUCKETS: ai-assistant
image: minio/minio:latest
ingress:
- domain: minio-api.example.com
Expand Down
6 changes: 3 additions & 3 deletions lib/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ def write_terminate() -> None:


def write_routers() -> None:
projects_tcp = get_projects(filter=lambda _, _2, i: i.protocol == Protocol.tcp)
projects_passthrough = get_projects(filter=lambda _, s, i: i.passthrough or not s.image)
with open("proxy/tpl/routers-web.yml.j2", encoding="utf-8") as f:
t = f.read()
tpl_routers_web = Template(t)
domain = os.environ.get("TRAEFIK_DOMAIN")
routers_web = tpl_routers_web.render(
projects=projects_tcp,
projects=projects_passthrough,
traefik_rule=f"Host(`{domain}`)",
traefik_admin=os.environ.get("TRAEFIK_ADMIN"),
plugin_registry=get_plugin_registry(),
Expand All @@ -106,7 +106,7 @@ def write_routers() -> None:
t = f.read()
tpl_routers_tcp = Template(t)
tpl_routers_tcp.globals["ProxyProtocol"] = ProxyProtocol
routers_tcp = tpl_routers_tcp.render(projects=projects_tcp, traefik_rule=f"HostSNI(`{domain}`)")
routers_tcp = tpl_routers_tcp.render(projects=projects_passthrough, traefik_rule=f"HostSNI(`{domain}`)")
with open("proxy/traefik/routers-tcp.yml", "w", encoding="utf-8") as f:
f.write(routers_tcp)
projects_udp = get_projects(filter=lambda _, _2, i: i.protocol == Protocol.udp)
Expand Down
1 change: 0 additions & 1 deletion lib/test_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
env={
"MINIO_ROOT_USER": "root",
"MINIO_ROOT_PASSWORD": "83b01a6b8f210b5f5862943f3ebe257d",
"MINIO_DEFAULT_BUCKETS": "ai-assistant",
},
image="minio/minio:latest",
ingress=[
Expand Down
21 changes: 7 additions & 14 deletions proxy/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ services:
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro

traefic-in:
traefik-in:
image: traefik:v2.11
container_name: traefic-in
container_name: traefik-in
env_file:
- ../.env
networks:
- default
- proxynet
ports:
- 8080:8080
- 8443:8443
- 8080:8080/tcp
- 8443:8443/tcp
- 1194:1194/udp
volumes:
- ./traefik/config-in.yml:/etc/traefik/traefik.yml:ro
Expand All @@ -77,13 +77,12 @@ services:
- '8080'
- '8443'
volumes:
- logs:/var/log/traefik
- ./traefik/config-web.yml:/etc/traefik/traefik.yml:ro
- ./traefik/routers-web.yml:/etc/traefik/dynamic/routers-web.yml:ro
- ./traefik/acme:/etc/acme
depends_on:
- dockerproxy
- traefic-in
- traefik-in
- crowdsec
crowdsec:
image: crowdsecurity/crowdsec:v1.6.0
Expand All @@ -101,12 +100,6 @@ services:
BOUNCER_KEY_TRAEFIK: L9yZ1y2XagDm9915mRg2fg==
volumes:
- ./traefik/crowdsec/acquis.yml:/etc/crowdsec/acquis.yaml:ro
- logs:/var/log/traefik:ro
- crowdsec-db:/var/lib/crowdsec/data/
- ../data/crowdsec:/var/lib/crowdsec/data/
labels:
- "traefik.enable=false"

volumes:
logs:
crowdsec-db:
shared:
- "traefik.enable=false"
5 changes: 2 additions & 3 deletions proxy/tpl/config-in.yml.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
accessLog:
filePath: /var/log/traefik/access.log
accessLog: {}

entryPoints:
tcp:
Expand Down Expand Up @@ -55,7 +54,7 @@ entryPoints:
{%- endfor %}

log:
level: DEBUG
level: INFO

providers:
file:
Expand Down
10 changes: 2 additions & 8 deletions proxy/tpl/config-web.yml.j2
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
accessLog:
filePath: /var/log/traefik/access.log
# format: json
# fields:
# defaultMode: keep
# headers:
# defaultMode: keep
accessLog: {}

api:
insecure: false
Expand Down Expand Up @@ -69,7 +63,7 @@ global:
sendAnonymousUsage: true

log:
level: DEBUG
level: INFO

providers:
docker:
Expand Down
19 changes: 6 additions & 13 deletions proxy/tpl/docker-compose.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ services:
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro

traefic-in:
traefik-in:
image: traefik:v2.11
container_name: traefic-in
container_name: traefik-in
env_file:
- ../.env
networks:
- default
- proxynet
ports:
- 8080:8080
- 8443:8443
- 8080:8080/tcp
- 8443:8443/tcp
{%- for p in projects %}
{%- for s in p.services %}
{%- for i in s.ingress %}
Expand All @@ -83,13 +83,12 @@ services:
- '8080'
- '8443'
volumes:
- logs:/var/log/traefik
- ./traefik/config-web.yml:/etc/traefik/traefik.yml:ro
- ./traefik/routers-web.yml:/etc/traefik/dynamic/routers-web.yml:ro
- ./traefik/acme:/etc/acme
depends_on:
- dockerproxy
- traefic-in
- traefik-in
{%- if plugin_registry.crowdsec.enabled %}
{%- set cs = plugin_registry.crowdsec %}
- crowdsec
Expand All @@ -111,13 +110,7 @@ services:
{%- endif %}
volumes:
- ./traefik/crowdsec/acquis.yml:/etc/crowdsec/acquis.yaml:ro
- logs:/var/log/traefik:ro
- crowdsec-db:/var/lib/crowdsec/data/
- ../data/crowdsec:/var/lib/crowdsec/data/
labels:
- "traefik.enable=false"
{%- endif %}

volumes:
logs:
crowdsec-db:
shared:
4 changes: 1 addition & 3 deletions proxy/tpl/routers-tcp.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ tcp:
{%- for s in p.services %}
{%- for i in s.ingress %}
{%- set name = p.name ~ '-' ~ s.name.replace('.', '-') ~ '-' ~ i.port %}
{%- if i.passthrough %}
{{ name }}:
entryPoints:
- tcp-secure
service: {{ name }}
rule: 'HostSNI(`{{ i.domain }}`){% if i.path_prefix %} && PathPrefix(`{{ i.path_prefix }}`){% endif %}'
{%- if i.passthrough %}
tls:
passthrough: true
{%- endif %}
Expand All @@ -33,7 +33,6 @@ tcp:
{%- for s in p.services %}
{%- for i in s.ingress %}
{%- set name = p.name ~ '-' ~ s.name.replace('.', '-') ~ '-' ~ i.port %}
{%- if i.passthrough %}
{{ name }}:
loadBalancer:
{%- if i.proxyprotocol %}
Expand All @@ -42,7 +41,6 @@ tcp:
{%- endif %}
servers:
- address: {{ s.name }}:{{ i.port }}
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
Expand Down
12 changes: 2 additions & 10 deletions proxy/tpl/routers-web.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ http:
{%- for s in p.services %}
{%- for i in s.ingress %}
{%- set name = p.name ~ '-' ~ s.name.replace('.', '-') ~ '-' ~ i.port %}
{%- if i.passthrough %}
{{ name}}:
service: {{ name }}
entryPoints:
{%- if i.passthrough %}
- web
rule: 'Host(`{{ i.domain }}`) && PathPrefix(`/.well-known/acme-challenge/`)'
{%- else %}
- web-secure
rule: 'Host(`{{ i.domain }}`){% if i.path_prefix %} && PathPrefix(`{{ i.path_prefix }}`){% endif %}'
tls:
certResolver: letsencrypt
{%- endif %}
{%- endfor %}
{%- endfor %}
Expand All @@ -36,15 +31,12 @@ http:
{%- for s in p.services %}
{%- for i in s.ingress %}
{%- set name = p.name ~ '-' ~ s.name.replace('.', '-') ~ '-' ~ i.port %}
{%- if i.passthrough %}
{{ name}}:
loadBalancer:
servers:
{%- if i.passthrough %}
# just forwarding port 80 for doing own http challenge:
- url: http://{{ s.name }}:80/
{%- else %}
# routing to a service on the host:
- url: http://{{ s.name }}:{{ i.port }}/
{%- endif %}
{%- endfor %}
{%- endfor %}
Expand Down
3 changes: 2 additions & 1 deletion tpl/docker-compose.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ services:
{%- for i in s.ingress %}
{%- if i.domain %}
{%- set name = project.name ~ '-' ~ s.name.replace('.', '-') ~ '-' ~ i.port %}
- traefik.http.routers.{{ name }}.entrypoints=web-secure
- traefik.http.routers.{{ name }}.entrypoints={% if i.hostport %}{{ Protocol(i.protocol).value }}-{{ i.hostport }}{% else %}web-secure{% endif %}
- traefik.http.routers.{{ name }}.rule=Host(`{{ i.domain }}`){%- if i.path_prefix %} && PathPrefix(`{{ i.path_prefix }}`){%- endif %}
- traefik.http.routers.{{ name }}.tls.certresolver=letsencrypt
- traefik.http.routers.{{ name }}.service={{ name }}
{%- if i.path_prefix and i.path_remove %}
- traefik.http.middlewares.removeServiceSelector.stripPrefix.prefixes={{ i.path_prefix }}
{%- endif %}
Expand Down

0 comments on commit a417e7e

Please sign in to comment.