Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option property to haproxy_listen #470

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This file is used to list changes made in each version of the haproxy cookbook.

## Unreleased

- Add `option` property to `haproxy_listen` - [@eheydrick](https://github.com/eheydrick)

## 12.2.2 - *2021-10-05*

## 12.2.1 - *2021-08-30*
Expand Down
1 change: 1 addition & 0 deletions documentation/haproxy_listen.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ This resource also uses the following partial resources:
| `server` | Array | None | Servers the listen section routes to |
| `stats` | Hash | None | Enable stats with various options |
| `hash_type` | String | None | Specify a method to use for mapping hashes to servers | `consistent`, `map-based` |
| `option` | Array | None | Array of HAProxy `option` directives |

## Examples

Expand Down
8 changes: 8 additions & 0 deletions resources/listen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
property :acl, Array,
description: 'Access control list items'

property :option, Array,
description: 'Array of HAProxy option directives'

property :server, Array,
description: 'Servers the listen section routes to'

Expand Down Expand Up @@ -95,6 +98,11 @@
haproxy_config_resource.variables['listen'][new_resource.name]['server'].push(new_resource.server)
end

if property_is_set?(:option)
haproxy_config_resource.variables['listen'][new_resource.name]['option'] ||= []
haproxy_config_resource.variables['listen'][new_resource.name]['option'].push(new_resource.option)
end

haproxy_config_resource.variables['listen'][new_resource.name]['hash_type'] = new_resource.hash_type if property_is_set?(:hash_type)
haproxy_config_resource.variables['listen'][new_resource.name]['extra_options'] = new_resource.extra_options if property_is_set?(:extra_options)
end
Expand Down
7 changes: 7 additions & 0 deletions templates/default/haproxy.cfg.erb
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,13 @@ listen <%= key %>
<% if listen['default_backend'] -%>
default_backend <%= listen['default_backend'] %>
<% end %>
<% unless nil_or_empty?(listen['option']) %>
<% listen['option'].each do | option |%>
<% option.each do | option | %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say I am running haproxy in my envs, the example seems to be a flat array but it looks like we are iterating as if its an array of arrays. Can this be an array of array options? If so could we add an example to the documentation?

Copy link
Contributor

@bmhughes bmhughes Jan 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got to agree, can we confirm this is correct as it looks like a mistake?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it does look funny. I based it on how it's done for frontend and backend. Looks like it's because :option takes an Array which then gets pushed into another array. And so the template has to iterate over both array dimensions.

We have the same situation with :acl except it gets flattened into a 1D array, hence no need for the extra iteration.

We could switch to the flatten approach for option or merge this as-is and do the cleanup later.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, I don't really have the time to dig into this to see why the code was written that way but it's certainly a way to do that. I lean towards using flatten as we have an established pattern and that is a little clearer as to the intent (without reading a lot of code) even if they are both equally correct/complete. That being said I could be talked into accepting it as is if other reviewers agree.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to switch to flatten if nobody objects.

option <%= option %>
<% end -%>
<% end -%>
<% end -%>
<% unless nil_or_empty?(listen['extra_options']) %>
<% listen['extra_options'].each do | key, value | %>
<% if key == 'http-request' %>
Expand Down
1 change: 1 addition & 0 deletions test/cookbooks/test/recipes/config_4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
]
http_response 'set-header Expires %[date(3600),http_date]'
default_backend 'servers'
option ['dontlog-normal']
extra_options('bind-process' => 'odd')
hash_type 'consistent'
end
Expand Down
1 change: 1 addition & 0 deletions test/integration/config_4/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
' http-request add-header X-Proto http',
' http-response set-header Expires %\[date\(3600\),http_date\]',
' default_backend servers',
' option dontlog-normal',
' bind-process odd',
' hash-type consistent',
'',
Expand Down