Use container host's IP:port if we're connected to a swarm master

This commit is contained in:
Viranch Mehta 2015-07-04 18:43:52 +05:30
parent ae85ec013b
commit c4923d1f58

View file

@ -44,7 +44,13 @@ upstream {{ $host }} {
{{ $addrLen := len $container.Addresses }} {{ $addrLen := len $container.Addresses }}
{{/* If only 1 port exposed, use that */}} {{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }} {{ if eq $addrLen 1 }}
{{ with $address := index $container.Addresses 0 }} {{ $address := index $container.Addresses 0 }}
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
{{ if and $container.Node $address.HostPort }}
# {{ $container.Node.Name }}/{{$container.Name}}
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 }}
# {{$container.Name}} # {{$container.Name}}
server {{ $address.IP }}:{{ $address.Port }}; server {{ $address.IP }}:{{ $address.Port }};
{{ end }} {{ end }}
@ -52,16 +58,26 @@ upstream {{ $host }} {
{{ else if $container.Env.VIRTUAL_PORT }} {{ else if $container.Env.VIRTUAL_PORT }}
{{ range $address := .Addresses }} {{ range $address := .Addresses }}
{{ if eq $address.Port $container.Env.VIRTUAL_PORT }} {{ if eq $address.Port $container.Env.VIRTUAL_PORT }}
{{ if and $container.Node $address.HostPort }}
# {{ $container.Node.Name }}/{{$container.Name}}
server {{ $container.Node.Address.IP }}:{{ $address.HostPort }};
{{ else }}
# {{$container.Name}} # {{$container.Name}}
server {{ $address.IP }}:{{ $address.Port }}; server {{ $address.IP }}:{{ $address.Port }};
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ end }}
{{/* Else default to standard web port 80 */}} {{/* Else default to standard web port 80 */}}
{{ else }} {{ else }}
{{ $address := where $container.Addresses "Port" "80" | first }} {{ $address := where $container.Addresses "Port" "80" | first }}
{{ if $address }} {{ if $address }}
{{ if and $container.Node $address.HostPort }}
# {{ $container.Node.Name }}/{{$container.Name}}
server {{ $container.Node.Address.IP }}:{{ $address.HostPort }};
{{ else }}
# {{$container.Name}} # {{$container.Name}}
server {{ $address.IP }}:80; server {{ $address.IP }}:80;
{{ end }}
{{ else }} {{ else }}
# {{$container.Name}} # {{$container.Name}}
server {{ $container.IP }} down; server {{ $container.IP }} down;