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 diff --git a/manifests/cache.pp b/manifests/cache.pp new file mode 100644 index 00000000..8b00975f --- /dev/null +++ b/manifests/cache.pp @@ -0,0 +1,64 @@ +# @summary +# Manage a HAProxy cache resource as documented in +# https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#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 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'. +# +define haproxy::cache ( + Integer[1,4095] $total_max_size, + Optional[Integer] $max_object_size = undef, + Integer $max_age = 60, + Optional[Enum['on', 'off']] $process_vary = undef, + Optional[Integer[1]] $max_secondary_entries = undef, + 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, + 'process_vary' => $process_vary, + 'max_secondary_entries' => $max_secondary_entries, + } + + # Templates uses $ipaddresses, $server_name, $ports, $option + concat::fragment { "${instance_name}-cache-${name}": + order => "30-cache-${name}", + target => $config_file, + content => epp('haproxy/haproxy_cache.epp', $parameters), + } +} diff --git a/spec/defines/cache_spec.rb b/spec/defines/cache_spec.rb new file mode 100644 index 00000000..237a160c --- /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' => '30-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.epp b/templates/haproxy_cache.epp new file mode 100644 index 00000000..b4c9f4c3 --- /dev/null +++ b/templates/haproxy_cache.epp @@ -0,0 +1,13 @@ + +cache <%= $name %> + total-max-size <%= $total_max_size %> +<% if $max_object_size { -%> + max-object-size <%= $max_object_size %> +<% } -%> + max-age <%= $max_age %> +<% if $process_vary { -%> + process-vary <%= $process_vary %> +<% } -%> +<% if $max_secondary_entries { -%> + max-secondary-entries <%= $max_secondary_entries %> +<% } -%>