mirror of
https://github.com/thib8956/nginx-proxy
synced 2025-02-24 01:38:15 +00:00
chore: Refactor upstream
template for readability
In particular, reduce the nesting depth to make it easier to understand what the code is doing by: * converting an $O(nm)$ nested loop into two serial $O(n)+O(m)$ loops, and * consolidating similar nested `if` cases.
This commit is contained in:
parent
daeed502cb
commit
bcec2d9075
72
nginx.tmpl
72
nginx.tmpl
@ -19,6 +19,12 @@
|
|||||||
{{- $_ := set $globals "access_log" (or (and (not $globals.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }}
|
{{- $_ := set $globals "access_log" (or (and (not $globals.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }}
|
||||||
{{- $_ := set $globals "enable_ipv6" (parseBool (coalesce $globals.Env.ENABLE_IPV6 "false")) }}
|
{{- $_ := set $globals "enable_ipv6" (parseBool (coalesce $globals.Env.ENABLE_IPV6 "false")) }}
|
||||||
{{- $_ := set $globals "ssl_policy" (or ($globals.Env.SSL_POLICY) "Mozilla-Intermediate") }}
|
{{- $_ := set $globals "ssl_policy" (or ($globals.Env.SSL_POLICY) "Mozilla-Intermediate") }}
|
||||||
|
{{- $_ := set $globals "networks" (dict) }}
|
||||||
|
# networks available to nginx-proxy:
|
||||||
|
{{- range $globals.CurrentContainer.Networks }}
|
||||||
|
{{- $_ := set $globals.networks .Name . }}
|
||||||
|
# {{ .Name }}
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
{{- define "ssl_policy" }}
|
{{- define "ssl_policy" }}
|
||||||
{{- if eq .ssl_policy "Mozilla-Modern" }}
|
{{- if eq .ssl_policy "Mozilla-Modern" }}
|
||||||
@ -113,11 +119,11 @@ upstream {{ .Upstream }} {
|
|||||||
{{- /* If only 1 port exposed, use that as a default, else 80 */}}
|
{{- /* If only 1 port exposed, use that as a default, else 80 */}}
|
||||||
{{- $defaultPort := (when (eq (len $container.Addresses) 1) (first $container.Addresses) (dict "Port" "80")).Port }}
|
{{- $defaultPort := (when (eq (len $container.Addresses) 1) (first $container.Addresses) (dict "Port" "80")).Port }}
|
||||||
{{- $port := (coalesce $container.Env.VIRTUAL_PORT $defaultPort) }}
|
{{- $port := (coalesce $container.Env.VIRTUAL_PORT $defaultPort) }}
|
||||||
{{- $address := where $container.Addresses "Port" $port | first }}
|
{{- $addr_obj := where $container.Addresses "Port" $port | first }}
|
||||||
# Exposed ports:{{ range $container.Addresses }} {{ .Port }}/{{ .Proto }}{{ else }} (none){{ end }}
|
# Exposed ports:{{ range $container.Addresses }} {{ .Port }}/{{ .Proto }}{{ else }} (none){{ end }}
|
||||||
# Default virtual port: {{ $defaultPort }}
|
# Default virtual port: {{ $defaultPort }}
|
||||||
# VIRTUAL_PORT: {{ $container.Env.VIRTUAL_PORT }}
|
# VIRTUAL_PORT: {{ $container.Env.VIRTUAL_PORT }}
|
||||||
{{- if and $address $address.HostPort }}
|
{{- if and $addr_obj $addr_obj.HostPort }}
|
||||||
# /!\ WARNING: Virtual port published on host. Clients might be able to
|
# /!\ WARNING: Virtual port published on host. Clients might be able to
|
||||||
# bypass nginx-proxy and access the container's server
|
# bypass nginx-proxy and access the container's server
|
||||||
# directly.
|
# directly.
|
||||||
@ -125,44 +131,32 @@ upstream {{ .Upstream }} {
|
|||||||
{{- if $container.Node.ID }}
|
{{- if $container.Node.ID }}
|
||||||
# Swarm node name: {{ $container.Node.Name }}
|
# Swarm node name: {{ $container.Node.Name }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- range $knownNetwork := $networks }}
|
# Container networks:
|
||||||
# Container network reachability from {{ $knownNetwork.Name }}:
|
{{- range $containerNetwork := sortObjectsByKeysAsc $container.Networks "Name" }}
|
||||||
{{- range $containerNetwork := sortObjectsByKeysAsc $container.Networks "Name" }}
|
{{- if eq $containerNetwork.Name "ingress" }}
|
||||||
{{- if eq $containerNetwork.Name "ingress" }}
|
|
||||||
# {{ $containerNetwork.Name }} (ignored)
|
# {{ $containerNetwork.Name }} (ignored)
|
||||||
{{- else if and (ne $knownNetwork.Name $containerNetwork.Name) (ne $knownNetwork.Name "host") }}
|
{{- continue }}
|
||||||
# {{ $containerNetwork.Name }} (unreachable)
|
|
||||||
{{- else }}
|
|
||||||
# {{ $containerNetwork.Name }} (reachable)
|
|
||||||
{{- if $address }}
|
|
||||||
{{- /*
|
|
||||||
* If we got the containers from swarm and this
|
|
||||||
* container's port is published to host, use host
|
|
||||||
* IP:PORT.
|
|
||||||
*/}}
|
|
||||||
{{- if and $container.Node.ID $address.HostPort }}
|
|
||||||
{{- $server_found = true }}
|
|
||||||
server {{ $container.Node.Address.IP }}:{{ $address.HostPort }};
|
|
||||||
{{- /*
|
|
||||||
* If there is no swarm node or the port is not
|
|
||||||
* published on host, use container's IP:PORT.
|
|
||||||
*/}}
|
|
||||||
{{- else if $containerNetwork }}
|
|
||||||
{{- $server_found = true }}
|
|
||||||
server {{ $containerNetwork.IP }}:{{ $address.Port }};
|
|
||||||
{{- end }}
|
|
||||||
{{- else if $containerNetwork }}
|
|
||||||
{{- if $containerNetwork.IP }}
|
|
||||||
{{- $server_found = true }}
|
|
||||||
server {{ $containerNetwork.IP }}:{{ $port }};
|
|
||||||
{{- else }}
|
|
||||||
# /!\ No IP for this network!
|
|
||||||
{{- end }}
|
|
||||||
{{- end }}
|
|
||||||
{{- end }}
|
|
||||||
{{- else }}
|
|
||||||
# (none)
|
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
{{- if and (not (index $networks $containerNetwork.Name)) (not $networks.host) }}
|
||||||
|
# {{ $containerNetwork.Name }} (unreachable)
|
||||||
|
{{- continue }}
|
||||||
|
{{- end }}
|
||||||
|
# {{ $containerNetwork.Name }} (reachable)
|
||||||
|
{{- /*
|
||||||
|
* If we got the containers from swarm and this container's
|
||||||
|
* port is published to host, use host IP:PORT.
|
||||||
|
*/}}
|
||||||
|
{{- if and $container.Node.ID $addr_obj $addr_obj.HostPort }}
|
||||||
|
{{- $server_found = true }}
|
||||||
|
server {{ $container.Node.Address.IP }}:{{ $addr_obj.HostPort }};
|
||||||
|
{{- else if and $containerNetwork $containerNetwork.IP }}
|
||||||
|
{{- $server_found = true }}
|
||||||
|
server {{ $containerNetwork.IP }}:{{ $port }};
|
||||||
|
{{- else }}
|
||||||
|
# /!\ No IP for this network!
|
||||||
|
{{- end }}
|
||||||
|
{{- else }}
|
||||||
|
# (none)
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- /* nginx-proxy/nginx-proxy#1105 */}}
|
{{- /* nginx-proxy/nginx-proxy#1105 */}}
|
||||||
@ -293,7 +287,7 @@ server {
|
|||||||
{{- $upstream = printf "%s-%s" $upstream $sum }}
|
{{- $upstream = printf "%s-%s" $upstream $sum }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
# {{ $host }}{{ $path }}
|
# {{ $host }}{{ $path }}
|
||||||
{{ template "upstream" (dict "Upstream" $upstream "Containers" $containers "Networks" $globals.CurrentContainer.Networks) }}
|
{{ template "upstream" (dict "Upstream" $upstream "Containers" $containers "Networks" $globals.networks) }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
{{- $default_host := or ($globals.Env.DEFAULT_HOST) "" }}
|
{{- $default_host := or ($globals.Env.DEFAULT_HOST) "" }}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user