From 4a1c36d003838fd0c0b6550729c66903c7f3d72f Mon Sep 17 00:00:00 2001 From: Joao M Date: Fri, 12 Aug 2022 14:49:48 +0000 Subject: [PATCH] Find definitions dynamically --- README.md | 25 ++++++++----------- easymapping/__init__.py | 20 +++++++++++---- .../docker/docker-compose-changed-label.yml | 2 -- .../docker-compose-multi-containers.yml | 2 -- examples/docker/docker-compose.yml | 2 -- tests/fixtures/services | 8 +++--- tests/fixtures/services-changed-label | 8 +++--- tests/fixtures/services-multi-containers | 4 +-- tests/fixtures/services-tcp | 2 +- 9 files changed, 37 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index bb2a85e..51224e3 100644 --- a/README.md +++ b/README.md @@ -83,16 +83,15 @@ Important: easyhaproxy needs to be in the same network of the containers or othe ### Tags to be attached in the Docker Container (Swarm or Docker) -| Tag | Description | Example | -|------------------------------------|---------------------------------------------------------------------------------------------------------|--------------| -| easyhaproxy.definitions | A Comma delimited list with the definitions. Each name requires the definition of the parameters below. | service,service2 | -| easyhaproxy.[definition].mode | (Optional) Is this `http` or `tcp` mode in HAProxy. (Defaults to http) | http | -| easyhaproxy.[definition].port | (Optional) What is the port that the HAProxy will listen to. (Defaults to 80) | 80 | -| easyhaproxy.[definition].localport | (Optional) What is the port that the container is listening. (Defaults to 80) | 8080 | -| easyhaproxy.[definition].host | What is the host that the HAProxy will listen to. | somehost.com | -| easyhaproxy.[definition].redirect | (Optional) Host redirects from connections in the port defined above. | foo.com--https://bla.com,bar.com--https://bar.org | -| easyhaproxy.[definition].sslcert | (Optional) Cert PEM Base64 encoded. | | -| easyhaproxy.[definition].health-check | (Optional) `ssl`, enable health check via SSL in `mode tcp` (Defaults to "empty") | | +| Tag | Description | Example | +|---------------------------------------|---------------------------------------------------------------------------------------------------------|--------------| +| easyhaproxy.[definition].mode | (Optional) Is this `http` or `tcp` mode in HAProxy. (Defaults to http) | http | +| easyhaproxy.[definition].port | (Optional) What is the port that the HAProxy will listen to. (Defaults to 80) | 80 | +| easyhaproxy.[definition].localport. | (Optional) What is the port that the container is listening. (Defaults to 80) | 8080 | +| easyhaproxy.[definition].host | What is the host that the HAProxy will listen to. | somehost.com | +| easyhaproxy.[definition].redirect | (Optional) Host redirects from connections in the port defined above. | foo.com--https://bla.com,bar.com--https://bar.org | +| easyhaproxy.[definition].sslcert | (Optional) Cert PEM Base64 encoded. | | +| easyhaproxy.[definition].health-check | (Optional) `ssl`, enable health check via SSL in `mode tcp` (Defaults to "empty") | | ### Defining the labels in Docker Swarm @@ -103,7 +102,8 @@ services: foo: deploy: labels: - easyhaproxy.definitions: "service1,service2" + easyhaproxy.my.host: "www.example.org" + easyhaproxy.my.localport: 8080 ... ``` @@ -112,7 +112,6 @@ services: ```bash docker run \ - -l easyhaproxy.definitions=webapi \ -l easyhaproxy.webapi.port=80\ -l easyhaproxy.webapi.host=byjg.com.br \ .... @@ -122,8 +121,6 @@ docker run \ ```bash docker run \ - -l easyhaproxy.definitions=express,admin \ - -l easyhaproxy.express.port=80 \ -l easyhaproxy.express.localport=3000 \ -l easyhaproxy.express.host=express.byjg.com.br \ diff --git a/easymapping/__init__.py b/easymapping/__init__.py index 936389a..c414bb2 100644 --- a/easymapping/__init__.py +++ b/easymapping/__init__.py @@ -3,11 +3,14 @@ import hashlib from jinja2 import Environment, FileSystemLoader import json import os +import re class DockerLabelHandler: def __init__(self, label): self.__label_base = label + def get_lookup_label(self): + return self.__label_base def create(self, key): if isinstance(key, str): @@ -70,16 +73,23 @@ class HaproxyConfigGenerator: line = line.strip() i = line.find("=") container = line[:i] - jsonStr = line[i+1:] - d = json.loads(jsonStr) + json_str = line[i+1:] + d = json.loads(json_str) - if self.label.create("definitions") not in d.keys(): + # Extract the definitions dynamically + definitions = {} + r = re.compile(self.label.get_lookup_label() + r"\.(.*)\..*") + for key in d.keys(): + if r.match(key): + definitions[r.search(key).group(1)] = 1 + + if len(definitions.keys()) == 0: continue self.label.set_data(d) - definitions = d[self.label.create("definitions")].split(",") - for definition in definitions: + # Parse each definition found. + for definition in definitions.keys(): mode = self.label.get( self.label.create([definition, "mode"]), "http" diff --git a/examples/docker/docker-compose-changed-label.yml b/examples/docker/docker-compose-changed-label.yml index 1092ebd..5183940 100644 --- a/examples/docker/docker-compose-changed-label.yml +++ b/examples/docker/docker-compose-changed-label.yml @@ -27,8 +27,6 @@ services: container: image: byjg/static-httpserver labels: - haproxy.definitions: "http,https" - haproxy.http.redirect: host1.local--https://host1.local haproxy.http.host: host1.local haproxy.http.port: 80 diff --git a/examples/docker/docker-compose-multi-containers.yml b/examples/docker/docker-compose-multi-containers.yml index b1767eb..e5ee2a5 100644 --- a/examples/docker/docker-compose-multi-containers.yml +++ b/examples/docker/docker-compose-multi-containers.yml @@ -30,8 +30,6 @@ services: deploy: replicas: 2 labels: - easyhaproxy.definitions: "http" - easyhaproxy.http.redirect: google.helloworld.com--www.google.com easyhaproxy.http.host: www.helloworld.com easyhaproxy.http.port: 19901 diff --git a/examples/docker/docker-compose.yml b/examples/docker/docker-compose.yml index 1d90116..0675f35 100644 --- a/examples/docker/docker-compose.yml +++ b/examples/docker/docker-compose.yml @@ -26,8 +26,6 @@ services: container: image: byjg/static-httpserver labels: - easyhaproxy.definitions: "http,https" - easyhaproxy.http.redirect: host1.local--https://host1.local easyhaproxy.http.host: host1.local easyhaproxy.http.port: 80 diff --git a/tests/fixtures/services b/tests/fixtures/services index c949ed7..47ed2b0 100644 --- a/tests/fixtures/services +++ b/tests/fixtures/services @@ -1,6 +1,6 @@ portainer-agent_agent={"com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"portainer-agent"} -my-stack_agent={"easyhaproxy.definitions":"agent","easyhaproxy.agent.host":"agent.quantum.example.org","easyhaproxy.agent.localport":"9001","easyhaproxy.agent.mode":"tcp","easyhaproxy.agent.port":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} -my-stack_cadvisor={"easyhaproxy.definitions":"cadvisor","easyhaproxy.cadvisor.host":"cadvisor.quantum.example.org","easyhaproxy.cadvisor.localport":"8080","easyhaproxy.cadvisor.port":"31337","com.docker.stack.image":"gcr.io/google-containers/cadvisor:v0.34.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} -my-stack_node-exporter={"easyhaproxy.definitions":"exp","easyhaproxy.exp.host":"node-exporter.quantum.example.org","easyhaproxy.exp.localport":"9100","easyhaproxy.exp.port":"31337","com.docker.stack.image":"stefanprodan/swarmprom-node-exporter:v0.16.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} +my-stack_agent={"easyhaproxy.agent.host":"agent.quantum.example.org","easyhaproxy.agent.localport":"9001","easyhaproxy.agent.mode":"tcp","easyhaproxy.agent.port":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} +my-stack_cadvisor={"easyhaproxy.cadvisor.host":"cadvisor.quantum.example.org","easyhaproxy.cadvisor.localport":"8080","easyhaproxy.cadvisor.port":"31337","com.docker.stack.image":"gcr.io/google-containers/cadvisor:v0.34.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} +my-stack_node-exporter={"easyhaproxy.exp.host":"node-exporter.quantum.example.org","easyhaproxy.exp.localport":"9100","easyhaproxy.exp.port":"31337","com.docker.stack.image":"stefanprodan/swarmprom-node-exporter:v0.16.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} my-stack_reverse-proxy={"com.docker.stack.image":"quay.io/pngmbh/easy-haproxy:tcp-mode","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} -some-service={"easyhaproxy.definitions":"http,https","easyhaproxy.http.port":"80","easyhaproxy.http.host":"www.somehost.com.br","easyhaproxy.http.localport":"80","easyhaproxy.http.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","easyhaproxy.https.port":"443","easyhaproxy.https.host":"www.somehost.com.br","easyhaproxy.https.localport":"80","easyhaproxy.https.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","easyhaproxy.https.sslcert":"U29tZSBQRU0gQ2VydGlmaWNhdGU="} \ No newline at end of file +some-service={"easyhaproxy.http.port":"80","easyhaproxy.http.host":"www.somehost.com.br","easyhaproxy.http.localport":"80","easyhaproxy.http.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","easyhaproxy.https.port":"443","easyhaproxy.https.host":"www.somehost.com.br","easyhaproxy.https.localport":"80","easyhaproxy.https.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","easyhaproxy.https.sslcert":"U29tZSBQRU0gQ2VydGlmaWNhdGU="} \ No newline at end of file diff --git a/tests/fixtures/services-changed-label b/tests/fixtures/services-changed-label index 52b2548..a6d5cc7 100644 --- a/tests/fixtures/services-changed-label +++ b/tests/fixtures/services-changed-label @@ -1,6 +1,6 @@ portainer-agent_agent={"com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"portainer-agent"} -my-stack_agent={"haproxy.definitions":"agent","haproxy.agent.host":"agent.quantum.example.org","haproxy.agent.localport":"9001","haproxy.agent.mode":"tcp","haproxy.agent.port":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} -my-stack_cadvisor={"haproxy.definitions":"cadvisor","haproxy.cadvisor.host":"cadvisor.quantum.example.org","haproxy.cadvisor.localport":"8080","haproxy.cadvisor.port":"31337","com.docker.stack.image":"gcr.io/google-containers/cadvisor:v0.34.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} -my-stack_node-exporter={"haproxy.definitions":"exp","haproxy.exp.host":"node-exporter.quantum.example.org","haproxy.exp.localport":"9100","haproxy.exp.port":"31337","com.docker.stack.image":"stefanprodan/swarmprom-node-exporter:v0.16.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} +my-stack_agent={"haproxy.agent.host":"agent.quantum.example.org","haproxy.agent.localport":"9001","haproxy.agent.mode":"tcp","haproxy.agent.port":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} +my-stack_cadvisor={"haproxy.cadvisor.host":"cadvisor.quantum.example.org","haproxy.cadvisor.localport":"8080","haproxy.cadvisor.port":"31337","com.docker.stack.image":"gcr.io/google-containers/cadvisor:v0.34.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} +my-stack_node-exporter={"haproxy.exp.host":"node-exporter.quantum.example.org","haproxy.exp.localport":"9100","haproxy.exp.port":"31337","com.docker.stack.image":"stefanprodan/swarmprom-node-exporter:v0.16.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} my-stack_reverse-proxy={"com.docker.stack.image":"quay.io/pngmbh/easy-haproxy:tcp-mode","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"} -some-service={"haproxy.definitions":"http,https","haproxy.http.port":"80","haproxy.http.host":"www.somehost.com.br","haproxy.http.localport":"80","haproxy.http.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","haproxy.https.port":"443","haproxy.https.host":"www.somehost.com.br","haproxy.https.localport":"80","haproxy.https.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","haproxy.https.sslcert":"U29tZSBQRU0gQ2VydGlmaWNhdGU="} \ No newline at end of file +some-service={"haproxy.http.port":"80","haproxy.http.host":"www.somehost.com.br","haproxy.http.localport":"80","haproxy.http.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","haproxy.https.port":"443","haproxy.https.host":"www.somehost.com.br","haproxy.https.localport":"80","haproxy.https.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","haproxy.https.sslcert":"U29tZSBQRU0gQ2VydGlmaWNhdGU="} \ No newline at end of file diff --git a/tests/fixtures/services-multi-containers b/tests/fixtures/services-multi-containers index 54e854c..7e58c70 100644 --- a/tests/fixtures/services-multi-containers +++ b/tests/fixtures/services-multi-containers @@ -1,2 +1,2 @@ -test_nginx.2.t5r94mjlced7m3t5orfjbowmm={"easyhaproxy.definitions":"http","easyhaproxy.http.host":"www.helloworld.com","easyhaproxy.http.localport":"80","easyhaproxy.http.port":"19901","com.docker.stack.image":"stenote/nginx-hostname","com.docker.stack.namespace":"test"} -test_nginx.1.p552hqxkdx88narjrp5kouwb2={"easyhaproxy.definitions":"http","easyhaproxy.http.host":"www.helloworld.com","easyhaproxy.http.localport":"80","easyhaproxy.http.port":"19901","com.docker.stack.image":"stenote/nginx-hostname","com.docker.stack.namespace":"test"} \ No newline at end of file +test_nginx.2.t5r94mjlced7m3t5orfjbowmm={"easyhaproxy.http.host":"www.helloworld.com","easyhaproxy.http.localport":"80","easyhaproxy.http.port":"19901","com.docker.stack.image":"stenote/nginx-hostname","com.docker.stack.namespace":"test"} +test_nginx.1.p552hqxkdx88narjrp5kouwb2={"easyhaproxy.http.host":"www.helloworld.com","easyhaproxy.http.localport":"80","easyhaproxy.http.port":"19901","com.docker.stack.image":"stenote/nginx-hostname","com.docker.stack.namespace":"test"} \ No newline at end of file diff --git a/tests/fixtures/services-tcp b/tests/fixtures/services-tcp index cb8d8ac..41f7410 100644 --- a/tests/fixtures/services-tcp +++ b/tests/fixtures/services-tcp @@ -1,2 +1,2 @@ -test_agent={"easyhaproxy.definitions":"agent","easyhaproxy.agent.host":"agent.quantum.local","easyhaproxy.agent.localport":"9001","easyhaproxy.agent.mode":"tcp","easyhaproxy.agent.port":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"test", "easyhaproxy.agent.health-check":"ssl"} +test_agent={"easyhaproxy.agent.host":"agent.quantum.local","easyhaproxy.agent.localport":"9001","easyhaproxy.agent.mode":"tcp","easyhaproxy.agent.port":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"test", "easyhaproxy.agent.health-check":"ssl"} test_proxy={"com.docker.stack.image":"byjg/easy-haproxy:local","com.docker.stack.namespace":"test"}