Skip to content

Commit

Permalink
Merge pull request #10 from Ox0400/master
Browse files Browse the repository at this point in the history
support multi containers
  • Loading branch information
byjg authored Mar 27, 2022
2 parents 0821f5e + 062b437 commit 82b4445
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
9 changes: 7 additions & 2 deletions easymapping/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def generate(self, lineList = []):
# static?
if len(lineList) > 0:
self.mapping["easymapping"] = self.__parse(lineList)

else:
for d in self.mapping["easymapping"]:
for name, hosts in d.get('hosts', {}).items():
if type(hosts) != list:
d['hosts'][name] = [hosts]
# still 'None' -> default to [] for jinja2
if self.mapping["easymapping"] is None:
self.mapping["easymapping"] = []
Expand Down Expand Up @@ -119,7 +123,8 @@ def __parse(self, lineList):
""
)

easymapping[key]["hosts"][d[host_label]] = "{}:{}".format(container, ct_port)
easymapping[key]["hosts"].setdefault(d[host_label], [])
easymapping[key]["hosts"][d[host_label]] += ["{}:{}".format(container, ct_port)]

# handle SSL
ssl_label = self.label.create(["sslcert", definition])
Expand Down
38 changes: 38 additions & 0 deletions examples/docker/docker-compose-multi-containers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: "3"

services:
haproxy:
image: 0x0400/easy-haproxy
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
DISCOVER: docker
HAPROXY_CUSTOMERRORS: "true"
HAPROXY_USERNAME: admin
HAPROXY_PASSWORD: password
HAPROXY_STATS_PORT: 1936
ports:
- 19901:19901


nginx:
#image: nginx
image: stenote/nginx-hostname
deploy:
replicas: 2
labels:
easyhaproxy.definitions: "http"

easyhaproxy.redirect.http: google.helloworld.com--www.google.com
easyhaproxy.host.http: www.helloworld.com
easyhaproxy.port.http: 19901
easyhaproxy.localport.http: 80

# curl -H Host:www.helloworld.com localhost:19901
# f6d8d45b7411
# 59b213cb8592

# curl -I -H Host:google.helloworld.com localhost:19901
# HTTP/1.1 301 Moved Permanently
# content-length: 0
# location: www.google.com/
4 changes: 3 additions & 1 deletion templates/haproxy.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ backend srv_{{ host }}
option tcp-check
tcp-check connect{{ " ssl" if o["health-check"] == "ssl" }}
{% endif %}
server srv {{ o["hosts"][k] }} check weight 1{{ " verify none" if o["health-check"] == "ssl" }}
{% for c in o["hosts"][k] %}
server srv-{{ loop.index0 }} {{ c }} check weight 1{{ " verify none" if o["health-check"] == "ssl" }}
{% endfor %}
{% endfor %}
{% endfor %}
2 changes: 1 addition & 1 deletion tests/expected/services-tcp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ backend srv_agent_quantum_local_31339_1
mode tcp
option tcp-check
tcp-check connect ssl
server srv test_agent:9001 check weight 1 verify none
server srv-0 test_agent:9001 check weight 1 verify none
10 changes: 5 additions & 5 deletions tests/expected/services.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ backend srv_agent_quantum_example_org_31339_1
mode tcp
option tcp-check
tcp-check connect
server srv my-stack_agent:9001 check weight 1
server srv-0 my-stack_agent:9001 check weight 1

frontend http_in_31337_2
bind *:31337
Expand All @@ -43,14 +43,14 @@ backend srv_cadvisor_quantum_example_org_31337_2
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server srv my-stack_cadvisor:8080 check weight 1
server srv-0 my-stack_cadvisor:8080 check weight 1
backend srv_node-exporter_quantum_example_org_31337_2
balance roundrobin
mode http
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server srv my-stack_node-exporter:9100 check weight 1
server srv-0 my-stack_node-exporter:9100 check weight 1

frontend http_in_80_3
bind *:80
Expand All @@ -71,7 +71,7 @@ backend srv_www_somehost_com_br_80_3
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server srv some-service:80 check weight 1
server srv-0 some-service:80 check weight 1

frontend http_in_443_4
bind *:443 ssl crt /tmp/www.somehost.com.br.1.pem
Expand All @@ -92,4 +92,4 @@ backend srv_www_somehost_com_br_443_4
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server srv some-service:80 check weight 1
server srv-0 some-service:80 check weight 1
8 changes: 4 additions & 4 deletions tests/expected/static.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ backend srv_host1_com_br_80_1
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server srv container:5000 check weight 1
server srv-0 container:5000 check weight 1
backend srv_host2_com_br_80_1
balance roundrobin
mode http
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server srv other:3000 check weight 1
server srv-0 other:3000 check weight 1

frontend http_in_443_2
bind *:443 ssl crt /etc/haproxy/certs/mycert.pem
Expand All @@ -76,7 +76,7 @@ backend srv_host1_com_br_443_2
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server srv container:80 check weight 1
server srv-0 container:80 check weight 1

frontend http_in_8080_3
bind *:8080
Expand All @@ -92,4 +92,4 @@ backend srv_host3_com_br_8080_3
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server srv domain:8181 check weight 1
server srv-0 domain:8181 check weight 1

0 comments on commit 82b4445

Please sign in to comment.