Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit e52edde

Browse files
committed
Merge branch 'matiasdecarli-master'
2 parents 978045e + ba39673 commit e52edde

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ Settings in this part is immutable, you have to redeploy HAProxy service to make
198198

199199
|Environment Variable|Default|Description|
200200
|:-----:|:-----:|:----------|
201+
|ADDITIONAL_BACKENDS| |list of additional backends to balance. The format is `backend name, FORCE_SSL(True|False), server name, host:port, options`|
201202
|ADDITIONAL_SERVICES| |list of additional services to balance (es: `prj1:web,prj2:sql`). Discovery will be based on `com.docker.compose.[project|service]` container labels. This environment variable only works on compose v2, and the referenced services must be on a network resolvable and accessible to this containers.|
202203
|BALANCE|roundrobin|load balancing algorithm to use. Possible values include: `roundrobin`, `static-rr`, `source`, `leastconn`. See:[HAProxy:balance](https://cbonte.github.io/haproxy-dconv/configuration-1.5.html#4-balance)|
203204
|CA_CERT_FILE| |the path of a ca-cert file. This allows you to mount your ca-cert file directly from a volume instead of from envvar. If set, `CA_CERT` envvar will be ignored. Possible value: `/cacerts/cert0.pem`|
@@ -210,6 +211,7 @@ Settings in this part is immutable, you have to redeploy HAProxy service to make
210211
|EXTRA_GLOBAL_SETTINGS| |comma-separated string of extra settings, and each part will be appended to GLOBAL section in the configuration file. To escape comma, use `\,`. Possible value: `tune.ssl.cachesize 20000, tune.ssl.default-dh-param 2048`|
211212
|EXTRA_ROUTE_SETTINGS| |a string which is append to the each backend route after the health check, can be over written in the linked services. Possible value: "send-proxy"|
212213
|EXTRA_SSL_CERTS| |list of extra certificate names separated by comma, eg. `CERT1, CERT2, CERT3`. You also need to specify each certificate as separate env variables like so: `CERT1="<cert-body1>"`, `CERT2="<cert-body2>"`, `CERT3="<cert-body3>"`|
214+
|FORCE_DEFAULT_BACKEND| True | set the default_service as a default backend. This is useful when you have more than one backend and you don't want your default_service as a default backend
213215
|HEALTH_CHECK|check|set health check on each backend route, possible value: "check inter 2000 rise 2 fall 3". See:[HAProxy:check](https://cbonte.github.io/haproxy-dconv/configuration-1.5.html#5.2-check)|
214216
|HTTP_BASIC_AUTH| |a comma-separated list of credentials(`<user>:<pass>`) for HTTP basic auth, which applies to all the backend routes. To escape comma, use `\,`. *Attention:* DO NOT rely on this for authentication in production|
215217
|MAXCONN|4096|sets the maximum per-process number of concurrent connections.|
@@ -507,4 +509,4 @@ Legacy links:
507509
In most cases, `dockercloud/haproxy` will configure itself automatically when the linked services change, you don't need to reload it manually. But for some reason, if you have to do so, here is how:
508510

509511
* `docker exec <haproxy_id> /reload.sh`, if you are on the node where dockercloud/haproxy deploys
510-
* `docker-cloud exec <haproxy_uuid> /reload.sh`, if you use docker-cloud cli
512+
* `docker-cloud exec <haproxy_uuid> /reload.sh`, if you use docker-cloud cli

haproxy/config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,23 @@ def parse_extra_frontend_settings(envvars):
3232
settings_dict[port] = settings
3333
return settings_dict
3434

35+
def parse_additional_backend_settings(envvars):
36+
settings_dict = {}
37+
if isinstance(envvars, os._Environ) or isinstance(envvars, dict):
38+
frontend_settings_pattern = re.compile(r"^ADDITIONAL_BACKEND_(\w{1,9})$")
39+
for k, v in envvars.iteritems():
40+
match = frontend_settings_pattern.match(k)
41+
if match:
42+
server = match.group(1)
43+
settings = [x.strip().replace("\,", ",") for x in re.split(r'(?<!\\),', v.strip())]
44+
if server in settings_dict:
45+
settings_dict[server].extend(settings)
46+
else:
47+
settings_dict[server] = settings
48+
return settings_dict
3549

3650
# envvar
51+
ADDITIONAL_BACKENDS = parse_additional_backend_settings(os.environ)
3752
ADDITIONAL_SERVICES = os.getenv("ADDITIONAL_SERVICES")
3853
API_AUTH = os.getenv("DOCKERCLOUD_AUTH")
3954
BALANCE = os.getenv("BALANCE", "roundrobin")
@@ -48,6 +63,7 @@ def parse_extra_frontend_settings(envvars):
4863
EXTRA_GLOBAL_SETTINGS = os.getenv("EXTRA_GLOBAL_SETTINGS")
4964
EXTRA_SSL_CERT = os.getenv("EXTRA_SSL_CERTS")
5065
EXTRA_ROUTE_SETTINGS = os.getenv("EXTRA_ROUTE_SETTINGS", "")
66+
FORCE_DEFAULT_BACKEND = os.getenv("FORCE_DEFAULT_BACKEND", "True")
5167
HAPROXY_CONTAINER_URI = os.getenv("DOCKERCLOUD_CONTAINER_API_URI")
5268
HAPROXY_SERVICE_URI = os.getenv("DOCKERCLOUD_SERVICE_API_URI")
5369
HEALTH_CHECK = os.getenv("HEALTH_CHECK", "check inter 2000 rise 2 fall 3")

haproxy/haproxycfg.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def update(self):
167167
cfg_dict.update(self._config_tcp_sections())
168168
cfg_dict.update(self._config_frontend_sections())
169169
cfg_dict.update(self._config_backend_sections())
170+
cfg_dict.update(self._config_adittional_backends_sections())
170171

171172
cfg = prettify(cfg_dict)
172173
self._update_haproxy(cfg)
@@ -386,3 +387,12 @@ def _config_backend_sections(self):
386387
else:
387388
cfg["backend default_service"] = backend
388389
return cfg
390+
391+
def _config_adittional_backends_sections(self):
392+
cfg = OrderedDict()
393+
394+
if ADDITIONAL_BACKENDS:
395+
for key in ADDITIONAL_BACKENDS:
396+
cfg["backend %s" % key] = ADDITIONAL_BACKENDS[key]
397+
398+
return cfg

haproxy/helper/backend_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def get_backend_section(details, routes, vhosts, service_alias, routes_added):
1818
route_setting = " ".join([route_health_check, extra_route_settings]).strip()
1919
backend_routes = get_backend_routes(route_setting, is_sticky, routes, routes_added, service_alias)
2020
backend.extend(backend_routes)
21+
2122
return backend
2223

2324

@@ -44,7 +45,6 @@ def get_backend_routes(route_setting, is_sticky, routes, routes_added, service_a
4445

4546
return sorted(backend_routes)
4647

47-
4848
def get_route_health_check(details, service_alias, default_health_check):
4949
health_check = get_service_attribute(details, "health_check", service_alias)
5050
health_check = health_check if health_check else default_health_check

haproxy/helper/frontend_helper.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import OrderedDict
22

33
from haproxy.config import EXTRA_BIND_SETTINGS, EXTRA_FRONTEND_SETTINGS, MONITOR_URI, MONITOR_PORT, MAXCONN, \
4-
SKIP_FORWARDED_PROTO
4+
SKIP_FORWARDED_PROTO, FORCE_DEFAULT_BACKEND
55

66

77
def check_require_default_route(routes, routes_added):
@@ -152,7 +152,9 @@ def config_default_frontend(ssl_bind_string):
152152
if "80" in EXTRA_FRONTEND_SETTINGS:
153153
frontend.extend(EXTRA_FRONTEND_SETTINGS["80"])
154154

155-
frontend.append("default_backend default_service")
155+
if "True" in FORCE_DEFAULT_BACKEND:
156+
frontend.append("default_backend default_service")
157+
156158
cfg["frontend default_port_80"] = frontend
157159

158160
if ssl_bind_string:
@@ -171,7 +173,9 @@ def config_default_frontend(ssl_bind_string):
171173
if "443" in EXTRA_FRONTEND_SETTINGS:
172174
ssl_frontend.extend(EXTRA_FRONTEND_SETTINGS["443"])
173175

174-
ssl_frontend.append("default_backend default_service")
176+
if "True" in FORCE_DEFAULT_BACKEND:
177+
ssl_frontend.append("default_backend default_service")
178+
175179
cfg["frontend default_port_443"] = ssl_frontend
176180

177181
return cfg, monitor_uri_configured

0 commit comments

Comments
 (0)