Files
dokku/plugins/traefik-vhosts/internal-functions
T
Jose Diaz-Gonzalez 53066ec4ac fix: traefik priority loss, caddy directive order, openresty cache, k3s diagnostic
Traefik was setting an explicit priority equal to the path length, which is smaller than traefik's default priority (rule length) on the web container. Web's bare Host(...) rule has implicit priority ~40 while my explicit 7 lost the match, so /api/v0/Procfile always went to web. Dropping the explicit priority lets traefik use rule length, which already gives longest-prefix-wins because the route rule includes both Host and PathPrefix and so is always longer than web's bare Host(...). Caddy's route block was being preempted by web's bare reverse_proxy due to caddy's default directive order (reverse_proxy ~190, route ~210). Switching to handle, which terminates matched requests and isn't bypassed by reverse_proxy, makes /api/v0/* land on api instead of web's catch-all. Openresty T2 polls longer (60 retries) and dumps sidecar logs on failure so the upstream label-update cache delay can be reported. The k3s files dump kubectl get service/ingress/endpoints in T1 so the next CI run reveals whether the api Service has ports and whether the Ingress backend reference is correct.
2026-11-04 14:56:38 -05:00

335 lines
10 KiB
Bash
Executable File

#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
fn-traefik-route-labels() {
declare desc="emits per-route traefik labels for a non-web container"
declare APP="$1" PROC_TYPE="$2" STDIN="$3"
local host_rule output route_proc route_port route_path route_strip
local router_name service_name middleware_name path_len
host_rule="$(fn-traefik-host-rule "$APP")"
output="--label traefik.enable=true"
while IFS='|' read -r route_proc route_port route_path route_strip; do
[[ -z "$route_proc" ]] && continue
[[ "$route_proc" != "$PROC_TYPE" ]] && continue
local route_slug
route_slug="$(fn-traefik-route-slug "$route_path")"
router_name="${APP}-${route_proc}-${route_slug}"
service_name="${APP}-${route_proc}-${route_slug}"
path_len="${#route_path}"
# Triple-backslash for the same eval-survival reason as fn-traefik-host-rule.
local rule="PathPrefix(\\\`${route_path}\\\`)"
if [[ -n "$host_rule" ]]; then
rule="(${host_rule}) && ${rule}"
fi
output="$output --label \"traefik.http.routers.${router_name}.rule=${rule}\""
output="$output --label traefik.http.routers.${router_name}.entrypoints=$(fn-traefik-computed-http-entry-point)"
# No explicit priority - traefik's default (rule length) gives
# longest-prefix-wins because our rule includes both Host(...) and
# PathPrefix(...), which is always longer than web's bare Host(...) rule.
output="$output --label traefik.http.routers.${router_name}.service=${service_name}"
output="$output --label traefik.http.services.${service_name}.loadbalancer.server.port=${route_port}"
if [[ "$route_strip" == "1" ]]; then
middleware_name="${router_name}-strip"
output="$output --label traefik.http.middlewares.${middleware_name}.stripprefix.prefixes=${route_path}"
output="$output --label traefik.http.routers.${router_name}.middlewares=${middleware_name}"
fi
done < <(plugn trigger proxy-routes-list "$APP")
local proxy_labels_file_path=$(fn-proxy-get-labels-file-path "traefik" "$APP")
if [[ -f "$proxy_labels_file_path" ]]; then
while read -r line; do
[[ -z "$line" ]] && continue
output="$output --label '$line'"
done <"$proxy_labels_file_path"
fi
echo -n "$STDIN $output"
}
fn-traefik-route-slug() {
declare desc="returns a deterministic kebab-case slug for a route path"
declare ROUTE_PATH="$1"
local s="${ROUTE_PATH#/}"
s="${s%/}"
s="${s//\//-}"
s="${s,,}"
if [[ -z "$s" ]]; then
s="root"
fi
echo "$s"
}
fn-traefik-host-rule() {
declare desc="builds a traefik Host(\`d1\`) || Host(\`d2\`) rule fragment from an app's domains"
declare APP="$1"
local app_domains domain result=""
app_domains="$(plugn trigger domains-list "$APP")"
if [[ -z "$app_domains" ]]; then
return
fi
# Backticks must be double-escaped so they survive the docker-args eval
# path. A single backslash leaves a bare backtick in the final string,
# which then runs as a command substitution at eval time and breaks the
# docker run invocation. Triple-backslash matches the existing emission
# convention used by docker-args-process-deploy.
for domain in $(echo "$app_domains" | xargs); do
if [[ -z "$result" ]]; then
result="Host(\\\`${domain}\\\`)"
continue
fi
result="${result} || Host(\\\`${domain}\\\`)"
done
echo "$result"
}
fn-traefik-logs() {
declare desc="shows the logs for the traefik container"
declare TAIL="$1" NUM="$2"
local dokku_logs_args=("--tail" "$NUM")
if [[ "$TAIL" == "true" ]]; then
dokku_logs_args+=("--follow")
fi
"$DOCKER_BIN" logs traefik-traefik-1 "${dokku_logs_args[@]}"
}
fn-traefik-logs-usage() {
declare desc="logs specific usage"
echo "Usage: dokku traefik:logs"
echo " display recent traefik log output"
echo ""
echo " -n, --num NUM # the number of lines to display"
echo " -t, --tail # continually stream logs"
}
fn-traefik-template-compose-file() {
declare desc="templates out the compose file"
declare OUTPUT_PATH="$1"
local COMPOSE_TEMPLATE="$PLUGIN_AVAILABLE_PATH/traefik-vhosts/templates/compose.yml.sigil"
local basic_auth basic_auth_password basic_auth_username
CUSTOM_COMPOSE_TEMPLATE="$(plugn trigger traefik-template-source "$APP")"
if [[ -n "$CUSTOM_COMPOSE_TEMPLATE" ]]; then
COMPOSE_TEMPLATE="$CUSTOM_COMPOSE_TEMPLATE"
fi
basic_auth_password="$(fn-traefik-computed-basic-auth-password)"
basic_auth_username="$(fn-traefik-computed-basic-auth-username)"
if [[ -n "$basic_auth_password" ]] && [[ -n "$basic_auth_username" ]]; then
basic_auth="$(htpasswd -nb "$basic_auth_username" "$basic_auth_password" | sed -e s/\\$/\\$\\$/g)"
fi
local dns_provider_env_vars=""
local dns_provider
dns_provider="$(fn-traefik-computed-dns-provider)"
if [[ -n "$dns_provider" ]]; then
dns_provider_env_vars="$(fn-traefik-dns-provider-env-vars)"
fi
local api_entry_point_address api_entry_point_port=""
api_entry_point_address="$(fn-traefik-computed-api-entry-point-address)"
if [[ -n "$api_entry_point_address" ]]; then
api_entry_point_port="${api_entry_point_address##*:}"
fi
local SIGIL_PARAMS=(TRAEFIK_API_ENABLED="$(fn-traefik-computed-api-enabled)"
TRAEFIK_API_ENTRY_POINT="$(fn-traefik-computed-api-entry-point)"
TRAEFIK_API_ENTRY_POINT_ADDRESS="$api_entry_point_address"
TRAEFIK_API_ENTRY_POINT_PORT="$api_entry_point_port"
TRAEFIK_API_VHOST="$(fn-traefik-computed-api-vhost)"
TRAEFIK_BASIC_AUTH="$basic_auth"
TRAEFIK_CHALLENGE_MODE="$(fn-traefik-computed-challenge-mode)"
TRAEFIK_DASHBOARD_ENABLED="$(fn-traefik-computed-dashboard-enabled)"
TRAEFIK_DATA_DIR="${DOKKU_LIB_ROOT}/data/traefik"
TRAEFIK_DNS_PROVIDER="$dns_provider"
TRAEFIK_DNS_PROVIDER_ENV_VARS="$dns_provider_env_vars"
TRAEFIK_IMAGE="$(fn-traefik-computed-image)"
TRAEFIK_LETSENCRYPT_EMAIL="$(fn-traefik-computed-letsencrypt-email)"
TRAEFIK_LETSENCRYPT_SERVER="$(fn-traefik-computed-letsencrypt-server)"
TRAEFIK_LOG_LEVEL="$(fn-traefik-computed-log-level)")
sigil -f "$COMPOSE_TEMPLATE" "${SIGIL_PARAMS[@]}" | cat -s >"$OUTPUT_PATH"
}
fn-traefik-global-api-enabled() {
fn-plugin-property-get-default "traefik" "--global" "api-enabled" ""
}
fn-traefik-computed-api-enabled() {
local value
value="$(fn-traefik-global-api-enabled)"
if [[ -z "$value" ]]; then
value="false"
fi
echo "$value"
}
fn-traefik-global-api-vhost() {
fn-plugin-property-get-default "traefik" "--global" "api-vhost" ""
}
fn-traefik-computed-api-vhost() {
local value
value="$(fn-traefik-global-api-vhost)"
if [[ -z "$value" ]]; then
value="traefik.dokku.me"
fi
echo "$value"
}
fn-traefik-global-basic-auth-password() {
fn-plugin-property-get-default "traefik" "--global" "basic-auth-password" ""
}
fn-traefik-computed-basic-auth-password() {
fn-traefik-global-basic-auth-password
}
fn-traefik-global-basic-auth-username() {
fn-plugin-property-get-default "traefik" "--global" "basic-auth-username" ""
}
fn-traefik-computed-basic-auth-username() {
fn-traefik-global-basic-auth-username
}
fn-traefik-global-dashboard-enabled() {
fn-plugin-property-get-default "traefik" "--global" "dashboard-enabled" ""
}
fn-traefik-computed-dashboard-enabled() {
local value
value="$(fn-traefik-global-dashboard-enabled)"
if [[ -z "$value" ]]; then
value="false"
fi
echo "$value"
}
fn-traefik-global-image() {
fn-plugin-property-get-default "traefik" "--global" "image" ""
}
fn-traefik-computed-image() {
local value
value="$(fn-traefik-global-image)"
if [[ -z "$value" ]]; then
value="$(grep "FROM" "$PLUGIN_AVAILABLE_PATH/traefik-vhosts/Dockerfile" | awk '{print $2}')"
fi
echo "$value"
}
fn-traefik-global-letsencrypt-email() {
fn-plugin-property-get-default "traefik" "--global" "letsencrypt-email" ""
}
fn-traefik-computed-letsencrypt-email() {
fn-traefik-global-letsencrypt-email
}
fn-traefik-global-letsencrypt-server() {
fn-plugin-property-get-default "traefik" "--global" "letsencrypt-server" ""
}
fn-traefik-computed-letsencrypt-server() {
local value
value="$(fn-traefik-global-letsencrypt-server)"
if [[ -z "$value" ]]; then
value="https://acme-v02.api.letsencrypt.org/directory"
fi
echo "$value"
}
fn-traefik-global-log-level() {
fn-plugin-property-get-default "traefik" "--global" "log-level" ""
}
fn-traefik-computed-log-level() {
local value
value="$(fn-traefik-global-log-level)"
if [[ -z "$value" ]]; then
value="ERROR"
fi
echo "${value^^}"
}
fn-traefik-global-http-entry-point() {
fn-plugin-property-get-default "traefik" "--global" "http-entry-point" ""
}
fn-traefik-computed-http-entry-point() {
local value
value="$(fn-traefik-global-http-entry-point)"
if [[ -z "$value" ]]; then
value="http"
fi
echo "$value"
}
fn-traefik-global-https-entry-point() {
fn-plugin-property-get-default "traefik" "--global" "https-entry-point" ""
}
fn-traefik-computed-https-entry-point() {
local value
value="$(fn-traefik-global-https-entry-point)"
if [[ -z "$value" ]]; then
value="https"
fi
echo "$value"
}
fn-traefik-global-api-entry-point() {
fn-plugin-property-get-default "traefik" "--global" "api-entry-point" ""
}
fn-traefik-computed-api-entry-point() {
fn-traefik-global-api-entry-point
}
fn-traefik-global-api-entry-point-address() {
fn-plugin-property-get-default "traefik" "--global" "api-entry-point-address" ""
}
fn-traefik-computed-api-entry-point-address() {
fn-traefik-global-api-entry-point-address
}
fn-traefik-global-challenge-mode() {
fn-plugin-property-get-default "traefik" "--global" "challenge-mode" ""
}
fn-traefik-computed-challenge-mode() {
local value
value="$(fn-traefik-global-challenge-mode)"
if [[ -z "$value" ]]; then
value="tls"
fi
echo "$value"
}
fn-traefik-global-dns-provider() {
fn-plugin-property-get-default "traefik" "--global" "dns-provider" ""
}
fn-traefik-computed-dns-provider() {
fn-traefik-global-dns-provider
}
fn-traefik-dns-provider-env-vars() {
declare desc="returns all dns-provider-* environment variables"
fn-plugin-property-get-all-by-prefix "traefik" "--global" "dns-provider-"
}