Commit f23030d4 authored by Michael Kozono's avatar Michael Kozono

Merge branch 'mwaw/activate_shared_services_on_project_creation' into 'master'

Activate Promethues integration for newly created projects with access to shared Promethues application

See merge request gitlab-org/gitlab!24676
parents ed3f5ff1 91eba767
......@@ -88,7 +88,9 @@ class PrometheusService < MonitoringService
return false if template?
return false unless project
project.all_clusters.enabled.any? { |cluster| cluster.application_prometheus_available? }
project.all_clusters.enabled.eager_load(:application_prometheus).any? do |cluster|
cluster.application_prometheus&.available?
end
end
def allow_local_api_url?
......
......@@ -98,6 +98,7 @@ module Projects
setup_authorizations
current_user.invalidate_personal_projects_count
create_prometheus_service
create_readme if @initialize_with_readme
end
......@@ -169,6 +170,20 @@ module Projects
end
# rubocop: enable CodeReuse/ActiveRecord
def create_prometheus_service
service = @project.find_or_initialize_service(::PrometheusService.to_param)
if service.prometheus_available?
service.save!
else
@project.prometheus_service = nil
end
rescue ActiveRecord::RecordInvalid => e
Gitlab::ErrorTracking.track_exception(e, extra: { project_id: project.id })
@project.prometheus_service = nil
end
def set_project_name_from_path
# Set project name from path
if @project.name.present? && @project.path.present?
......
---
title: Activate Prometheus integration service for newly created project if this project
has access to shared Prometheus application.
merge_request: 24676
author:
type: fixed
......@@ -177,6 +177,7 @@ module API
use :create_params
end
post do
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab/issues/21139')
attrs = declared_params(include_missing: false)
attrs = translate_params_for_compatibility(attrs)
filter_attributes_using_license!(attrs)
......@@ -209,6 +210,7 @@ module API
end
# rubocop: disable CodeReuse/ActiveRecord
post "user/:user_id" do
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab/issues/21139')
authenticated_as_admin!
user = User.find_by(id: params.delete(:user_id))
not_found!('User') unless user
......
......@@ -176,6 +176,15 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
it 'returns true' do
expect(service.prometheus_available?).to be(true)
end
it 'avoids N+1 queries' do
service
5.times do |i|
other_cluster = create(:cluster_for_group, :with_installed_helm, groups: [group], environment_scope: i)
create(:clusters_applications_prometheus, :installing, cluster: other_cluster)
end
expect { service.prometheus_available? }.not_to exceed_query_limit(1)
end
end
context 'cluster belongs to gitlab instance' do
......
......@@ -344,7 +344,7 @@ describe Projects::CreateService, '#execute' do
context 'when there is an active service template' do
before do
create(:service, project: nil, template: true, active: true)
create(:prometheus_service, project: nil, template: true, active: true)
end
it 'creates a service from this template' do
......@@ -394,6 +394,67 @@ describe Projects::CreateService, '#execute' do
expect(rugged.config['gitlab.fullpath']).to eq project.full_path
end
context 'when project has access to shared service' do
context 'Prometheus application is shared via group cluster' do
let(:cluster) { create(:cluster, :group, groups: [group]) }
let(:group) do
create(:group).tap do |group|
group.add_owner(user)
end
end
before do
create(:clusters_applications_prometheus, :installed, cluster: cluster)
end
it 'creates PrometheusService record', :aggregate_failures do
project = create_project(user, opts.merge!(namespace_id: group.id))
service = project.prometheus_service
expect(service.active).to be true
expect(service.manual_configuration?).to be false
expect(service.persisted?).to be true
end
end
context 'Prometheus application is shared via instance cluster' do
let(:cluster) { create(:cluster, :instance) }
before do
create(:clusters_applications_prometheus, :installed, cluster: cluster)
end
it 'creates PrometheusService record', :aggregate_failures do
project = create_project(user, opts)
service = project.prometheus_service
expect(service.active).to be true
expect(service.manual_configuration?).to be false
expect(service.persisted?).to be true
end
it 'cleans invalid record and logs warning', :aggregate_failures do
invalid_service_record = build(:prometheus_service, properties: { api_url: nil, manual_configuration: true }.to_json)
allow_next_instance_of(Project) do |instance|
allow(instance).to receive(:build_prometheus_service).and_return(invalid_service_record)
end
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(an_instance_of(ActiveRecord::RecordInvalid), include(extra: { project_id: a_kind_of(Integer) }))
project = create_project(user, opts)
expect(project.prometheus_service).to be_nil
end
end
context 'shared Prometheus application is not available' do
it 'does not persist PrometheusService record', :aggregate_failures do
project = create_project(user, opts)
expect(project.prometheus_service).to be_nil
end
end
end
context 'with external authorization enabled' do
before do
enable_external_authorization_service_check
......
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