Skip to content

Commit 4917687

Browse files
authored
feat: introduce additional flag for health check with proxy protocol (#850)
feat: introduce an additional flag for health check with proxy protocol
1 parent 3c10da5 commit 4917687

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

jobs/haproxy/spec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,8 @@ properties:
577577
ha_proxy.disable_health_check_proxy:
578578
description: "Disables the use of the PROXY protocol for health checks. Only applies if `ha_proxy.accept_proxy` is enabled."
579579
default: false
580+
ha_proxy.enable_additional_health_check_proxy:
581+
description: "Enable the additional health check listener with use of the PROXY protocol"
580582
ha_proxy.binding_ip:
581583
description: "If there are multiple ethernet interfaces, specify which one to bind. Set to `::` to bind to all IPv6 interfaces (no IPv4). IPv6 must be enabled on the HAProxy VM in the deployment manifest."
582584
default: ""
@@ -599,7 +601,7 @@ properties:
599601
- 172.168.4.1/32
600602
- 10.2.0.0/16
601603
ha_proxy.expect_proxy_cidrs:
602-
description: "List of CIDRs to enable proxy protocol for. This enables forwarding of the client source IP for hyperscalers not supporting IP dual stack (v4 & v6). This property is mutually exclusive with the accept_proxy."
604+
description: "List of CIDRs to enable proxy protocol for. This enables the forwarding of the client source IP for hyperscalers that do not support IP dual stack (v4 & v6). This property is mutually exclusive with the accept_proxy. For backward compatibility, if the list is not empty, HAProxy will listen on an additional health check port (health_check_port + 1) with proxy protocol enabled, by implicitly setting enable_additional_health_check_proxy to true if not set explicitly."
603605
default: ~
604606
example:
605607
expect_proxy_cidrs:

jobs/haproxy/templates/haproxy.config.erb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ end
271271
if backend_ssl != "" && (enable_http2 || backend_match_http_protocol)
272272
backends += [{ name: "http-routers-http2", backend_ssl: backend_ssl, alpn: "alpn h2,http/1.1 " }]
273273
end
274+
275+
# to keep backward compatibility enable_additional_health_check_proxy if expect_proxy_cidrs is not empty.
276+
enable_additional_health_check_proxy = p("ha_proxy.enable_additional_health_check_proxy", p("ha_proxy.expect_proxy_cidrs", []).size > 0)
277+
274278
-%>
275279

276280
global
@@ -385,7 +389,7 @@ listen health_check_http_url
385389
acl http-routers_down nbsrv(<%= backends.first[:name] %>) eq 0
386390
monitor fail if http-routers_down
387391

388-
<%- if p("ha_proxy.expect_proxy_cidrs", []).size > 0 -%>
392+
<%- if enable_additional_health_check_proxy -%>
389393
listen health_check_http_url_proxy_protocol
390394
bind :<%= p("ha_proxy.health_check_port") + 1 %> accept-proxy
391395
mode http
@@ -1101,7 +1105,7 @@ listen health_check_http_tcp-<%= tcp_proxy["name"] %>
11011105
acl tcp-<%= tcp_proxy["name"] %>-routers_down nbsrv(tcp-<%= tcp_proxy["name"] %>) eq 0
11021106
monitor fail if tcp-<%= tcp_proxy["name"] %>-routers_down
11031107

1104-
<%- if p("ha_proxy.expect_proxy_cidrs", []).size > 0 -%>
1108+
<%- if enable_additional_health_check_proxy -%>
11051109
listen health_check_http_tcp-<%= tcp_proxy["name"] %>_proxy_protocol
11061110
bind :<%= tcp_proxy["health_check_http"] + 1 %> accept-proxy
11071111
mode http

spec/haproxy/templates/haproxy_config/healthcheck_listener_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
context 'when ha_proxy.enable_health_check_http is true' do
1111
let(:healthcheck_listener) { haproxy_conf['listen health_check_http_url'] }
12+
let(:healthcheck_listener_proxy_protocol) { haproxy_conf['listen health_check_http_url_proxy_protocol'] }
1213

1314
let(:properties) do
1415
{
@@ -84,6 +85,50 @@
8485
expect(healthcheck_listener).not_to include('tcp-request connection expect-proxy layer4 unless LOCALHOST')
8586
end
8687
end
88+
89+
context 'when ha_proxy.enable_additional_health_check_proxy is also true' do
90+
let(:properties) do
91+
{
92+
'enable_health_check_http' => true,
93+
'accept_proxy' => true,
94+
'enable_additional_health_check_proxy' => true
95+
}
96+
end
97+
98+
it 'sets expect-proxy for the healthchecks on ports 8080 and 8081' do
99+
expect(healthcheck_listener).to include('bind :8080')
100+
expect(healthcheck_listener).to include('tcp-request connection expect-proxy layer4 unless LOCALHOST')
101+
expect(healthcheck_listener_proxy_protocol).to include('bind :8081 accept-proxy')
102+
end
103+
end
104+
105+
context 'when expect_proxy_cidrs is not empty due to backward compatibility' do
106+
let(:properties) do
107+
{
108+
'enable_health_check_http' => true,
109+
'expect_proxy_cidrs' => ['10.5.6.7/27']
110+
}
111+
end
112+
113+
it 'sets expect-proxy for the healthcheck on port 8081' do
114+
expect(healthcheck_listener).to include('bind :8080')
115+
expect(healthcheck_listener_proxy_protocol).to include('bind :8081 accept-proxy')
116+
end
117+
end
118+
119+
context 'when ha_proxy.enable_additional_health_check_proxy is false but accept_proxy true' do
120+
let(:properties) do
121+
{
122+
'enable_health_check_http' => true,
123+
'accept_proxy' => true
124+
}
125+
end
126+
127+
it 'does not contain healthcheck_listener_proxy_protocol' do
128+
expect(healthcheck_listener).to include('tcp-request connection expect-proxy layer4 unless LOCALHOST')
129+
expect(haproxy_conf).not_to have_key('listen health_check_http_url_proxy_protocol')
130+
end
131+
end
87132
end
88133
end
89134
end

0 commit comments

Comments
 (0)