Allow separate listen port in haproxy-config templates

This allows a service to pass in a different listening port than haproxy
listens on for the service. This allows haproxy to live on the same
network interface as the services, and will be used by services in
subsequent changes.

Change-Id: Id1d2429a4c800628cb43e0fb7d2b69131e71ee1c
Implements: blueprint service-hostnames
This commit is contained in:
Jim Rollenhagen 2018-12-27 12:18:15 -05:00
parent 4ded787e79
commit 3ea51cefe9
2 changed files with 13 additions and 8 deletions

View File

@ -6,7 +6,8 @@ userlist {{ service_name }}-user
user {{ auth_user }} insecure-password {{ auth_pass }} user {{ auth_user }} insecure-password {{ auth_pass }}
{% endmacro %} {% endmacro %}
{%- macro listen_macro(service_name, service_port, service_mode, external, {%- macro listen_macro(service_name, service_port, listen_port,
service_mode, external,
haproxy_http_extra, haproxy_tcp_extra, host_group, haproxy_http_extra, haproxy_tcp_extra, host_group,
custom_member_list, auth_user, auth_pass) %} custom_member_list, auth_user, auth_pass) %}
listen {{ service_name }} listen {{ service_name }}
@ -56,7 +57,7 @@ listen {{ service_name }}
{% set api_interface = "ansible_%s"|format(hostvars[host]['api_interface']) %} {% set api_interface = "ansible_%s"|format(hostvars[host]['api_interface']) %}
{% set host_name = hostvars[host]['ansible_hostname'] %} {% set host_name = hostvars[host]['ansible_hostname'] %}
{% set host_ip = hostvars[host][api_interface]['ipv4']['address'] %} {% set host_ip = hostvars[host][api_interface]['ipv4']['address'] %}
server {{ host_name }} {{ host_ip }}:{{ service_port }} {{ haproxy_health_check }} server {{ host_name }} {{ host_ip }}:{{ listen_port }} {{ haproxy_health_check }}
{% endfor %} {% endfor %}
{% endif %} {% endif %}
{% endif %} {% endif %}
@ -69,6 +70,8 @@ listen {{ service_name }}
{# Skip anything that is external when the external vip is not enabled #} {# Skip anything that is external when the external vip is not enabled #}
{% if haproxy_service.enabled|bool and (not external or haproxy_enable_external_vip|bool)%} {% if haproxy_service.enabled|bool and (not external or haproxy_enable_external_vip|bool)%}
{# Here we define variables and their defaults #} {# Here we define variables and their defaults #}
{# services can be listening on a different port than haproxy #}
{% set listen_port = haproxy_service.listen_port|default(haproxy_service.port) %}
{# Custom member list can use jinja to generate a semicolon separated list #} {# Custom member list can use jinja to generate a semicolon separated list #}
{% set custom_member_list = haproxy_service.custom_member_list|default(none) %} {% set custom_member_list = haproxy_service.custom_member_list|default(none) %}
{# Mode defaults to http #} {# Mode defaults to http #}
@ -84,8 +87,8 @@ listen {{ service_name }}
{% if auth_user and auth_pass %} {% if auth_user and auth_pass %}
{{ userlist_macro(haproxy_name, auth_user, auth_pass) }} {{ userlist_macro(haproxy_name, auth_user, auth_pass) }}
{% endif %} {% endif %}
{{ listen_macro(haproxy_name, haproxy_service.port, mode, external, {{ listen_macro(haproxy_name, haproxy_service.port, listen_port,
haproxy_http_extra, haproxy_tcp_extra, host_group, mode, external, haproxy_http_extra, haproxy_tcp_extra,
custom_member_list, auth_user, auth_pass) }} host_group, custom_member_list, auth_user, auth_pass) }}
{% endif %} {% endif %}
{%- endfor -%} {%- endfor -%}

View File

@ -45,7 +45,7 @@ frontend {{ service_name }}_front
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}
{%- macro backend_macro(service_name, service_port, service_mode, host_group, {%- macro backend_macro(service_name, listen_port, service_mode, host_group,
custom_member_list, backend_http_extra, custom_member_list, backend_http_extra,
backend_tcp_extra, auth_user, auth_pass) %} backend_tcp_extra, auth_user, auth_pass) %}
backend {{ service_name }}_back backend {{ service_name }}_back
@ -77,7 +77,7 @@ backend {{ service_name }}_back
{% set api_interface = "ansible_%s"|format(hostvars[host]['api_interface']) %} {% set api_interface = "ansible_%s"|format(hostvars[host]['api_interface']) %}
{% set host_name = hostvars[host]['ansible_hostname'] %} {% set host_name = hostvars[host]['ansible_hostname'] %}
{% set host_ip = hostvars[host][api_interface]['ipv4']['address'] %} {% set host_ip = hostvars[host][api_interface]['ipv4']['address'] %}
server {{ host_name }} {{ host_ip }}:{{ service_port }} {{ haproxy_health_check }} server {{ host_name }} {{ host_ip }}:{{ listen_port }} {{ haproxy_health_check }}
{% endfor %} {% endfor %}
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}
@ -89,6 +89,8 @@ backend {{ service_name }}_back
{# Skip anything that is external when the external vip is not enabled #} {# Skip anything that is external when the external vip is not enabled #}
{% if haproxy_service.enabled|bool and (not external or haproxy_enable_external_vip|bool)%} {% if haproxy_service.enabled|bool and (not external or haproxy_enable_external_vip|bool)%}
{# Here we define variables and their defaults #} {# Here we define variables and their defaults #}
{# services can be listening on a different port than haproxy #}
{% set listen_port = haproxy_service.listen_port|default(haproxy_service.port) %}
{# Custom member list can use jinja to generate a semicolon separated list #} {# Custom member list can use jinja to generate a semicolon separated list #}
{% set custom_member_list = haproxy_service.custom_member_list|default() %} {% set custom_member_list = haproxy_service.custom_member_list|default() %}
{# Mode defaults to http #} {# Mode defaults to http #}
@ -110,7 +112,7 @@ backend {{ service_name }}_back
frontend_http_extra, frontend_tcp_extra) }} frontend_http_extra, frontend_tcp_extra) }}
{# Redirect (to https) is a special case, as it does not include a backend #} {# Redirect (to https) is a special case, as it does not include a backend #}
{% if haproxy_service.mode != 'redirect' %} {% if haproxy_service.mode != 'redirect' %}
{{ backend_macro(haproxy_name, haproxy_service.port, mode, host_group, {{ backend_macro(haproxy_name, listen_port, mode, host_group,
custom_member_list, backend_http_extra, backend_tcp_extra, custom_member_list, backend_http_extra, backend_tcp_extra,
auth_user, auth_pass) }} auth_user, auth_pass) }}
{% endif %} {% endif %}