Skip to content

Add support for cache #626

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
75 changes: 75 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -708,6 +710,79 @@ Optional. Defaults to 'server'

Default value: `'server'`

### <a name="haproxy--cache"></a>`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)

##### <a name="-haproxy--cache--name"></a>`name`

Name of the cache.

##### <a name="-haproxy--cache--total_max_size"></a>`total_max_size`

Data type: `Integer[1,4095]`

Size of cache in megabytes. Maximum value is 4095.

##### <a name="-haproxy--cache--max_object_size"></a>`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`

##### <a name="-haproxy--cache--max_age"></a>`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`

##### <a name="-haproxy--cache--process_vary"></a>`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`

##### <a name="-haproxy--cache--max_secondary_entries"></a>`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`

##### <a name="-haproxy--cache--instance"></a>`instance`

Data type: `String`

Optional. Defaults to 'haproxy'.

Default value: `'haproxy'`

### <a name="haproxy--defaults"></a>`haproxy::defaults`

This type will setup a additional defaults configuration block inside the
Expand Down
64 changes: 64 additions & 0 deletions manifests/cache.pp
Original file line number Diff line number Diff line change
@@ -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),
}
}
41 changes: 41 additions & 0 deletions spec/defines/cache_spec.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions templates/haproxy_cache.epp
Original file line number Diff line number Diff line change
@@ -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 %>
<% } -%>