From 5f3dc6f11a42d66c13dd50b5a2115d36b1243f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20B=C3=A9rtoli?= Date: Thu, 27 May 2021 20:16:17 -0300 Subject: [PATCH] feat(config): allow to manage extra files --- pillar.example | 32 +++++++++++++++++++ prometheus/config/file.sls | 31 ++++++++++++++++++ prometheus/defaults.yaml | 1 + .../default/controls/archive_spec.rb | 10 ------ .../default/controls/config_spec.rb | 26 +++++++++++++++ test/salt/pillar/default.sls | 32 +++++++++++++++++++ 6 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 test/integration/default/controls/config_spec.rb diff --git a/pillar.example b/pillar.example index fabe703..748a151 100644 --- a/pillar.example +++ b/pillar.example @@ -127,6 +127,7 @@ prometheus: # Load rules once and periodically evaluate them according to the global # 'evaluation_interval' + # You can manage these files with the `extra_files` dict (see below) rule_files: - "first_rules.yml" # - "second_rules.yml" @@ -207,6 +208,37 @@ prometheus: # yamllint disable-line rule:braces altpriority: {{ range(1, 100000) | random }} + # This dict lets you manage other config files (like the `rule_files` for the + # alertmanager) or split the config un multiple and organize files in meaninful ways + extra_files: + first_rules: + component: alertmanager + config: + groups: + - name: example + rules: + - alert: HighRequestLatency + expr: 'job:request_latency_seconds:mean5m{job="myjob"} > 0.5' + for: 10m + labels: + severity: page + annotations: + summary: High request latency + # You can specify a `file` parameter, which will be used to create a file + # under the prometheus config dir. In this example, the file will be + # named /etc/prometheus/subdir/second.yml + second_rules: + file: subdir/second + component: alertmanager + config: + groups: + - name: example + rules: + - alert: HighRequestLatency + expr: 'job:request_latency_seconds:mean5m{job="myjob"} > 0.5' + for: 10m + labels: {} + tofs: # The files_switch key serves as a selector for alternative # directories under the formula files directory. See TOFS pattern diff --git a/prometheus/config/file.sls b/prometheus/config/file.sls index 1b487e7..ccd3abd 100644 --- a/prometheus/config/file.sls +++ b/prometheus/config/file.sls @@ -25,6 +25,7 @@ prometheus-config-file-etc-file-directory: - require: - sls: {{ sls_archive_install if p.pkg.use_upstream_archive else sls_package_install }} + # This loop manages the main config file for each component {%- for name in p.wanted.component %} {%- if 'config' in p.pkg.component[name] and p.pkg.component[name]['config'] %} @@ -53,3 +54,33 @@ prometheus-config-file-{{ name }}-file-managed: {%- endif %} {%- endfor %} + + # This loop manages other config files, like `rules_files` + {%- for ef in p.extra_files %} + {%- set name = p.extra_files[ef]['file'] | default(ef) %} + {%- set component = p.extra_files[ef]['component'] | default('prometheus') %} + +prometheus-config-file-{{ ef }}-file-managed: + file.managed: + - name: {{ p.dir.etc }}{{ p.div }}{{ name }}.yml + - source: {{ files_switch(['config.yml.jinja'], + lookup='prometheus-config-file-' ~ ef ~ '-file-managed' + ) + }} + - makedirs: True + - template: jinja + {%- if grains.os != 'Windows' %} + - mode: 644 + - user: {{ component }} + - group: {{ component }} + {%- endif %} + - context: + config: {{ p.extra_files[ef]['config'] }} + - require: + - file: prometheus-config-file-etc-file-directory + - user: prometheus-config-users-install-{{ component }}-user-present + - group: prometheus-config-users-install-{{ component }}-group-present + - watch_in: + - service: prometheus-service-running-{{ component }} + + {%- endfor %} diff --git a/prometheus/defaults.yaml b/prometheus/defaults.yaml index 2a69e9e..974fff1 100644 --- a/prometheus/defaults.yaml +++ b/prometheus/defaults.yaml @@ -5,6 +5,7 @@ prometheus: div: '/' force: false overwrite: true + extra_files: {} wanted: clientlibs: [] component: diff --git a/test/integration/default/controls/archive_spec.rb b/test/integration/default/controls/archive_spec.rb index da78961..591b9db 100644 --- a/test/integration/default/controls/archive_spec.rb +++ b/test/integration/default/controls/archive_spec.rb @@ -178,14 +178,4 @@ control 'prometheus components' do it { should exist } its('group') { should eq 'root' } end - describe file('/etc/prometheus/prometheus.yml') do - it { should exist } - its('group') { should eq 'prometheus' } - its('mode') { should cmp '0644' } - end - describe file('/etc/prometheus/alertmanager.yml') do - it { should exist } - its('group') { should eq 'alertmanager' } - its('mode') { should cmp '0644' } - end end diff --git a/test/integration/default/controls/config_spec.rb b/test/integration/default/controls/config_spec.rb new file mode 100644 index 0000000..19c78d4 --- /dev/null +++ b/test/integration/default/controls/config_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +control 'prometheus configuration files' do + title 'should exist' + + describe file('/etc/prometheus/prometheus.yml') do + it { should exist } + its('group') { should eq 'prometheus' } + its('mode') { should cmp '0644' } + end + describe file('/etc/prometheus/alertmanager.yml') do + it { should exist } + its('group') { should eq 'alertmanager' } + its('mode') { should cmp '0644' } + end + describe file('/etc/prometheus/first_rules.yml') do + it { should exist } + its('group') { should eq 'alertmanager' } + its('mode') { should cmp '0644' } + end + describe file('/etc/prometheus/subdir/second.yml') do + it { should exist } + its('group') { should eq 'alertmanager' } + its('mode') { should cmp '0644' } + end +end diff --git a/test/salt/pillar/default.sls b/test/salt/pillar/default.sls index dda9073..902dc9f 100644 --- a/test/salt/pillar/default.sls +++ b/test/salt/pillar/default.sls @@ -135,6 +135,7 @@ prometheus: # Load rules once and periodically evaluate them according to the global # 'evaluation_interval' + # You can manage these files with the `extra_files` dict (see below) rule_files: - "first_rules.yml" # - "second_rules.yml" @@ -212,6 +213,37 @@ prometheus: # yamllint disable-line rule:braces altpriority: {{ range(1, 100000) | random }} + # This dict lets you manage other config files (like the `rule_files` for the + # alertmanager) or split the config un multiple and organize files in meaninful ways + extra_files: + first_rules: + component: alertmanager + config: + groups: + - name: example + rules: + - alert: HighRequestLatency + expr: 'job:request_latency_seconds:mean5m{job="myjob"} > 0.5' + for: 10m + labels: + severity: page + annotations: + summary: High request latency + # You can specify a `file` parameter, which will be used to create a file + # under the prometheus config dir. In this example, the file will be + # named /etc/prometheus/subdir/second.yml + second_rules: + file: subdir/second + component: alertmanager + config: + groups: + - name: example + rules: + - alert: HighRequestLatency + expr: 'job:request_latency_seconds:mean5m{job="myjob"} > 0.5' + for: 10m + labels: {} + tofs: # The files_switch key serves as a selector for alternative # directories under the formula files directory. See TOFS pattern