Commit 41469e6c authored by Sean McGivern's avatar Sean McGivern

Merge branch...

Merge branch '9558-alert-notification-not-working-for-managed-prometheus-installations' into 'master'

Resolve "Alert notification not working for managed Prometheus installations"

Closes #9558

See merge request gitlab-org/gitlab-ee!9402
parents 072e9f54 6c98301e
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
module Clusters module Clusters
module Applications module Applications
class PrometheusConfigService class PrometheusConfigService
def initialize(project, cluster) def initialize(project, cluster, app)
@project = project @project = project
@cluster = cluster @cluster = cluster
@app = app
end end
def execute(config) def execute(config)
...@@ -18,7 +19,7 @@ module Clusters ...@@ -18,7 +19,7 @@ module Clusters
private private
attr_reader :project, :cluster attr_reader :project, :cluster, :app
def reset_alert_manager(config) def reset_alert_manager(config)
config = set_alert_manager_enabled(config, false) config = set_alert_manager_enabled(config, false)
...@@ -80,13 +81,22 @@ module Clusters ...@@ -80,13 +81,22 @@ module Clusters
'webhook_configs' => [ 'webhook_configs' => [
{ {
'url' => notify_url, 'url' => notify_url,
'send_resolved' => true 'send_resolved' => true,
'http_config' => {
'bearer_token' => alert_manager_token
}
} }
] ]
} }
] ]
end end
def alert_manager_token
app.generate_alert_manager_token!
app.alert_manager_token
end
def alert_manager_route_params def alert_manager_route_params
{ {
'receiver' => 'gitlab', 'receiver' => 'gitlab',
...@@ -97,11 +107,8 @@ module Clusters ...@@ -97,11 +107,8 @@ module Clusters
end end
def notify_url def notify_url
::Gitlab::Routing.url_helpers.notify_namespace_project_prometheus_alerts_url( ::Gitlab::Routing.url_helpers
namespace_id: project.namespace.path, .notify_project_prometheus_alerts_url(project, format: :json)
project_id: project.path,
format: :json
)
end end
def has_alerts? def has_alerts?
......
...@@ -34,7 +34,7 @@ module Clusters ...@@ -34,7 +34,7 @@ module Clusters
def update_config(config) def update_config(config)
PrometheusConfigService PrometheusConfigService
.new(project, cluster) .new(project, cluster, app)
.execute(config) .execute(config)
end end
end end
......
---
title: Fix alert notifications for managed Prometheus
merge_request: 9402
author:
type: fixed
...@@ -3,11 +3,17 @@ ...@@ -3,11 +3,17 @@
require 'spec_helper' require 'spec_helper'
describe Clusters::Applications::PrometheusConfigService do describe Clusters::Applications::PrometheusConfigService do
include Gitlab::Routing.url_helpers
set(:project) { create(:project) } set(:project) { create(:project) }
set(:production) { create(:environment, project: project) } set(:production) { create(:environment, project: project) }
set(:cluster) { create(:cluster, :provided_by_user, projects: [project]) } set(:cluster) { create(:cluster, :provided_by_user, projects: [project]) }
subject { described_class.new(project, cluster).execute(input) } let(:application) do
create(:clusters_applications_prometheus, :installed, cluster: cluster)
end
subject { described_class.new(project, cluster, application).execute(input) }
describe '#execute' do describe '#execute' do
let(:input) do let(:input) do
...@@ -37,7 +43,7 @@ describe Clusters::Applications::PrometheusConfigService do ...@@ -37,7 +43,7 @@ describe Clusters::Applications::PrometheusConfigService do
let(:webhook_config) { receiver.dig('webhook_configs', 0) } let(:webhook_config) { receiver.dig('webhook_configs', 0) }
let(:notify_url) do let(:notify_url) do
"http://localhost/#{project.namespace.name}/#{project.name}/prometheus/alerts/notify.json" notify_project_prometheus_alerts_url(project, format: :json)
end end
it 'sets receiver' do it 'sets receiver' do
...@@ -47,7 +53,10 @@ describe Clusters::Applications::PrometheusConfigService do ...@@ -47,7 +53,10 @@ describe Clusters::Applications::PrometheusConfigService do
it 'sets webhook_config' do it 'sets webhook_config' do
expect(webhook_config).to eq( expect(webhook_config).to eq(
'url' => notify_url, 'url' => notify_url,
'send_resolved' => true 'send_resolved' => true,
'http_config' => {
'bearer_token' => application.alert_manager_token
}
) )
end end
end end
......
...@@ -42,7 +42,7 @@ describe Clusters::Applications::PrometheusUpdateService do ...@@ -42,7 +42,7 @@ describe Clusters::Applications::PrometheusUpdateService do
expect(Clusters::Applications::PrometheusConfigService) expect(Clusters::Applications::PrometheusConfigService)
.to receive(:new) .to receive(:new)
.with(project, cluster) .with(project, cluster, application)
.and_return(prometheus_config_service) .and_return(prometheus_config_service)
expect(prometheus_config_service) expect(prometheus_config_service)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment