From 77dfe1a5c416241fdc75688964096cf8dc13259b Mon Sep 17 00:00:00 2001 From: M Rizwan Shaik Date: Wed, 12 Feb 2025 18:19:26 +0530 Subject: [PATCH 1/2] feat: introduce backend targeted config Co-authored-by: b1tamara --- jobs/haproxy/spec | 4 +- jobs/haproxy/templates/haproxy.config.erb | 7 +++ .../haproxy_config/backend_http_spec.rb | 48 ++++++++++++++++++- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/jobs/haproxy/spec b/jobs/haproxy/spec index e393df03..75849679 100644 --- a/jobs/haproxy/spec +++ b/jobs/haproxy/spec @@ -704,7 +704,9 @@ properties: ha_proxy.backend_config: description: | Raw HAProxy config that will be added to the default HTTP + routed HTTP backend definitions, provided either as a multiline text blob or as an array of lines. - + ha_proxy.backend_config_targeted: + description: | + Raw HAProxy config property, customizable for various protocols, that will be added to the default HTTP and Routed HTTP backends. ha_proxy.custom_http_error_files: description: | A map of status codes to errorfile contents diff --git a/jobs/haproxy/templates/haproxy.config.erb b/jobs/haproxy/templates/haproxy.config.erb index 0e52caad..47d15edd 100644 --- a/jobs/haproxy/templates/haproxy.config.erb +++ b/jobs/haproxy/templates/haproxy.config.erb @@ -869,6 +869,13 @@ backend <%= backend[:name] %> <%- if properties.ha_proxy.backend_config -%> <%= format_indented_multiline_config(p("ha_proxy.backend_config")) %> <%- end -%> + <%- backend_configs = p('ha_proxy.backend_config_targeted', {}) -%> + <%- backend_configs.keys.each do |backend_name| -%> + <%- if backend[:name] == backend_name -%> + <%= format_indented_multiline_config(backend_configs[backend_name]) -%> + <%- end -%> + <%- end -%> + <%- p('ha_proxy.custom_http_error_files', {}).keys.each do |status_code| -%> errorfile <%= status_code %> /var/vcap/jobs/haproxy/errorfiles/custom<%=status_code%>.http <%- end -%> diff --git a/spec/haproxy/templates/haproxy_config/backend_http_spec.rb b/spec/haproxy/templates/haproxy_config/backend_http_spec.rb index 94630eab..eaf61cde 100644 --- a/spec/haproxy/templates/haproxy_config/backend_http_spec.rb +++ b/spec/haproxy/templates/haproxy_config/backend_http_spec.rb @@ -39,7 +39,53 @@ expect(backend_http1).to include('custom backend config') end end - + + context 'when ha_proxy.backend_config_targeted is h1' do + let(:properties) do + { + 'backend_config_targeted' => { + 'http-routers-http1' => 'http-reuse aggressive' + } + } + end + it 'includes the config' do + expect(backend_http1).to include('http-reuse aggressive') + end + end + + context 'when ha_proxy.backend_config_targeted is h2' do + let(:properties) do + { + 'enable_http2' => true , + 'backend_ssl' => 'verify', + 'backend_config_targeted' => { + 'http-routers-http2' => 'http-reuse safe' + } + } + end + it 'includes the config' do + expect(backend_http2).to include('http-reuse safe') + end + end + + context 'when ha_proxy.backend_config_targeted is h1 and h2' do + let(:properties) do + { + 'enable_http2' => true , + 'backend_ssl' => 'verify', + 'disable_backend_http2_websockets' => true, + 'backend_config_targeted' => { + 'http-routers-http1' => 'http-reuse aggressive', + 'http-routers-http2' => 'http-reuse safe' + } + } + end + it 'includes the config' do + expect(backend_http1).to include('http-reuse aggressive') + expect(backend_http2).to include('http-reuse safe') + end + end + context 'when ha_proxy.custom_http_error_files is provided' do let(:properties) do { From 7e9f40c9fe35dc5f52a9abeb33e4ef767a8bedb9 Mon Sep 17 00:00:00 2001 From: M Rizwan Shaik Date: Thu, 13 Feb 2025 15:40:36 +0530 Subject: [PATCH 2/2] fix: fix linting --- jobs/haproxy/spec | 10 ++++++++-- jobs/haproxy/templates/haproxy.config.erb | 4 ++-- .../haproxy_config/backend_http_spec.rb | 17 ++++++++++------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/jobs/haproxy/spec b/jobs/haproxy/spec index 75849679..b31c96f4 100644 --- a/jobs/haproxy/spec +++ b/jobs/haproxy/spec @@ -705,8 +705,14 @@ properties: description: | Raw HAProxy config that will be added to the default HTTP + routed HTTP backend definitions, provided either as a multiline text blob or as an array of lines. ha_proxy.backend_config_targeted: - description: | - Raw HAProxy config property, customizable for various protocols, that will be added to the default HTTP and Routed HTTP backends. + description: | + A map of existing backends which enables more fine-grained configuration of the backend. + example: + backend_config_targeted: + http-routers-http1: | + http-reuse aggressive + http-routers-http2: | + http-reuse safe ha_proxy.custom_http_error_files: description: | A map of status codes to errorfile contents diff --git a/jobs/haproxy/templates/haproxy.config.erb b/jobs/haproxy/templates/haproxy.config.erb index 47d15edd..a1a33775 100644 --- a/jobs/haproxy/templates/haproxy.config.erb +++ b/jobs/haproxy/templates/haproxy.config.erb @@ -871,8 +871,8 @@ backend <%= backend[:name] %> <%- end -%> <%- backend_configs = p('ha_proxy.backend_config_targeted', {}) -%> <%- backend_configs.keys.each do |backend_name| -%> - <%- if backend[:name] == backend_name -%> - <%= format_indented_multiline_config(backend_configs[backend_name]) -%> + <%- if backend[:name] == backend_name -%> + <%= format_indented_multiline_config(backend_configs[backend_name]) -%> <%- end -%> <%- end -%> diff --git a/spec/haproxy/templates/haproxy_config/backend_http_spec.rb b/spec/haproxy/templates/haproxy_config/backend_http_spec.rb index eaf61cde..aa5897e5 100644 --- a/spec/haproxy/templates/haproxy_config/backend_http_spec.rb +++ b/spec/haproxy/templates/haproxy_config/backend_http_spec.rb @@ -39,7 +39,7 @@ expect(backend_http1).to include('custom backend config') end end - + context 'when ha_proxy.backend_config_targeted is h1' do let(:properties) do { @@ -48,31 +48,33 @@ } } end + it 'includes the config' do expect(backend_http1).to include('http-reuse aggressive') end end - + context 'when ha_proxy.backend_config_targeted is h2' do let(:properties) do { - 'enable_http2' => true , + 'enable_http2' => true, 'backend_ssl' => 'verify', 'backend_config_targeted' => { 'http-routers-http2' => 'http-reuse safe' } } end + it 'includes the config' do expect(backend_http2).to include('http-reuse safe') end end - + context 'when ha_proxy.backend_config_targeted is h1 and h2' do let(:properties) do { - 'enable_http2' => true , - 'backend_ssl' => 'verify', + 'enable_http2' => true, + 'backend_ssl' => 'verify', 'disable_backend_http2_websockets' => true, 'backend_config_targeted' => { 'http-routers-http1' => 'http-reuse aggressive', @@ -80,12 +82,13 @@ } } end + it 'includes the config' do expect(backend_http1).to include('http-reuse aggressive') expect(backend_http2).to include('http-reuse safe') end end - + context 'when ha_proxy.custom_http_error_files is provided' do let(:properties) do {