From 154e2781d9debe5417bdec14689ace88eb4ea3e3 Mon Sep 17 00:00:00 2001 From: Lars Kellogg-Stedman Date: Sat, 4 Oct 2014 17:54:03 -0400 Subject: added hautoproxy auto-configuring haproxy This image configures haproxy to forward connections for all available kubernetes services. It is meant to be run alongside other contains in a kubernetes pod to provide access to "remote" services at a consistent address so that keystone api endpoints can be configured in a sane fashion. Change-Id: Ic923c6a772f1bdf36b97b05a1d04de9e5b841ddd --- docker/hautoproxy/start.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 docker/hautoproxy/start.py (limited to 'docker/hautoproxy/start.py') diff --git a/docker/hautoproxy/start.py b/docker/hautoproxy/start.py new file mode 100755 index 000000000..877b22157 --- /dev/null +++ b/docker/hautoproxy/start.py @@ -0,0 +1,65 @@ +#!/usr/bin/python + +'''This script configures and starts a local haproxy instances, bound to +127.0.0.1, that forwards connections all of the discovered +docker/kubernetes environment variables.''' + +import argparse +import os +import sys +from jinja2 import Environment, FileSystemLoader +import re +import urlparse + +re_url = re.compile( + '^(?P.*)_PORT_(?P\d+)_(?P(UDP|TCP))$') + +def parse_args(): + p = argparse.ArgumentParser() + p.add_argument('--output', '-o', + default='/etc/haproxy/haproxy.cfg') + p.add_argument('--no-start', '-n', + action='store_true') + p.add_argument('--template-dir', '-t', + default='/etc/haproxy/templates') + return p.parse_args() + +def discover_services(): + services = [] + for k in os.environ: + mo = re_url.match(k) + + if mo: + parts = urlparse.urlparse(os.environ[k]) + remote_host,remote_port = parts.netloc.split(':') + service_name = '%(name)s-%(port)s' % mo.groupdict() + + services.append({ + 'remote_name': mo.group('name'), + 'remote_addr': remote_host, + 'remote_port': remote_port, + 'remote_proto': parts.scheme, + 'local_port': mo.group('port'), + 'service_name': service_name, + }) + + return services + +def main(): + args = parse_args() + services = discover_services() + + env = Environment(loader=FileSystemLoader(['.', + args.template_dir])) + template = env.get_template('haproxy.cfg.tmpl') + with open(args.output, 'w') as fd: + fd.write(template.render(services=services)) + + if args.no_start: + return + + os.execlp('haproxy', 'haproxy', '-f', args.output, '-db') + +if __name__ == '__main__': + main() + -- cgit v1.2.3