1
0
Fork 0

Refactor: tried to refactor code

- move scripts to assets/scripts (and updated build)
 - put most logic into HaproxyConfigGenerator
 - created DockerLabelHandler for all label handling from previous code
 - added unit tests and fixtures to cover HaproxyConfigGenerator and DockerLabelHandler
 - added a Makefile with build (for docker build) and test targets
This commit is contained in:
till 2020-05-30 22:24:40 +02:00
parent 5f9eac4b8a
commit 08de132450
No known key found for this signature in database
GPG key ID: B119050E2EBA1DC5
21 changed files with 387 additions and 131 deletions

8
templates/bind.j2 Normal file
View file

@ -0,0 +1,8 @@
{% if "ssl_cert" in o %}
bind *:{{ o["port"] }} ssl crt {{ o["ssl_cert"] }}
{% elif "h2" in o and o["h2"] %}
bind *:{{ o["port"] }} proto h2
option http-use-htx
{% else %}
bind *:{{ o["port"] }}
{% endif %}

View file

@ -0,0 +1,11 @@
mode http
{% for k in o["redirect"] %}
redirect prefix {{ o["redirect"][k] }} code 301 if { hdr(host) -i {{ k }} }
{% endfor %}
{% for k in o["hosts"] %}
{% set host = k.replace(".", "_") + "_{0}_{1}".format(o["port"], salt) %}
acl is_rule_{{ host }}_1 hdr(host) -i {{ k }}
acl is_rule_{{ host }}_2 hdr(host) -i {{ k }}:{{ o["port"] }}
use_backend srv_{{ host }} if is_rule_{{ host }}_1 OR is_rule_{{ host }}_2
{% endfor %}

View file

@ -0,0 +1,6 @@
mode tcp
option tcplog
log global
{% set backend = (o["hosts"]|first) %}
default_backend srv_{{ backend.replace(".", "_") + "_{0}_{1}".format(o["port"], salt) }}

View file

@ -1,3 +1,8 @@
global
log stdout format raw local0 info
maxconn 2000
tune.ssl.default-dh-param 2048
defaults
log global
@ -14,11 +19,6 @@ defaults
errorfile 504 /etc/haproxy/errors-custom/504.http
{% endif %}
global
log stdout format raw local0 info
maxconn 2000
tune.ssl.default-dh-param 2048
{% if "stats" in data %}
frontend stats
bind *:{{ data["stats"]["port"] | default(1936) }}
@ -37,43 +37,31 @@ backend srv_stats
mode http
server Local 127.0.0.1:{{ data["stats"]["port"] | default(1936) }}
{% endif %}
{% for o in data["easymapping"] %}
{% for o in data["easymapping"] -%}
{% set mode = o["mode"] or "http" %}
{% set salt = loop.index %}
frontend {{ mode }}_in_{{ o["port"] }}_{{ salt }}
bind *:{{ o["port"] }} {{ " ssl crt " + o["ssl_cert"] if "ssl_cert" in o else "" }}
mode {{ mode }}
{% include "bind.j2" %}
{% if mode == "http" %}
{% include "frontend-mode-http.j2" %}
{% else %}
{% include "frontend-mode-tcp.j2" %}
{% endif %}
{% if mode == "tcp" -%}
option tcplog
tcp-request inspect-delay 5s
tcp-request content accept if { req.ssl_hello_type 1 }
{% endif -%}
{% for k in o["redirect"] -%}
redirect prefix {{ o["redirect"][k] }} code 301 if { hdr(host) -i {{ k }} }
{% endfor -%}
{% for k in o["hosts"] %}
{% set host = k.replace(".", "_") + "_{0}_{1}".format(o["port"], salt) %}
acl is_rule_{{ host }}_1 hdr(host) -i {{ k }}
acl is_rule_{{ host }}_2 hdr(host) -i {{ k }}:{{ o["port"] }}
use_backend srv_{{ host }} if is_rule_{{ host }}_1 OR is_rule_{{ host }}_2
{% endfor %}
{% for k in o["hosts"] %}
{% for k in o["hosts"] -%}
{% set host = k.replace(".", "_") + "_{0}_{1}".format(o["port"], salt) %}
backend srv_{{ host }}
balance roundrobin
mode {{ mode }}
{% if mode == "http" %}
{% if mode == "http" %}
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
{% endif %}
{% elif mode == "tcp" %}
option tcp-check
tcp-check connect
{% endif %}
server srv {{ o["hosts"][k] }} check weight 1
{% endfor %}
{% endfor %}