Merge pull request #9083 from dokku/8974-caddy-vhosts-requests-are-routed-before-healthchecks-pass-on-docker-local-contrary-to-docs
Add proxy health checks from app.json readiness
This commit is contained in:
19 files changed
+895
-47
No files matched your search
@@ -95,7 +95,6 @@ trigger-caddy-vhosts-docker-args-process-deploy() {
|
||||
|
||||
ssl_warning_mapping="https:443"
|
||||
if [[ "$tls_internal" == "true" ]]; then
|
||||
output="--label caddy.tls=internal"
|
||||
if [[ "$has_443_mapping" == "false" ]]; then
|
||||
ssl_warning_mapping="http:80"
|
||||
proxy_host_https_port_candidate="$proxy_host_http_port_candidate"
|
||||
@@ -107,6 +106,9 @@ trigger-caddy-vhosts-docker-args-process-deploy() {
|
||||
scheme="http"
|
||||
if [[ -n "$letsencrypt_email" ]] && [[ "$has_443_mapping" == "true" ]]; then
|
||||
output="--label 'caddy=${caddy_domains}'"
|
||||
if [[ "$tls_internal" == "true" ]]; then
|
||||
output="$output --label caddy.tls=internal"
|
||||
fi
|
||||
scheme="https"
|
||||
if [[ -z "$proxy_container_https_port" ]]; then
|
||||
warning_scheme="$(awk -F ':' '{ print $1 }' <<<"$ssl_warning_mapping")"
|
||||
@@ -127,6 +129,10 @@ trigger-caddy-vhosts-docker-args-process-deploy() {
|
||||
|
||||
output="$output --label \"caddy.reverse_proxy={{ upstreams $proxy_container_http_port }}\""
|
||||
fi
|
||||
|
||||
if [[ "$output" == *"caddy.reverse_proxy="* ]]; then
|
||||
output="$output$(fn-caddy-healthcheck-labels "$APP")"
|
||||
fi
|
||||
fi
|
||||
|
||||
local proxy_labels_file_path=$(fn-proxy-get-labels-file-path "caddy" "$APP")
|
||||
@@ -140,4 +146,58 @@ trigger-caddy-vhosts-docker-args-process-deploy() {
|
||||
echo -n "$STDIN $output"
|
||||
}
|
||||
|
||||
fn-caddy-healthcheck-labels() {
|
||||
declare desc="outputs caddy reverse_proxy health check and retry labels derived from the app's web readiness healthcheck"
|
||||
declare APP="$1"
|
||||
local healthcheck hc_path hc_port hc_timeout hc_interval hc_attempts hc_host name value
|
||||
|
||||
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_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 caddy: path must start with a / and must not contain whitespace, control characters, quotes, or backslashes"
|
||||
fi
|
||||
|
||||
hc_host="$(fn-proxy-get-healthcheck-host "$APP" "$healthcheck")"
|
||||
|
||||
echo -n " --label \"caddy.reverse_proxy.health_uri=$hc_path\""
|
||||
[[ -n "$hc_port" ]] && echo -n " --label caddy.reverse_proxy.health_port=$hc_port"
|
||||
echo -n " --label caddy.reverse_proxy.health_interval=${hc_interval}s"
|
||||
echo -n " --label caddy.reverse_proxy.health_timeout=${hc_timeout}s"
|
||||
echo -n " --label caddy.reverse_proxy.health_fails=$hc_attempts"
|
||||
if [[ -n "$hc_host" ]] && fn-proxy-healthcheck-header-is-valid "Host" "$hc_host" && [[ "$hc_host" != *[[:space:]]* ]]; then
|
||||
echo -n " --label caddy.reverse_proxy.health_headers.Host=$hc_host"
|
||||
fi
|
||||
|
||||
while IFS=$'\t' read -r name value; do
|
||||
[[ -z "$name" ]] && continue
|
||||
if ! fn-caddy-healthcheck-header-name-is-valid "$name" || ! fn-proxy-healthcheck-header-is-valid "$name" "$value"; then
|
||||
dokku_log_warn "Skipping invalid app.json healthcheck header for caddy: $name"
|
||||
continue
|
||||
fi
|
||||
|
||||
echo -n " --label 'caddy.reverse_proxy.health_headers.$name=\"$value\"'"
|
||||
done < <(fn-proxy-get-healthcheck-headers "$healthcheck")
|
||||
|
||||
echo -n " --label caddy.reverse_proxy.lb_try_duration=5s"
|
||||
echo -n " --label caddy.reverse_proxy.lb_try_interval=250ms"
|
||||
}
|
||||
|
||||
fn-caddy-healthcheck-header-name-is-valid() {
|
||||
declare desc="checks that a healthcheck header name can be used as a caddy label segment, which may not contain dots or end in an _<number> ordering suffix"
|
||||
declare NAME="$1"
|
||||
|
||||
[[ "$NAME" == *.* ]] && return 1
|
||||
[[ "$NAME" =~ _[0-9]+$ ]] && return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
trigger-caddy-vhosts-docker-args-process-deploy "$@"
|
||||
@@ -38,9 +38,11 @@ fn-domains-global-vhosts() {
|
||||
fn-domains-generate-urls() {
|
||||
declare APP="$1" DEFAULT_SCHEME="$2" DEFAULT_LISTEN_PORT="$3"
|
||||
|
||||
local app_vhosts="$(plugn trigger domains-list "$APP")"
|
||||
if [[ -n "$app_vhosts" ]]; then
|
||||
for app_vhost in $app_vhosts; do
|
||||
local app_vhost
|
||||
local -a app_vhosts
|
||||
read -r -d '' -a app_vhosts <<<"$(plugn trigger domains-list "$APP")" || true
|
||||
if [[ "${#app_vhosts[@]}" -gt 0 ]]; then
|
||||
for app_vhost in "${app_vhosts[@]}"; do
|
||||
fn-domains-generate-urls-from-config "$APP" "$DEFAULT_SCHEME" "$app_vhost" "$DEFAULT_LISTEN_PORT"
|
||||
done
|
||||
else
|
||||
|
||||
@@ -252,7 +252,9 @@ fn-nginx-vhosts-render-template() {
|
||||
SSL_VHOSTS=$(<"$DOKKU_ROOT/VHOST")
|
||||
fi
|
||||
local host
|
||||
for host in $SSL_VHOSTS; do
|
||||
local -a ssl_vhosts
|
||||
read -r -d '' -a ssl_vhosts <<<"$SSL_VHOSTS" || true
|
||||
for host in "${ssl_vhosts[@]}"; do
|
||||
if [[ ! $NOSSL_SERVER_NAME =~ (^|[[:space:]])$host($|[[:space:]]) ]]; then
|
||||
SSL_SERVER_NAME="${host}${SSL_SERVER_NAME:+ $SSL_SERVER_NAME}"
|
||||
fi
|
||||
|
||||
@@ -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,55 @@ 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 name value
|
||||
|
||||
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="$(fn-proxy-get-healthcheck-host "$APP" "$healthcheck")"
|
||||
|
||||
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-proxy-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-proxy-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 < <(fn-proxy-get-healthcheck-headers "$healthcheck")
|
||||
}
|
||||
|
||||
trigger-openresty-vhosts-docker-args-process-deploy "$@"
|
||||
@@ -17,6 +17,71 @@ fn-proxy-get-labels-file-path() {
|
||||
echo "$PROXY_FILE"
|
||||
}
|
||||
|
||||
fn-proxy-get-readiness-healthcheck() {
|
||||
declare desc="outputs the first web readiness healthcheck with a path from the app's app.json as json"
|
||||
declare APP="$1"
|
||||
local app_json
|
||||
|
||||
app_json="$(plugn trigger app-json-get-content "$APP" 2>/dev/null || echo "{}")"
|
||||
echo "$app_json" | jq -c '.healthchecks.web // [] | map(select(.type == "readiness" and .path != null and .path != "")) | .[0] // empty'
|
||||
}
|
||||
|
||||
fn-proxy-healthcheck-path-is-valid() {
|
||||
declare desc="checks that an app.json healthcheck path is absolute and free of whitespace, control, quote, and backslash characters"
|
||||
declare HC_PATH="$1"
|
||||
|
||||
[[ "$HC_PATH" == /* ]] || return 1
|
||||
[[ "$HC_PATH" =~ [[:space:][:cntrl:]\"\'\\] ]] && return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
fn-proxy-healthcheck-header-is-valid() {
|
||||
declare desc="checks that an app.json 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
|
||||
}
|
||||
|
||||
fn-proxy-get-healthcheck-host() {
|
||||
declare desc="outputs the host header for a healthcheck, preferring an app.json Host header over the first app domain"
|
||||
declare APP="$1" HEALTHCHECK="$2"
|
||||
local domain hc_host
|
||||
local -a domains
|
||||
|
||||
hc_host="$(echo "$HEALTHCHECK" | jq -r '.httpHeaders // [] | map(select((.name | ascii_downcase) == "host")) | .[0].value // empty')"
|
||||
if [[ -n "$hc_host" ]]; then
|
||||
echo "$hc_host"
|
||||
return
|
||||
fi
|
||||
|
||||
read -r -d '' -a domains <<<"$(plugn trigger domains-list "$APP")" || true
|
||||
for domain in "${domains[@]}"; do
|
||||
if [[ "$domain" == "localhost" ]] || [[ "$domain" == '*' ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
echo "$domain"
|
||||
return
|
||||
done
|
||||
}
|
||||
|
||||
fn-proxy-get-healthcheck-headers() {
|
||||
declare desc="outputs the tab-separated name and value of each non-Host app.json healthcheck header"
|
||||
declare HEALTHCHECK="$1"
|
||||
|
||||
echo "$HEALTHCHECK" | jq -r '.httpHeaders // [] | map(select((.name | ascii_downcase) != "host")) | .[] | [.name, .value] | @tsv'
|
||||
}
|
||||
|
||||
fn-proxy-healthcheck-scheme-is-valid() {
|
||||
declare desc="checks that an app.json healthcheck scheme is either http or https"
|
||||
declare HC_SCHEME="$1"
|
||||
|
||||
[[ -z "$HC_SCHEME" ]] && return 0
|
||||
[[ "${HC_SCHEME,,}" == "http" ]] || [[ "${HC_SCHEME,,}" == "https" ]]
|
||||
}
|
||||
|
||||
fn-proxy-display-labels() {
|
||||
declare desc="print user-set app container labels for specified proxy"
|
||||
declare PROXY="$1" APP="$2"
|
||||
|
||||
@@ -145,9 +145,11 @@ trigger-scheduler-docker-local-check-deploy() {
|
||||
ARG_ARRAY+=("$DOKKU_APP_LISTEN_PORT")
|
||||
fi
|
||||
|
||||
local app_vhosts="$(plugn trigger domains-list "$APP")"
|
||||
if [[ -n "$app_vhosts" ]]; then
|
||||
for app_vhost in $app_vhosts; do
|
||||
local app_vhost
|
||||
local -a app_vhosts
|
||||
read -r -d '' -a app_vhosts <<<"$(plugn trigger domains-list "$APP")" || true
|
||||
if [[ "${#app_vhosts[@]}" -gt 0 ]]; then
|
||||
for app_vhost in "${app_vhosts[@]}"; do
|
||||
if [[ "$app_vhost" == "localhost" ]] || [[ "$app_vhost" == '*' ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
@@ -130,21 +130,20 @@ trigger-traefik-vhosts-docker-args-process-deploy() {
|
||||
fi
|
||||
fi
|
||||
|
||||
local app_json hc_path hc_scheme hc_port hc_timeout hc_interval hc_filter
|
||||
app_json="$(plugn trigger app-json-get-content "$APP" 2>/dev/null || echo "{}")"
|
||||
hc_filter='.healthchecks.web // [] | map(select(.type == "readiness" and .path != null and .path != "")) | .[0]'
|
||||
hc_path=$(echo "$app_json" | jq -r "$hc_filter | .path // empty")
|
||||
if [[ -n "$hc_path" ]]; then
|
||||
hc_scheme=$(echo "$app_json" | jq -r "$hc_filter | .scheme // empty")
|
||||
hc_port=$(echo "$app_json" | jq -r "$hc_filter | .port // empty")
|
||||
hc_timeout=$(echo "$app_json" | jq -r "$hc_filter | .timeout // empty")
|
||||
hc_interval=$(echo "$app_json" | jq -r "$hc_filter | .wait // empty")
|
||||
local healthcheck hc_path hc_scheme hc_port hc_timeout hc_interval
|
||||
healthcheck="$(fn-proxy-get-readiness-healthcheck "$APP")"
|
||||
if [[ -n "$healthcheck" ]]; then
|
||||
hc_path=$(echo "$healthcheck" | jq -r ".path // empty")
|
||||
hc_scheme=$(echo "$healthcheck" | jq -r ".scheme // empty")
|
||||
hc_port=$(echo "$healthcheck" | jq -r ".port // empty")
|
||||
hc_timeout=$(echo "$healthcheck" | jq -r ".timeout // empty")
|
||||
hc_interval=$(echo "$healthcheck" | jq -r ".wait // empty")
|
||||
|
||||
if ! fn-traefik-healthcheck-path-is-valid "$hc_path"; then
|
||||
if ! fn-proxy-healthcheck-path-is-valid "$hc_path"; then
|
||||
dokku_log_fail "Invalid app.json healthcheck path for traefik: path must start with a / and must not contain whitespace, control characters, quotes, or backslashes"
|
||||
fi
|
||||
|
||||
if ! fn-traefik-healthcheck-scheme-is-valid "$hc_scheme"; then
|
||||
if ! fn-proxy-healthcheck-scheme-is-valid "$hc_scheme"; then
|
||||
dokku_log_fail "Invalid app.json healthcheck scheme for traefik: scheme must be one of http or https"
|
||||
fi
|
||||
|
||||
|
||||
@@ -243,20 +243,3 @@ 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-"
|
||||
}
|
||||
|
||||
fn-traefik-healthcheck-path-is-valid() {
|
||||
declare desc="checks that an app.json healthcheck path is absolute and free of whitespace, control, quote, and backslash characters"
|
||||
declare HC_PATH="$1"
|
||||
|
||||
[[ "$HC_PATH" == /* ]] || return 1
|
||||
[[ "$HC_PATH" =~ [[:space:][:cntrl:]\"\'\\] ]] && return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
fn-traefik-healthcheck-scheme-is-valid() {
|
||||
declare desc="checks that an app.json healthcheck scheme is either http or https"
|
||||
declare HC_SCHEME="$1"
|
||||
|
||||
[[ -z "$HC_SCHEME" ]] && return 0
|
||||
[[ "${HC_SCHEME,,}" == "http" ]] || [[ "${HC_SCHEME,,}" == "https" ]]
|
||||
}
|
||||
Reference in new issue
Block a user