Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
5f445d90
Commit
5f445d90
authored
Nov 16, 2018
by
Peter Leitzen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Parametrize alerting rules with variables
parent
5c741fc8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
8 deletions
+56
-8
ee/app/services/clusters/applications/prometheus_update_service.rb
...rvices/clusters/applications/prometheus_update_service.rb
+15
-1
ee/changelogs/unreleased/7527-ci-variables-are-not-parameterized-for-alerting-rules.yml
...ci-variables-are-not-parameterized-for-alerting-rules.yml
+5
-0
ee/spec/services/clusters/applications/prometheus_update_service_spec.rb
...s/clusters/applications/prometheus_update_service_spec.rb
+36
-7
No files found.
ee/app/services/clusters/applications/prometheus_update_service.rb
View file @
5f445d90
...
...
@@ -132,7 +132,7 @@ module Clusters
@environments_with_alerts
||=
environments
.
each_with_object
({})
do
|
environment
,
hsh
|
name
=
rule_name
(
environment
)
hsh
[
name
]
=
environment
.
prometheus_alerts
.
map
(
&
:to_param
)
hsh
[
name
]
=
alerts
(
environment
)
end
end
...
...
@@ -140,6 +140,20 @@ module Clusters
"
#{
environment
.
name
}
.rules"
end
def
alerts
(
environment
)
ci_environment_slug
=
environment
.
slug
kube_namespace
=
environment
.
deployment_platform
&
.
actual_namespace
||
''
environment
.
prometheus_alerts
.
map
do
|
alert
|
alert
.
to_param
.
tap
do
|
hash
|
hash
[
'expr'
]
%=
{
ci_environment_slug:
ci_environment_slug
,
kube_namespace:
kube_namespace
}
end
end
end
def
environments
project
.
environments_for_scope
(
cluster
.
environment_scope
)
end
...
...
ee/changelogs/unreleased/7527-ci-variables-are-not-parameterized-for-alerting-rules.yml
0 → 100644
View file @
5f445d90
---
title
:
Parameterize alerting rules with variables
merge_request
:
8481
author
:
type
:
fixed
ee/spec/services/clusters/applications/prometheus_update_service_spec.rb
View file @
5f445d90
...
...
@@ -4,16 +4,20 @@ describe Clusters::Applications::PrometheusUpdateService do
describe
'#execute'
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:environment
)
{
create
(
:environment
,
project:
project
)
}
let
(
:cluster
)
{
create
(
:cluster
,
:with_installed_helm
,
projects:
[
project
])
}
let
(
:cluster
)
{
create
(
:cluster
,
:
provided_by_user
,
:
with_installed_helm
,
projects:
[
project
])
}
let
(
:application
)
{
create
(
:clusters_applications_prometheus
,
:installed
,
cluster:
cluster
)
}
let!
(
:get_command_values
)
{
OpenStruct
.
new
(
data:
OpenStruct
.
new
(
'values.yaml'
:
application
.
values
))
}
let!
(
:upgrade_command
)
{
application
.
upgrade_command
(
""
)
}
let
(
:upgrade_values_yaml
)
{
StringIO
.
new
}
let
(
:upgrade_values
)
{
YAML
.
safe_load
(
upgrade_values_yaml
.
string
)
}
let
(
:helm_client
)
{
instance_double
(
::
Gitlab
::
Kubernetes
::
Helm
::
Api
)
}
subject
(
:service
)
{
described_class
.
new
(
application
,
project
)
}
before
do
allow
(
service
).
to
receive
(
:upgrade_command
).
and_return
(
upgrade_command
)
allow
(
service
)
.
to
receive
(
:upgrade_command
)
{
|
values
|
upgrade_values_yaml
.
write
(
values
)
}
.
and_return
(
upgrade_command
)
allow
(
service
).
to
receive
(
:helm_api
).
and_return
(
helm_client
)
end
...
...
@@ -25,20 +29,45 @@ describe Clusters::Applications::PrometheusUpdateService do
end
context
'when prometheus alerts exist'
do
it
'generates the alert manager values'
do
create
(
:prometheus_alert
,
project:
project
,
environment:
environment
)
let
(
:metric
)
do
create
(
:prometheus_metric
,
project:
project
,
query:
'{pod_name=~"^%{ci_environment_slug}",namespace="%{kube_namespace}"}'
)
end
expect
(
service
).
to
receive
(
:generate_alert_manager
).
once
let!
(
:alert
)
do
create
(
:prometheus_alert
,
project:
project
,
environment:
environment
,
prometheus_metric:
metric
)
end
it
'generates the alert manager values'
do
service
.
execute
expect
(
upgrade_values
.
dig
(
'alertmanager'
,
'enabled'
)).
to
eq
(
true
)
alertmanager
=
upgrade_values
.
dig
(
'alertmanagerFiles'
,
'alertmanager.yml'
)
expect
(
alertmanager
).
not_to
be_nil
expect
(
alertmanager
.
dig
(
'receivers'
,
0
,
'name'
)).
to
eq
(
'gitlab'
)
expect
(
alertmanager
.
dig
(
'route'
,
'receiver'
)).
to
eq
(
'gitlab'
)
alerts
=
upgrade_values
.
dig
(
'serverFiles'
,
'alerts'
,
'groups'
)
expect
(
alerts
).
not_to
be_nil
expect
(
alerts
.
size
).
to
eq
(
1
)
expect
(
alerts
.
dig
(
0
,
'name'
)).
to
eq
(
"
#{
environment
.
name
}
.rules"
)
expect
(
alerts
.
dig
(
0
,
'rules'
,
0
,
'expr'
)).
to
include
(
environment
.
slug
,
environment
.
deployment_platform
.
actual_namespace
)
end
end
context
'when prometheus alerts do not exist'
do
it
'resets the alert manager values'
do
expect
(
service
).
to
receive
(
:reset_alert_manager
).
once
service
.
execute
expect
(
upgrade_values
.
dig
(
'alertmanager'
,
'enabled'
)).
to
eq
(
false
)
expect
(
upgrade_values
).
not_to
include
(
'alertmanagerFiles'
)
expect
(
upgrade_values
.
dig
(
'serverFiles'
,
'alerts'
)).
to
eq
({})
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment