fix: add proxy health checks from app.json readiness
The caddy, haproxy, openresty, and traefik docs stated that requests are only routed to containers passing healthchecks, but these proxies route to a new container as soon as it starts. The first `web` readiness healthcheck in `app.json` is now also turned into health checks and retries for caddy and openresty, as it already was for traefik, so requests are routed away from containers that are not yet ready, and the docs now describe how each proxy actually behaves.
This commit is contained in:
15 files changed
+694
-39
No files matched your search
@@ -206,6 +206,8 @@ trigger-openresty-vhosts-docker-args-process-deploy() {
|
||||
value="$(fn-openresty-computed-x-forwarded-ssl "$APP")"
|
||||
[[ -n "$value" ]] && output="$output '--label=openresty.x-forwarded-ssl=$value'"
|
||||
|
||||
output="$output$(fn-openresty-healthcheck-labels "$APP")"
|
||||
|
||||
local proxy_labels_file_path=$(fn-proxy-get-labels-file-path "openresty" "$APP")
|
||||
if [[ -f "$proxy_labels_file_path" ]]; then
|
||||
while read -r line; do
|
||||
@@ -217,4 +219,76 @@ trigger-openresty-vhosts-docker-args-process-deploy() {
|
||||
echo -n "$STDIN $output"
|
||||
}
|
||||
|
||||
fn-openresty-healthcheck-labels() {
|
||||
declare desc="outputs openresty upstream healthcheck and failover labels derived from the app's web readiness healthcheck"
|
||||
declare APP="$1"
|
||||
local healthcheck hc_path hc_port hc_scheme hc_timeout hc_interval hc_attempts hc_host domain name value
|
||||
local -a domains
|
||||
|
||||
healthcheck="$(fn-proxy-get-readiness-healthcheck "$APP")"
|
||||
if [[ -z "$healthcheck" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
hc_path="$(echo "$healthcheck" | jq -r ".path // empty")"
|
||||
hc_port="$(echo "$healthcheck" | jq -r ".port // empty")"
|
||||
hc_scheme="$(echo "$healthcheck" | jq -r ".scheme // empty")"
|
||||
hc_timeout="$(echo "$healthcheck" | jq -r ".timeout // 5")"
|
||||
hc_interval="$(echo "$healthcheck" | jq -r ".wait // 5")"
|
||||
hc_attempts="$(echo "$healthcheck" | jq -r ".attempts // 3")"
|
||||
|
||||
if ! fn-proxy-healthcheck-path-is-valid "$hc_path"; then
|
||||
dokku_log_fail "Invalid app.json healthcheck path for openresty: path must start with a / and must not contain whitespace, control characters, quotes, or backslashes"
|
||||
fi
|
||||
|
||||
if ! fn-proxy-healthcheck-scheme-is-valid "$hc_scheme"; then
|
||||
dokku_log_fail "Invalid app.json healthcheck scheme for openresty: scheme must be one of http or https"
|
||||
fi
|
||||
|
||||
hc_host="$(echo "$healthcheck" | jq -r '.httpHeaders // [] | map(select((.name | ascii_downcase) == "host")) | .[0].value // empty')"
|
||||
if [[ -z "$hc_host" ]]; then
|
||||
read -r -d '' -a domains <<<"$(plugn trigger domains-list "$APP")" || true
|
||||
for domain in "${domains[@]}"; do
|
||||
if [[ "$domain" == "localhost" ]] || [[ "$domain" == '*' ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
hc_host="$domain"
|
||||
break
|
||||
done
|
||||
fi
|
||||
|
||||
echo -n " '--label=openresty.upstream-max-fails=1'"
|
||||
echo -n " '--label=openresty.upstream-fail-timeout=${hc_interval}s'"
|
||||
echo -n " '--label=openresty.proxy-next-upstream-timeout=5s'"
|
||||
echo -n " '--label=openresty.healthcheck-path=$hc_path'"
|
||||
[[ -n "$hc_port" ]] && echo -n " '--label=openresty.healthcheck-port=$hc_port'"
|
||||
[[ -n "$hc_scheme" ]] && echo -n " '--label=openresty.healthcheck-scheme=${hc_scheme,,}'"
|
||||
echo -n " '--label=openresty.healthcheck-timeout=$hc_timeout'"
|
||||
echo -n " '--label=openresty.healthcheck-interval=$hc_interval'"
|
||||
echo -n " '--label=openresty.healthcheck-fall=$hc_attempts'"
|
||||
if [[ -n "$hc_host" ]] && fn-openresty-healthcheck-header-is-valid "Host" "$hc_host"; then
|
||||
echo -n " '--label=openresty.healthcheck-host=$hc_host'"
|
||||
fi
|
||||
|
||||
while IFS=$'\t' read -r name value; do
|
||||
[[ -z "$name" ]] && continue
|
||||
if ! fn-openresty-healthcheck-header-is-valid "$name" "$value"; then
|
||||
dokku_log_warn "Skipping invalid app.json healthcheck header for openresty: $name"
|
||||
continue
|
||||
fi
|
||||
|
||||
echo -n " '--label=openresty.healthcheck-header.$name=$value'"
|
||||
done < <(echo "$healthcheck" | jq -r '.httpHeaders // [] | map(select((.name | ascii_downcase) != "host")) | .[] | [.name, .value] | @tsv')
|
||||
}
|
||||
|
||||
fn-openresty-healthcheck-header-is-valid() {
|
||||
declare desc="checks that a healthcheck header name is a token and its value is free of control, quote, and backslash characters"
|
||||
declare NAME="$1" VALUE="$2"
|
||||
|
||||
[[ "$NAME" =~ ^[A-Za-z0-9!#$%\&*+.^_\`\|~-]+$ ]] || return 1
|
||||
[[ "$VALUE" =~ [[:cntrl:]\"\'\\] ]] && return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
trigger-openresty-vhosts-docker-args-process-deploy "$@"
|
||||
Reference in new issue
Block a user