diff --git a/proxy/Dockerfile b/proxy/Dockerfile index 105e89c4..300c37a1 100644 --- a/proxy/Dockerfile +++ b/proxy/Dockerfile @@ -11,5 +11,6 @@ RUN echo "Building for '${OS}/${ARCH}'..." \ COPY templates /etc/confd/templates/ COPY conf.d /etc/confd/conf.d/ COPY nginx-proxy /usr/bin/ +COPY test/portmap.yaml /etc/confd/values.yaml ENTRYPOINT nginx-proxy \ No newline at end of file diff --git a/proxy/conf.d/nginx.toml b/proxy/conf.d/nginx.toml index 6586eb9c..26decd79 100644 --- a/proxy/conf.d/nginx.toml +++ b/proxy/conf.d/nginx.toml @@ -2,7 +2,5 @@ src = "nginx.tmpl" dest = "/etc/nginx/nginx.conf" keys = [ - "SERVERS", - "PORTS", - "UDP_PORTS", -] + "ports" +] \ No newline at end of file diff --git a/proxy/nginx-proxy b/proxy/nginx-proxy index 39722944..1a1cd53b 100755 --- a/proxy/nginx-proxy +++ b/proxy/nginx-proxy @@ -1,7 +1,9 @@ #!/bin/sh # Run confd -confd -onetime -backend env +set -e +confd -onetime -backend file -file /etc/confd/values.yaml -log-level debug +set +e # Output Configuration echo "===== Initial nginx configuration =====" diff --git a/proxy/templates/nginx.tmpl b/proxy/templates/nginx.tmpl index 43e4a286..963b61dd 100644 --- a/proxy/templates/nginx.tmpl +++ b/proxy/templates/nginx.tmpl @@ -1,3 +1,10 @@ +################################### +# Generated by confd {{datetime}} # +# ####### # +# # k3d # # +# ####### # +################################### + {{- $servers := split (getenv "SERVERS") "," -}} {{- $ports := split (getenv "PORTS") "," -}} {{- $udp_ports := split (getenv "UDP_PORTS") "," -}} @@ -12,42 +19,28 @@ events { stream { - ####### - # TCP # - ####### - {{- range $port := $ports }} - upstream server_nodes_{{ $port }} { - {{- range $server := $servers }} - server {{ $server }}:{{ $port }} max_fails=1 fail_timeout=10s; - {{- end }} - } + {{- range $portstring := lsdir "/ports" }} - server { - listen {{ $port }}; - proxy_pass server_nodes_{{ $port }}; - proxy_timeout 600; - proxy_connect_timeout 2s; - } - {{- end }} - ####### - # UDP # - ####### + {{- $portdir := printf "/ports/%s/*" $portstring -}} + {{- $port := index (split $portstring ".") 0 -}} + {{- $protocol := index (split $portstring ".") 1 -}} + {{- $upstream := replace $portstring "." "_" -1 }} - {{- range $port := $udp_ports }} - {{- if $port }} - upstream server_nodes_udp_{{ $port }} { - {{- range $server := $servers }} + upstream {{ $upstream }} { + {{- range $server := getvs $portdir }} server {{ $server }}:{{ $port }} max_fails=1 fail_timeout=10s; {{- end }} } server { - listen {{ $port }} udp; - proxy_pass server_nodes_udp_{{ $port }}; + listen {{ $port }} {{- if (eq $protocol "udp") }} udp{{- end -}}; + proxy_pass {{ $upstream }}; proxy_timeout 600; proxy_connect_timeout 2s; } + + {{- end }} - {{- end }} + } diff --git a/proxy/test/portmap.yaml b/proxy/test/portmap.yaml new file mode 100644 index 00000000..02df30c3 --- /dev/null +++ b/proxy/test/portmap.yaml @@ -0,0 +1,7 @@ +ports: + 1234.tcp: + - server-0 + - server-1 + 4321.udp: + - agent-0 + - agent-1 \ No newline at end of file