From 0b9d57791050710379b212ba069067efe8947032 Mon Sep 17 00:00:00 2001 From: Matej Zerovnik Date: Tue, 11 Feb 2025 20:35:46 +0100 Subject: [PATCH 1/3] Add support for cache resource Push other files --- manifests/cache.pp | 52 +++++++++++++++++++++++++++++++ spec/defines/cache_spec.rb | 41 ++++++++++++++++++++++++ templates/haproxy_cache_block.epp | 9 ++++++ 3 files changed, 102 insertions(+) create mode 100644 manifests/cache.pp create mode 100644 spec/defines/cache_spec.rb create mode 100644 templates/haproxy_cache_block.epp diff --git a/manifests/cache.pp b/manifests/cache.pp new file mode 100644 index 00000000..00409d81 --- /dev/null +++ b/manifests/cache.pp @@ -0,0 +1,52 @@ +# @summary +# Manage a HAProxy cache resource as documented in +# https://www.haproxy.com/documentation/haproxy-configuration-manual/2-4r1/#6 +# +# @param name +# Name of the cache. +# +# @param total_max_size +# Size of cache in megabytes. Maximum value is 4095. +# +# @param max_object_size +# Maximum size of object to be cached. Must not be bigger than half of total_max_size. +# If not set, it equals to 1/256th of total cache size. +# +# @param max_age +# Maximum expiration time in seconds. The expiration is set as the lowest +# value between the s-maxage or max-age (in this order) directive in the +# Cache-Control response header and this value. +# +# @param instance +# Optional. Defaults to 'haproxy'. +# +define haproxy::cache ( + Integer[1,4095] $total_max_size, + Optional[Integer] $max_object_size = undef, + Integer $max_age = 60, + # Integer $max_secondary_entries + String $instance = 'haproxy', +) { + include haproxy::params + if $instance == 'haproxy' { + $instance_name = 'haproxy' + $config_file = $haproxy::config_file + } else { + $instance_name = "haproxy-${instance}" + $config_file = inline_template($haproxy::params::config_file_tmpl) + } + + $parameters = { + 'name' => $name, + 'total_max_size' => $total_max_size, + 'max_object_size' => $max_object_size, + 'max_age' => $max_age, + } + + # Templates uses $ipaddresses, $server_name, $ports, $option + concat::fragment { "${instance_name}-cache-${name}": + order => "40-cache-${name}", + target => $config_file, + content => epp('haproxy/haproxy_cache_block.epp', $parameters), + } +} diff --git a/spec/defines/cache_spec.rb b/spec/defines/cache_spec.rb new file mode 100644 index 00000000..4ed9f09c --- /dev/null +++ b/spec/defines/cache_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'haproxy::cache' do + let :pre_condition do + 'class{"haproxy": + config_file => "/tmp/haproxy.cfg" + } + ' + end + let(:title) { 'dero' } + let(:facts) do + { + networking: { + ip: '1.1.1.1', + hostname: 'dero' + }, + concat_basedir: '/foo', + os: { + family: 'RedHat' + } + } + end + + context 'with a single cache entry' do + let(:params) do + { + total_max_size: 1 + } + end + + it { + is_expected.to contain_concat__fragment('haproxy-cache-dero').with( + 'order' => '40-cache-dero', + 'target' => '/tmp/haproxy.cfg', + 'content' => "\ncache dero\n total-max-size 1\n max-age 60\n", + ) + } + end +end diff --git a/templates/haproxy_cache_block.epp b/templates/haproxy_cache_block.epp new file mode 100644 index 00000000..94c64d46 --- /dev/null +++ b/templates/haproxy_cache_block.epp @@ -0,0 +1,9 @@ + +cache <%= $name %> + total-max-size <%= $total_max_size %> +<% if $max_object_size { -%> + max-object-size <%= $max_object_size %> +<% } -%> +<% if $max_age { -%> + max-age <%= $max_age %> +<% } -%> From 6d1dfd1c3279ca786c50ff64b65d248bfc52b297 Mon Sep 17 00:00:00 2001 From: Matej Zerovnik Date: Tue, 11 Feb 2025 21:15:19 +0100 Subject: [PATCH 2/3] Add extra options --- manifests/cache.pp | 30 +++++++++++++------ spec/defines/cache_spec.rb | 2 +- ...roxy_cache_block.epp => haproxy_cache.epp} | 6 +++- 3 files changed, 27 insertions(+), 11 deletions(-) rename templates/{haproxy_cache_block.epp => haproxy_cache.epp} (52%) diff --git a/manifests/cache.pp b/manifests/cache.pp index 00409d81..8b00975f 100644 --- a/manifests/cache.pp +++ b/manifests/cache.pp @@ -1,6 +1,6 @@ # @summary # Manage a HAProxy cache resource as documented in -# https://www.haproxy.com/documentation/haproxy-configuration-manual/2-4r1/#6 +# https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#6 # # @param name # Name of the cache. @@ -17,6 +17,15 @@ # value between the s-maxage or max-age (in this order) directive in the # Cache-Control response header and this value. # +# @param process_vary +# Available since HAProxy 2.4. Turn on or off the processing of the Vary header +# in a response. For more info, check https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#6.2.1-process-vary +# +# @param max_secondary_entries +# Available since HAProxy 2.4. Define the maximum number of simultaneous secondary +# entries with the same primary key in the cache. This needs the vary support to +# be enabled. Its default value is 10 and should be passed a strictly positive integer. +# # @param instance # Optional. Defaults to 'haproxy'. # @@ -24,8 +33,9 @@ Integer[1,4095] $total_max_size, Optional[Integer] $max_object_size = undef, Integer $max_age = 60, - # Integer $max_secondary_entries - String $instance = 'haproxy', + Optional[Enum['on', 'off']] $process_vary = undef, + Optional[Integer[1]] $max_secondary_entries = undef, + String $instance = 'haproxy', ) { include haproxy::params if $instance == 'haproxy' { @@ -37,16 +47,18 @@ } $parameters = { - 'name' => $name, - 'total_max_size' => $total_max_size, - 'max_object_size' => $max_object_size, - 'max_age' => $max_age, + 'name' => $name, + 'total_max_size' => $total_max_size, + 'max_object_size' => $max_object_size, + 'max_age' => $max_age, + 'process_vary' => $process_vary, + 'max_secondary_entries' => $max_secondary_entries, } # Templates uses $ipaddresses, $server_name, $ports, $option concat::fragment { "${instance_name}-cache-${name}": - order => "40-cache-${name}", + order => "30-cache-${name}", target => $config_file, - content => epp('haproxy/haproxy_cache_block.epp', $parameters), + content => epp('haproxy/haproxy_cache.epp', $parameters), } } diff --git a/spec/defines/cache_spec.rb b/spec/defines/cache_spec.rb index 4ed9f09c..237a160c 100644 --- a/spec/defines/cache_spec.rb +++ b/spec/defines/cache_spec.rb @@ -32,7 +32,7 @@ it { is_expected.to contain_concat__fragment('haproxy-cache-dero').with( - 'order' => '40-cache-dero', + 'order' => '30-cache-dero', 'target' => '/tmp/haproxy.cfg', 'content' => "\ncache dero\n total-max-size 1\n max-age 60\n", ) diff --git a/templates/haproxy_cache_block.epp b/templates/haproxy_cache.epp similarity index 52% rename from templates/haproxy_cache_block.epp rename to templates/haproxy_cache.epp index 94c64d46..b4c9f4c3 100644 --- a/templates/haproxy_cache_block.epp +++ b/templates/haproxy_cache.epp @@ -4,6 +4,10 @@ cache <%= $name %> <% if $max_object_size { -%> max-object-size <%= $max_object_size %> <% } -%> -<% if $max_age { -%> max-age <%= $max_age %> +<% if $process_vary { -%> + process-vary <%= $process_vary %> +<% } -%> +<% if $max_secondary_entries { -%> + max-secondary-entries <%= $max_secondary_entries %> <% } -%> From b1170b7305d9ac20f81731735dd22d49d48fa48a Mon Sep 17 00:00:00 2001 From: Matej Zerovnik Date: Tue, 11 Feb 2025 21:23:49 +0100 Subject: [PATCH 3/3] Add docs --- REFERENCE.md | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/REFERENCE.md b/REFERENCE.md index cf18b5d8..46e39010 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -18,6 +18,8 @@ haproxy.cfg file on an haproxy load balancer. * [`haproxy::balancermember`](#haproxy--balancermember): This type will setup a balancer member inside a listening service configuration block in /etc/haproxy/haproxy.cfg on the load balancer. +* [`haproxy::cache`](#haproxy--cache): Manage a HAProxy cache resource as documented in +https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#6 * [`haproxy::defaults`](#haproxy--defaults): This type will setup a additional defaults configuration block inside the haproxy.cfg file on an haproxy load balancer. * [`haproxy::frontend`](#haproxy--frontend): This type will setup a frontend service configuration block inside @@ -708,6 +710,79 @@ Optional. Defaults to 'server' Default value: `'server'` +### `haproxy::cache` + +Manage a HAProxy cache resource as documented in +https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#6 + +#### Parameters + +The following parameters are available in the `haproxy::cache` defined type: + +* [`name`](#-haproxy--cache--name) +* [`total_max_size`](#-haproxy--cache--total_max_size) +* [`max_object_size`](#-haproxy--cache--max_object_size) +* [`max_age`](#-haproxy--cache--max_age) +* [`process_vary`](#-haproxy--cache--process_vary) +* [`max_secondary_entries`](#-haproxy--cache--max_secondary_entries) +* [`instance`](#-haproxy--cache--instance) + +##### `name` + +Name of the cache. + +##### `total_max_size` + +Data type: `Integer[1,4095]` + +Size of cache in megabytes. Maximum value is 4095. + +##### `max_object_size` + +Data type: `Optional[Integer]` + +Maximum size of object to be cached. Must not be bigger than half of total_max_size. +If not set, it equals to 1/256th of total cache size. + +Default value: `undef` + +##### `max_age` + +Data type: `Integer` + +Maximum expiration time in seconds. The expiration is set as the lowest +value between the s-maxage or max-age (in this order) directive in the +Cache-Control response header and this value. + +Default value: `60` + +##### `process_vary` + +Data type: `Optional[Enum['on', 'off']]` + +Available since HAProxy 2.4. Turn on or off the processing of the Vary header +in a response. For more info, check https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#6.2.1-process-vary + +Default value: `undef` + +##### `max_secondary_entries` + +Data type: `Optional[Integer[1]]` + +Available since HAProxy 2.4. Define the maximum number of simultaneous secondary +entries with the same primary key in the cache. This needs the vary support to +be enabled. Its default value is 10 and should be passed a strictly positive integer. + +Default value: `undef` + +##### `instance` + +Data type: `String` + +Optional. Defaults to 'haproxy'. + +Default value: `'haproxy'` + ### `haproxy::defaults` This type will setup a additional defaults configuration block inside the