From 72baae0e27379821652ee7db495708f9cb6396ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Tue, 9 Nov 2021 11:30:00 +0100 Subject: [PATCH] fix: enable plugins to implement the "caddyfile" patch When nginx was removed in favour of caddy, we decided that plugin implementations of the "caddyfile" patch should make use of the "port" local variable. However, local variables are not available from inside plugin patches, which are rendered outside of the context of the parent templates. For a more extensive description of the problem, see: https://github.com/overhangio/tutor-mfe/pull/23#issuecomment-964016190 We still want to make it easy for developers to decide what should the port be for caddy hosts. To do so, we make use of environment variables that are passed at runtime to the caddy container. Thus, a regular plugin patch should look like this: {{ PLUGIN_HOST }}{$default_site_port} { import proxy "myplugin:8000" } --- CHANGELOG-nightly.md | 1 + tutor/templates/apps/caddy/Caddyfile | 11 ++--------- tutor/templates/k8s/deployments.yml | 3 +++ tutor/templates/local/docker-compose.prod.yml | 2 ++ 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CHANGELOG-nightly.md b/CHANGELOG-nightly.md index 4ae842e..16bb0ff 100644 --- a/CHANGELOG-nightly.md +++ b/CHANGELOG-nightly.md @@ -2,6 +2,7 @@ Note: Breaking changes between versions are indicated by "💥". +- [Bugfix] Make it possible for plugins to implement the "caddyfile" patch without relying on the "port" local variable. - 💥[Improvement] Move the Open edX forum to a [dedicated plugin](https://github.com/overhangio/tutor-forum/) (#450). - 💥[Improvement] Get rid of the "tutor-openedx" package, which is no longer supported. - [Bugfix] Fix running Caddy container in k8s, which should always be the case even if `ENABLE_WEB_PROXY` is false. diff --git a/tutor/templates/apps/caddy/Caddyfile b/tutor/templates/apps/caddy/Caddyfile index b8c22ae..ae92508 100644 --- a/tutor/templates/apps/caddy/Caddyfile +++ b/tutor/templates/apps/caddy/Caddyfile @@ -25,14 +25,7 @@ } } -{% if ENABLE_HTTPS and ENABLE_WEB_PROXY %} -{% set port = "" %} -{# listening to https is disabled and we must only listen to http #} -{% else %} -{% set port = ":80" %} -{% endif %} - -{{ LMS_HOST }}{{ port }}, {{ PREVIEW_LMS_HOST }}{{ port }} { +{{ LMS_HOST }}{$default_site_port}, {{ PREVIEW_LMS_HOST }}{$default_site_port} { @favicon_matcher { path_regexp ^(.*)/favicon.ico$ } @@ -51,7 +44,7 @@ {{ patch("caddyfile-lms")|indent(4) }} } -{{ CMS_HOST }}{{ port }} { +{{ CMS_HOST }}{$default_site_port} { @favicon_matcher { path_regexp ^(.*)/favicon.ico$ } diff --git a/tutor/templates/k8s/deployments.yml b/tutor/templates/k8s/deployments.yml index 3841d78..0694c21 100644 --- a/tutor/templates/k8s/deployments.yml +++ b/tutor/templates/k8s/deployments.yml @@ -17,6 +17,9 @@ spec: containers: - name: caddy image: {{ DOCKER_IMAGE_CADDY }} + env: + - name: default_site_port + value: "{% if not ENABLE_HTTPS or not ENABLE_WEB_PROXY %}:80{% endif %}" volumeMounts: - mountPath: /etc/caddy/ name: config diff --git a/tutor/templates/local/docker-compose.prod.yml b/tutor/templates/local/docker-compose.prod.yml index 5f670e1..b753bfa 100644 --- a/tutor/templates/local/docker-compose.prod.yml +++ b/tutor/templates/local/docker-compose.prod.yml @@ -7,6 +7,8 @@ services: ports: - "{{ CADDY_HTTP_PORT }}:80" {% if ENABLE_HTTPS and ENABLE_WEB_PROXY %}- "443:443"{% endif %} + environment: + default_site_port: "{% if not ENABLE_HTTPS or not ENABLE_WEB_PROXY %}:80{% endif %}" volumes: - ../apps/caddy/Caddyfile:/etc/caddy/Caddyfile:ro {% if ENABLE_HTTPS and ENABLE_WEB_PROXY %}- ../../data/caddy:/data{% endif %}