Commit 61cd54b5 authored by Thong Kuah's avatar Thong Kuah

Save extra query

Combine management project query in cluster hierarchy so that we save a
query in DeploymentPlatform.
parent ce30ec2f
...@@ -38,12 +38,25 @@ module Clusters ...@@ -38,12 +38,25 @@ module Clusters
raise ArgumentError, "unknown type for #{clusterable}" raise ArgumentError, "unknown type for #{clusterable}"
end end
if clusterable.is_a?(::Project)
cte << management_clusters_query
end
cte << base_query cte << base_query
cte << parent_query(cte) cte << parent_query(cte)
cte cte
end end
# Management clusters should be first in the hierarchy so we use 0 for the
# depth column.
#
# group_parent_id is un-used but we still need to match the same number of
# columns as other queries in the CTE.
def management_clusters_query
clusterable.management_clusters.select([clusters_star, 'NULL AS group_parent_id', "0 AS #{DEPTH_COLUMN}"])
end
def group_clusters_base_query def group_clusters_base_query
group_parent_id_alias = alias_as_column(groups[:parent_id], 'group_parent_id') group_parent_id_alias = alias_as_column(groups[:parent_id], 'group_parent_id')
join_sources = ::Group.left_joins(:clusters).arel.join_sources join_sources = ::Group.left_joins(:clusters).arel.join_sources
......
...@@ -12,16 +12,10 @@ module DeploymentPlatform ...@@ -12,16 +12,10 @@ module DeploymentPlatform
private private
def find_deployment_platform(environment) def find_deployment_platform(environment)
find_management_cluster(environment) || find_platform_kubernetes_with_cte(environment) ||
find_platform_kubernetes_with_cte(environment) ||
find_instance_cluster_platform_kubernetes(environment: environment) find_instance_cluster_platform_kubernetes(environment: environment)
end end
# EE would override this and utilize environment argument
def find_management_cluster(_environment)
management_clusters.enabled.default_environment.first&.platform_kubernetes
end
# EE would override this and utilize environment argument # EE would override this and utilize environment argument
def find_platform_kubernetes_with_cte(_environment) def find_platform_kubernetes_with_cte(_environment)
Clusters::ClustersHierarchy.new(self).base_and_ancestors Clusters::ClustersHierarchy.new(self).base_and_ancestors
......
...@@ -4,14 +4,6 @@ module EE ...@@ -4,14 +4,6 @@ module EE
module DeploymentPlatform module DeploymentPlatform
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
override :find_management_cluster
def find_management_cluster(environment)
management_clusters
.enabled
.on_environment(environment, relevant_only: true)
.first&.platform_kubernetes
end
override :find_platform_kubernetes_with_cte override :find_platform_kubernetes_with_cte
def find_platform_kubernetes_with_cte(environment) def find_platform_kubernetes_with_cte(environment)
return super unless environment && feature_available?(:multiple_clusters) return super unless environment && feature_available?(:multiple_clusters)
......
...@@ -44,14 +44,40 @@ describe Clusters::ClustersHierarchy do ...@@ -44,14 +44,40 @@ describe Clusters::ClustersHierarchy do
end end
end end
context 'cluster has management project' do
let!(:project_cluster) { create(:cluster, :project, projects: [project]) }
let!(:group_cluster) { create(:cluster, :group, groups: [group], management_project: management_project) }
let(:group) { create(:group) }
let(:project) { create(:project, group: group) }
let(:management_project) { create(:project) }
it 'returns clusters for management_project' do
expect(base_and_ancestors(management_project)).to eq([group_cluster])
end
it 'returns clusters for project' do
expect(base_and_ancestors(project)).to eq([project_cluster, group_cluster])
end
it 'returns clusters for group' do
expect(base_and_ancestors(group)).to eq([group_cluster])
end
end
context 'project in nested group with clusters at some levels' do context 'project in nested group with clusters at some levels' do
let!(:child) { create(:cluster, :group, groups: [child_group]) } let!(:child) { create(:cluster, :group, groups: [child_group], management_project: management_project) }
let!(:ancestor) { create(:cluster, :group, groups: [ancestor_group]) } let!(:ancestor) { create(:cluster, :group, groups: [ancestor_group]) }
let(:ancestor_group) { create(:group) } let(:ancestor_group) { create(:group) }
let(:parent_group) { create(:group, parent: ancestor_group) } let(:parent_group) { create(:group, parent: ancestor_group) }
let(:child_group) { create(:group, parent: parent_group) } let(:child_group) { create(:group, parent: parent_group) }
let(:project) { create(:project, group: child_group) } let(:project) { create(:project, group: child_group) }
let(:management_project) { create(:project) }
it 'returns clusters for management_project' do
expect(base_and_ancestors(management_project)).to eq([child])
end
it 'returns clusters for project' do it 'returns clusters for project' do
expect(base_and_ancestors(project)).to eq([child, ancestor]) expect(base_and_ancestors(project)).to eq([child, ancestor])
......
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