feat(config): allow to manage extra files

This commit is contained in:
Javier Bértoli 2021-05-27 20:16:17 -03:00
parent babe311306
commit 5f3dc6f11a
6 changed files with 122 additions and 10 deletions

View File

@ -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

View File

@ -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 %}

View File

@ -5,6 +5,7 @@ prometheus:
div: '/'
force: false
overwrite: true
extra_files: {}
wanted:
clientlibs: []
component:

View File

@ -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

View File

@ -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

View File

@ -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