1
0
Fork 0

Make container labels configurable and extensible

Fixes https://github.com/mother-of-all-self-hosting/mash-playbook/issues/115
This commit is contained in:
Slavi Pantaleev 2023-11-21 16:23:48 +02:00
parent 4cd3fb7032
commit 195fd31ec2
2 changed files with 69 additions and 33 deletions

View file

@ -181,6 +181,70 @@ nextcloud_container_labels_traefik_additional_response_headers_auto: |
}} }}
nextcloud_container_labels_traefik_additional_response_headers_custom: {} 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. # 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. # See `roles/custom/nextcloud/templates/labels.j2` for details.
# #

View file

@ -5,47 +5,19 @@ traefik.enable=true
traefik.docker.network={{ nextcloud_container_labels_traefik_docker_network }} traefik.docker.network={{ nextcloud_container_labels_traefik_docker_network }}
{% endif %} {% endif %}
{% set middlewares = [] %} {% for middleware in nextcloud_container_labels_traefik_http_middlewares | sort (attribute='priority,name') %}
{% for key, value in middleware.config.items() %}
{% if nextcloud_container_labels_traefik_path_prefix != '/' %} traefik.http.middlewares.{{ middleware.name }}.{{ middleware.type }}.{{ middleware.config_key_prefix | default('') }}{{ key }}={{ value }}
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 }}
{% endfor %} {% 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 %} {% endfor %}
{% set middlewares = middlewares + [nextcloud_identifier + '-add-response-headers'] %}
{% endif %}
traefik.http.routers.{{ nextcloud_identifier }}.rule={{ nextcloud_container_labels_traefik_rule }} traefik.http.routers.{{ nextcloud_identifier }}.rule={{ nextcloud_container_labels_traefik_rule }}
{% if nextcloud_container_labels_traefik_priority | int > 0 %} {% if nextcloud_container_labels_traefik_priority | int > 0 %}
traefik.http.routers.{{ nextcloud_identifier }}.priority={{ nextcloud_container_labels_traefik_priority }} traefik.http.routers.{{ nextcloud_identifier }}.priority={{ nextcloud_container_labels_traefik_priority }}
{% endif %} {% endif %}
traefik.http.routers.{{ nextcloud_identifier }}.service={{ nextcloud_identifier }} traefik.http.routers.{{ nextcloud_identifier }}.service={{ nextcloud_identifier }}
{% if middlewares | length > 0 %} {% if nextcloud_container_labels_traefik_http_middlewares | length > 0 %}
traefik.http.routers.{{ nextcloud_identifier }}.middlewares={{ middlewares | join(',') }} traefik.http.routers.{{ nextcloud_identifier }}.middlewares={{ nextcloud_container_labels_traefik_http_middlewares | sort (attribute='priority,name') | map(attribute='name') | join(',') }}
{% endif %} {% endif %}
traefik.http.routers.{{ nextcloud_identifier }}.entrypoints={{ nextcloud_container_labels_traefik_entrypoints }} traefik.http.routers.{{ nextcloud_identifier }}.entrypoints={{ nextcloud_container_labels_traefik_entrypoints }}
traefik.http.routers.{{ nextcloud_identifier }}.tls={{ nextcloud_container_labels_traefik_tls | to_json }} traefik.http.routers.{{ nextcloud_identifier }}.tls={{ nextcloud_container_labels_traefik_tls | to_json }}