From 195fd31ec2cca73c2b8692df238fb42a310bb340 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Tue, 21 Nov 2023 16:23:48 +0200 Subject: [PATCH] Make container labels configurable and extensible Fixes https://github.com/mother-of-all-self-hosting/mash-playbook/issues/115 --- defaults/main.yml | 64 +++++++++++++++++++++++++++++++++++++++++++++ templates/labels.j2 | 38 ++++----------------------- 2 files changed, 69 insertions(+), 33 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 7cc2f37..83b0063 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -181,6 +181,70 @@ nextcloud_container_labels_traefik_additional_response_headers_auto: | }} nextcloud_container_labels_traefik_additional_response_headers_custom: {} +# A list of middlewares to add to the service. +# Add your own via the `nextcloud_container_labels_traefik_http_middlewares_custom` variable. +nextcloud_container_labels_traefik_http_middlewares: "{{ nextcloud_container_labels_traefik_http_middlewares_default + nextcloud_container_labels_traefik_http_middlewares_auto + nextcloud_container_labels_traefik_http_middlewares_custom }}" + +nextcloud_container_labels_traefik_http_middlewares_default: | + {{ + ([{ + 'priority': 1000, + 'name': (nextcloud_identifier + '-slashless-redirect'), + 'type': 'redirectregex', + 'config': { + 'regex': '^(' + nextcloud_container_labels_traefik_path_prefix | quote + ')$', + 'replacement': '${1}/', + }, + }] if nextcloud_container_labels_traefik_path_prefix != '/' else []) + + + + + ([{ + 'priority': 2000, + 'name': (nextcloud_identifier + '-dav-redirectregex'), + 'type': 'redirectregex', + 'config': { + 'permanent': 'true', + 'regex': ('^' + nextcloud_scheme + '://' + nextcloud_hostname | quote + '/.well-known/(?:card|cal)dav$'), + 'replacement': (nextcloud_scheme + '://' + nextcloud_hostname + '/remote.php/dav/'), + }, + }] if nextcloud_container_labels_traefik_dav_redirect_regex_enabled else []) + + + + + ([{ + 'priority': 3000, + 'name': (nextcloud_identifier + '-strip-prefix'), + 'type': 'stripprefix', + 'config': { + 'prefixes': nextcloud_container_labels_traefik_path_prefix, + }, + }] if nextcloud_container_labels_traefik_path_prefix != '/' else []) + + + + + ([{ + 'priority': 4000, + 'name': (nextcloud_identifier + '-add-request-headers'), + 'type': 'headers', + 'config_key_prefix': 'customrequestheaders.', + 'config': nextcloud_container_labels_traefik_additional_request_headers, + }] if nextcloud_container_labels_traefik_additional_request_headers.keys() | length > 0 else []) + + + + + ([{ + 'priority': 5000, + 'name': (nextcloud_identifier + '-add-response-headers'), + 'type': 'headers', + 'config_key_prefix': 'customresponseheaders.', + 'config': nextcloud_container_labels_traefik_additional_response_headers, + }] if nextcloud_container_labels_traefik_additional_response_headers.keys() | length > 0 else []) + }} + +nextcloud_container_labels_traefik_http_middlewares_auto: [] +nextcloud_container_labels_traefik_http_middlewares_custom: [] + # nextcloud_container_labels_additional_labels contains a multiline string with additional labels to add to the container label file. # See `roles/custom/nextcloud/templates/labels.j2` for details. # diff --git a/templates/labels.j2 b/templates/labels.j2 index 6fcaaa1..cd0e9ae 100644 --- a/templates/labels.j2 +++ b/templates/labels.j2 @@ -5,47 +5,19 @@ traefik.enable=true traefik.docker.network={{ nextcloud_container_labels_traefik_docker_network }} {% endif %} -{% set middlewares = [] %} - -{% if nextcloud_container_labels_traefik_path_prefix != '/' %} -traefik.http.middlewares.{{ nextcloud_identifier }}-slashless-redirect.redirectregex.regex=^({{ nextcloud_container_labels_traefik_path_prefix | quote }})$ -traefik.http.middlewares.{{ nextcloud_identifier }}-slashless-redirect.redirectregex.replacement=${1}/ -{% set middlewares = middlewares + [nextcloud_identifier + '-slashless-redirect'] %} -{% endif %} - -{% if nextcloud_container_labels_traefik_dav_redirect_regex_enabled %} -traefik.http.middlewares.{{ nextcloud_identifier }}-dav-redirectregex.redirectregex.permanent=true -traefik.http.middlewares.{{ nextcloud_identifier }}-dav-redirectregex.redirectregex.regex=^{{ nextcloud_scheme }}://{{ nextcloud_hostname | quote }}/.well-known/(?:card|cal)dav$ -traefik.http.middlewares.{{ nextcloud_identifier }}-dav-redirectregex.redirectregex.replacement={{ nextcloud_scheme }}://{{ nextcloud_hostname }}/remote.php/dav/ -{% set middlewares = middlewares + [nextcloud_identifier + '-dav-redirectregex'] %} -{% endif %} - -{% if nextcloud_container_labels_traefik_path_prefix != '/' %} -traefik.http.middlewares.{{ nextcloud_identifier }}-strip-prefix.stripprefix.prefixes={{ nextcloud_container_labels_traefik_path_prefix }} -{% set middlewares = middlewares + [nextcloud_identifier + '-strip-prefix'] %} -{% endif %} - -{% if nextcloud_container_labels_traefik_additional_request_headers.keys() | length > 0 %} -{% for name, value in nextcloud_container_labels_traefik_additional_request_headers.items() %} -traefik.http.middlewares.{{ nextcloud_identifier }}-add-request-headers.headers.customrequestheaders.{{ name }}={{ value }} +{% for middleware in nextcloud_container_labels_traefik_http_middlewares | sort (attribute='priority,name') %} +{% for key, value in middleware.config.items() %} +traefik.http.middlewares.{{ middleware.name }}.{{ middleware.type }}.{{ middleware.config_key_prefix | default('') }}{{ key }}={{ value }} {% endfor %} -{% set middlewares = middlewares + [nextcloud_identifier + '-add-request-headers'] %} -{% endif %} - -{% if nextcloud_container_labels_traefik_additional_response_headers.keys() | length > 0 %} -{% for name, value in nextcloud_container_labels_traefik_additional_response_headers.items() %} -traefik.http.middlewares.{{ nextcloud_identifier }}-add-response-headers.headers.customresponseheaders.{{ name }}={{ value }} {% endfor %} -{% set middlewares = middlewares + [nextcloud_identifier + '-add-response-headers'] %} -{% endif %} traefik.http.routers.{{ nextcloud_identifier }}.rule={{ nextcloud_container_labels_traefik_rule }} {% if nextcloud_container_labels_traefik_priority | int > 0 %} traefik.http.routers.{{ nextcloud_identifier }}.priority={{ nextcloud_container_labels_traefik_priority }} {% endif %} traefik.http.routers.{{ nextcloud_identifier }}.service={{ nextcloud_identifier }} -{% if middlewares | length > 0 %} -traefik.http.routers.{{ nextcloud_identifier }}.middlewares={{ middlewares | join(',') }} +{% if nextcloud_container_labels_traefik_http_middlewares | length > 0 %} +traefik.http.routers.{{ nextcloud_identifier }}.middlewares={{ nextcloud_container_labels_traefik_http_middlewares | sort (attribute='priority,name') | map(attribute='name') | join(',') }} {% endif %} traefik.http.routers.{{ nextcloud_identifier }}.entrypoints={{ nextcloud_container_labels_traefik_entrypoints }} traefik.http.routers.{{ nextcloud_identifier }}.tls={{ nextcloud_container_labels_traefik_tls | to_json }}