From 0b1b4281cc9b8b84211607e1ef15c739aefaf236 Mon Sep 17 00:00:00 2001 From: Tomas Barton Date: Thu, 27 Jul 2023 14:30:13 +0200 Subject: [PATCH] Avoid unnecessary code repetition --- README.md | 13 +++++++++- manifests/init.pp | 15 ++++++++--- spec/classes/fluentbit_spec.rb | 47 +++++++++++++++++++++++++++++++--- types/pipelineplugin.pp | 2 +- 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 746d286..b860c80 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,22 @@ include fluentbit ![fluentbit pipeline](img/pipeline.png) +Define some inputs: ```yaml fluentbit::inputs: 'tail-syslog': - pipeline: input plugin: tail properties: Path: /var/syslog ``` + +[outputs](https://docs.fluentbit.io/manual/pipeline/outputs): +```yaml +fluentbit::outputs: + 'prometheus': + plugin: prometheus_exporter + properties: + match: nginx.metrics.* + host: 0.0.0.0 + port: 2021 +``` diff --git a/manifests/init.pp b/manifests/init.pp index c52cf68..bddf627 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -259,8 +259,17 @@ -> Class['fluentbit::config'] ~> Class['fluentbit::service'] - create_resources(fluentbit::pipeline, $inputs, { pipeline => 'input' }) - create_resources(fluentbit::pipeline, $outputs, { pipeline => 'output' }) - create_resources(fluentbit::pipeline, $filters, { pipeline => 'filter' }) + $inputs.each |$name, $conf| { + create_resources(fluentbit::pipeline, { $name => merge({ 'pipeline' => 'input' }, $conf) }) + } + + $outputs.each |$name, $conf| { + create_resources(fluentbit::pipeline, { $name => merge({ 'pipeline' => 'output' }, $conf) }) + } + + $filters.each |$name, $conf| { + create_resources(fluentbit::pipeline, { $name => merge({ 'pipeline' => 'filter' }, $conf) }) + } + create_resources(fluentbit::upstream, $upstreams) } diff --git a/spec/classes/fluentbit_spec.rb b/spec/classes/fluentbit_spec.rb index b51cd0b..04ae0f6 100644 --- a/spec/classes/fluentbit_spec.rb +++ b/spec/classes/fluentbit_spec.rb @@ -106,8 +106,7 @@ let(:params) do { inputs: { - 'tail-syslog': { - 'pipeline': 'input', + 'syslog': { 'plugin': 'tail', 'properties': { 'Path': '/var/log/syslog', @@ -118,8 +117,48 @@ end it { - is_expected.to contain_file('/etc/fluent-bit/parsers.conf') - .with_content(%r{Name\s+json\n\s+Format\s+json\n\s+Time_key\s+time}) + is_expected.to contain_concat__fragment('pipeline-syslog') + .with_content(%r{Name\s+tail\n}) + .with_content(%r{Path\s+/var/log/syslog\n}) + } + + it { + is_expected.to contain_fluentbit__pipeline('syslog') + .with({ + 'plugin': 'tail', + 'pipeline': 'input', + }) + } + end + + context 'configure outputs' do + let(:params) do + { + outputs: { + 'prometheus': { + 'plugin': 'prometheus_exporter', + 'properties': { + 'match': 'nginx.metrics.*', + 'host': '0.0.0.0', + 'port': '2021', + } + } + }, + } + end + + it { + is_expected.to contain_concat__fragment('pipeline-prometheus') + .with_content(%r{Match\s+nginx.metrics.*\n}) + .with_content(%r{Host\s+0.0.0.0\n}) + } + + it { + is_expected.to contain_fluentbit__pipeline('prometheus') + .with({ + 'plugin': 'prometheus_exporter', + 'pipeline': 'output', + }) } end end diff --git a/types/pipelineplugin.pp b/types/pipelineplugin.pp index 87ae234..ee5a613 100644 --- a/types/pipelineplugin.pp +++ b/types/pipelineplugin.pp @@ -1,6 +1,6 @@ type Fluentbit::PipelinePlugin = Hash[String, Struct[{ - pipeline => Fluentbit::PipelineType, plugin => String[1], + Optional[pipeline] => Fluentbit::PipelineType, Optional[order] => Integer, Optional[properties] => Hash, }]]