Skip to content

Commit 0e20186

Browse files
committed
Initial yaml support draft
1 parent fc5d64c commit 0e20186

File tree

4 files changed

+121
-15
lines changed

4 files changed

+121
-15
lines changed

data/common.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
fluentbit::format: classic
23
fluentbit::manage_package_repo: true
34
fluentbit::package_ensure: present
45
fluentbit::package_name: fluent-bit

manifests/config_yaml.pp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# @summary Configures fluentbit using YAML config
2+
#
3+
# @param config_dir
4+
# Absolute path to main configuration directory
5+
# @param plugins_path
6+
# @param scripts_path
7+
#
8+
# @private
9+
class fluentbit::config_yaml (
10+
Stdlib::Absolutepath $config_dir = $fluentbit::config_dir,
11+
Stdlib::Absolutepath $plugins_path = $fluentbit::plugins_path,
12+
Stdlib::Absolutepath $scripts_path = $fluentbit::scripts_path,
13+
) {
14+
assert_private()
15+
16+
File {
17+
ensure => file,
18+
mode => $fluentbit::config_file_mode,
19+
}
20+
21+
if $fluentbit::manage_config_dir {
22+
file { $config_dir:
23+
ensure => directory,
24+
purge => true,
25+
recurse => true,
26+
mode => $fluentbit::config_folder_mode,
27+
}
28+
}
29+
30+
$config_hash = {
31+
'env' => $fluentbit::variables,
32+
'service' => {
33+
'flush' => $fluentbit::flush,
34+
'log_level' => $fluentbit::log_level,
35+
'http_server' => bool2str($fluentbit::http_server, 'on', 'off'),
36+
},
37+
'pipeline' => {
38+
'inputs' => $fluentbit::inputs,
39+
'outputs' => $fluentbit::outputs,
40+
'filters' => $fluentbit::filters,
41+
}
42+
} + $fluentbit::yaml
43+
44+
file { "$config_dir/fluent-bit.yaml":
45+
mode => $fluentbit::config_file_mode,
46+
content => stdlib::to_yaml($config_hash),
47+
}
48+
}

manifests/init.pp

+29-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# @summary Manage fluentbit installation
22
#
33
# @see https://docs.fluentbit.io/manual/
4+
# @param format Configuration files format, either `classic` or `yaml`.
45
#
56
# @param manage_package_repo Installs the package repositories
67
# @param inputs Hash of the INPUT plugins to be configured
@@ -197,9 +198,11 @@
197198
# macro definitions to use in the configuration file
198199
# the will be registered using the *@SET* command or using Env section in YAML syntax.
199200
#
201+
# @param yaml Configuration that will be converted to yaml (requires `fluentbit:format` set to `yaml`).
200202
# @example
201203
# include fluentbit
202204
class fluentbit (
205+
Enum['classic', 'yaml'] $format,
203206
Boolean $manage_package_repo,
204207
String[1] $package_ensure,
205208
String[1] $package_name,
@@ -268,31 +271,42 @@
268271
Fluentbit::Stream $streams = {},
269272
Array[Stdlib::Absolutepath] $plugins = [],
270273
Optional[String[1]] $memory_max = undef,
274+
Hash $yaml = {},
271275
) {
272276
$plugins_path = "${config_dir}/${plugins_dir}"
273277
$scripts_path = "${config_dir}/${scripts_dir}"
274278

275279
contain fluentbit::repo
276280
contain fluentbit::install
277-
contain fluentbit::config
278281
contain fluentbit::service
279282

280-
Class['fluentbit::repo']
281-
-> Class['fluentbit::install']
282-
-> Class['fluentbit::config']
283-
~> Class['fluentbit::service']
283+
if $format == 'classic' {
284+
contain fluentbit::config
284285

285-
$inputs.each |$name, $conf| {
286-
create_resources(fluentbit::pipeline, { $name => { 'pipeline' => 'input' } + $conf })
287-
}
286+
Class['fluentbit::repo']
287+
-> Class['fluentbit::install']
288+
-> Class['fluentbit::config']
289+
~> Class['fluentbit::service']
288290

289-
$outputs.each |$name, $conf| {
290-
create_resources(fluentbit::pipeline, { $name => { 'pipeline' => 'output' } + $conf })
291-
}
291+
$inputs.each |$name, $conf| {
292+
create_resources(fluentbit::pipeline, { $name => { 'pipeline' => 'input' } + $conf })
293+
}
292294

293-
$filters.each |$name, $conf| {
294-
create_resources(fluentbit::pipeline, { $name => { 'pipeline' => 'filter' } + $conf })
295-
}
295+
$outputs.each |$name, $conf| {
296+
create_resources(fluentbit::pipeline, { $name => { 'pipeline' => 'output' } + $conf })
297+
}
296298

297-
create_resources(fluentbit::upstream, $upstreams)
299+
$filters.each |$name, $conf| {
300+
create_resources(fluentbit::pipeline, { $name => { 'pipeline' => 'filter' } + $conf })
301+
}
302+
303+
create_resources(fluentbit::upstream, $upstreams)
304+
} else {
305+
contain fluentbit::config_yaml
306+
307+
Class['fluentbit::repo']
308+
-> Class['fluentbit::install']
309+
-> Class['fluentbit::config_yaml']
310+
~> Class['fluentbit::service']
311+
}
298312
}

spec/classes/fluentbit_spec.rb

+43
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,47 @@
181181
.with_content(%r{MemoryMax=2G})
182182
}
183183
end
184+
185+
describe 'with yaml config' do
186+
context 'with default parameters' do
187+
let(:params) do
188+
{
189+
format: 'yaml',
190+
}
191+
end
192+
193+
it { is_expected.to compile.with_all_deps }
194+
it { is_expected.to contain_class('fluentbit::config_yaml') }
195+
it { is_expected.not_to contain_class('fluentbit::config') }
196+
197+
it {
198+
is_expected.to contain_file('/etc/fluent-bit/fluent-bit.yaml')
199+
.with_ensure('file')
200+
.with_content(%r{^---\n})
201+
}
202+
end
203+
204+
context 'configure outputs' do
205+
let(:params) do
206+
{
207+
format: 'yaml',
208+
outputs: {
209+
'prometheus': {
210+
'plugin': 'prometheus_exporter',
211+
'properties': {
212+
'match': 'nginx.metrics.*',
213+
'host': '0.0.0.0',
214+
'port': '2021',
215+
}
216+
}
217+
},
218+
}
219+
end
220+
221+
it {
222+
is_expected.to contain_file('/etc/fluent-bit/fluent-bit.yaml')
223+
.with_content(%r{outputs:\n\s+prometheus:})
224+
}
225+
end
226+
end
184227
end

0 commit comments

Comments
 (0)