Commit 04af6132 authored by Thong Kuah's avatar Thong Kuah

Use #cluster for prometheus_adapter

We still fallback to environment.deployment_platform until we can
backfill
parent 4615dca1
......@@ -197,7 +197,22 @@ class Deployment < ApplicationRecord
private
def prometheus_adapter
environment.prometheus_adapter
service = project.find_or_initialize_service('prometheus')
if service.can_query?
service
else
cluster_prometheus
end
end
# TODO remove fallback case to deployment_platform_cluster.
# Otherwise we will continue to pay the performance penalty described in
# https://gitlab.com/gitlab-org/gitlab-ce/issues/63475
def cluster_prometheus
cluster_with_fallback = cluster || deployment_platform_cluster
cluster_with_fallback.application_prometheus if cluster_with_fallback&.application_prometheus_available?
end
def ref_path
......
......@@ -22,6 +22,10 @@ FactoryBot.define do
ref 'pages-deploy'
end
trait :on_cluster do
cluster factory: %i(cluster provided_by_gcp)
end
trait :running do
status :running
end
......
......@@ -295,6 +295,67 @@ describe Deployment do
end
end
describe '#has_metrics?' do
subject { deployment.has_metrics? }
context 'when deployment is failed' do
let(:deployment) { create(:deployment, :failed) }
it { is_expected.to be_falsy }
end
context 'when deployment is success' do
let(:deployment) { create(:deployment, :success) }
context 'without a monitoring service' do
it { is_expected.to be_falsy }
end
context 'with a Prometheus Service' do
let(:prometheus_service) { double(:prometheus_service, can_query?: true) }
before do
allow(deployment.project).to receive(:find_or_initialize_service).with('prometheus').and_return prometheus_service
end
it { is_expected.to be_truthy }
end
context 'with a Prometheus Service that cannot query' do
let(:prometheus_service) { double(:prometheus_service, can_query?: false) }
before do
allow(deployment.project).to receive(:find_or_initialize_service).with('prometheus').and_return prometheus_service
end
it { is_expected.to be_falsy }
end
context 'with a cluster Prometheus' do
let(:deployment) { create(:deployment, :success, :on_cluster) }
let!(:prometheus) { create(:clusters_applications_prometheus, :installed, cluster: deployment.cluster) }
before do
expect(deployment.cluster.application_prometheus).to receive(:can_query?).and_return(true)
end
it { is_expected.to be_truthy }
end
context 'fallback deployment platform' do
let(:cluster) { create(:cluster, :provided_by_user, environment_scope: '*', projects: [deployment.project]) }
let!(:prometheus) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
before do
expect(deployment.project).to receive(:deployment_platform).and_return(cluster.platform)
expect(cluster.application_prometheus).to receive(:can_query?).and_return(true)
end
it { is_expected.to be_truthy }
end
end
end
describe '#metrics' do
let(:deployment) { create(:deployment, :success) }
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) }
......
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