From 19f5a026d78a894c3ebf2830b0cd69149baacca5 Mon Sep 17 00:00:00 2001 From: Maurice Faber Date: Sat, 9 Nov 2024 18:35:30 +0100 Subject: [PATCH] feat: expose ingress to internal services --- lib/models.py | 2 ++ tpl/docker-compose.yml.j2 | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/models.py b/lib/models.py index 7c94fb0..f2e55bb 100644 --- a/lib/models.py +++ b/lib/models.py @@ -94,6 +94,8 @@ class Ingress(BaseModel): """The type of router to use for the service""" tls: TLS = None """TLS settings that will be used instead of 'domain'""" + expose: bool = False + """Expose the service to any other internal service""" @model_validator(mode="after") @classmethod diff --git a/tpl/docker-compose.yml.j2 b/tpl/docker-compose.yml.j2 index 1e4e28d..4e18a00 100755 --- a/tpl/docker-compose.yml.j2 +++ b/tpl/docker-compose.yml.j2 @@ -1,3 +1,21 @@ +{%- macro treedot(dict,string) %} + {%- for key, value in dict.items() -%} + {%- if value is mapping -%} + {%- if string -%} + {%- set str = string ~ '.' ~ key -%} + {%- else -%} + {%- set str = key -%} + {%- endif -%} + {{ treedot(value,str) }} + {%- else -%} + {%- if string %} + {{ string ~ '.' ~ key }} = {{ value }} + {%- else %} + {{ key }} = {{ value }} + {%- endif -%} + {%- endif -%} + {%- endfor -%} +{%- endmacro -%} --- networks: proxynet: @@ -10,7 +28,7 @@ services: {#- The service needs discovery labels when one of its ingress entries has a domain or tls and is not using hostport #} {#- (hostport services are handled by static routers because of their need to restart anyway for new entrypoints) #} {%- set needs_discovery = (list(s.ingress | selectattr('domain')) + list(s.ingress | selectattr('tls'))) | length > list(s.ingress | selectattr('domain') | selectattr('hostport')) | length %} - {%- set has_ingress = list(s.ingress | selectattr('domain')) | length > 0 or list(s.ingress | selectattr('tls')) | length > 0 or list(s.ingress | selectattr('hostport')) | length > 0 %} + {%- set has_ingress = list(s.ingress | selectattr('domain')) | length > 0 or list(s.ingress | selectattr('tls')) | length > 0 or list(s.ingress | selectattr('hostport')) or list(s.ingress | selectattr('expose')) | length > 0 %} {{ project.name }}-{{ s.host }}: {%- if s.command %} command: {{ s.command }} @@ -66,6 +84,9 @@ services: {%- if i.path_prefix and i.path_remove %} - traefik.{{ router }}.middlewares.removeServiceSelector.stripPrefix.prefixes={{ i.path_prefix }} {%- endif %} + {%- if i.middlewares %} + - traefik.{{ router }}.routers.{{ name }}.middlewares={{ i.middlewares | join(',') }} + {%- endif %} - traefik.{{ router }}.services.{{ name }}.loadbalancer.server.port={{ i.port }} {%- endif %} {%- endfor %}