Pārlūkot izejas kodu

Use define & template for re-usable blocks of upstream server template

Viranch Mehta 10 gadi atpakaļ
vecāks
revīzija
4f5351265a
1 mainītis faili ar 19 papildinājumiem un 21 dzēšanām
  1. 19 21
      nginx.tmpl

+ 19 - 21
nginx.tmpl

@@ -1,3 +1,20 @@
+{{ define "upstream" }}
+	{{ 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 }}
+			# {{ .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 }}
+			server {{ .Address.IP }}:{{ .Address.Port }};
+		{{ end }}
+	{{ else }}
+		# {{ .Container.Name }}
+		server {{ .Container.IP }} down;
+	{{ end }}
+{{ end }}
+
 # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
 # scheme used to connect to this server
 map $http_x_forwarded_proto $proxy_x_forwarded_proto {
@@ -45,31 +62,12 @@ upstream {{ $host }} {
 	{{/* If only 1 port exposed, use that */}}
 	{{ if eq $addrLen 1 }}
 		{{ $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}}
-			server {{ $address.IP }}:{{ $address.Port }};
-		{{ end }}
+		{{ template "upstream" (dict "Container" $container "Address" $address) }}
 	{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
 	{{ else }}
 		{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
 		{{ $address := where $container.Addresses "Port" $port | first }}
-		{{ if $address }}
-			{{ if and $container.Node $address.HostPort }}
-				# {{ $container.Node.Name }}/{{$container.Name}}
-				server {{ $container.Node.Address.IP }}:{{ $address.HostPort }};
-			{{ else }}
-				# {{$container.Name}}
-				server {{ $address.IP }}:{{ $address.Port }};
-			{{ end }}
-		{{ else }}
-			# {{$container.Name}}
-			server {{ $container.IP }} down;
-		{{ end }}
+		{{ template "upstream" (dict "Container" $container "Address" $address) }}
 	{{ end }}
 {{ end }}
 }