Commit cd25eac9 authored by Robert Speicher's avatar Robert Speicher

Merge branch '37480-validate-instance-level-cluster-environment-scope' into 'master'

Validate unique environment scope for instance clusters

See merge request gitlab-org/gitlab!20886
parents 54506cc8 0c15c5b5
......@@ -271,6 +271,21 @@ module Clusters
kubernetes_namespaces.delete_all(:delete_all)
end
def clusterable
return unless cluster_type
case cluster_type
when 'project_type'
project
when 'group_type'
group
when 'instance_type'
instance
else
raise NotImplementedError
end
end
private
def unique_management_project_environment_scope
......
---
title: Validate unique environment scope for instance clusters
merge_request: 20886
author:
type: fixed
......@@ -12,22 +12,16 @@ module EE
validate :unique_environment_scope
end
def unique_environment_scope
if project && project.clusters.where(environment_scope: environment_scope).where.not(id: id).exists?
errors.add(:environment_scope, "cannot add duplicated environment scope")
return false
end
def prometheus_adapter
application_prometheus
end
private
if group && group.clusters.where(environment_scope: environment_scope).where.not(id: id).exists?
def unique_environment_scope
if clusterable.present? && clusterable.clusters.where(environment_scope: environment_scope).where.not(id: id).exists?
errors.add(:environment_scope, 'cannot add duplicated environment scope')
return false
end
true
end
def prometheus_adapter
application_prometheus
end
end
end
......
......@@ -61,6 +61,24 @@ describe Clusters::Cluster do
it { is_expected.to be_truthy }
end
end
context 'for an instance cluster' do
before do
create(:cluster, :instance, environment_scope: 'product/*')
end
context 'identical environment scope exists' do
let(:cluster) { build(:cluster, :instance, environment_scope: 'product/*') }
it { is_expected.to be_falsey }
end
context 'identical environment scope does not exist' do
let(:cluster) { build(:cluster, :instance, environment_scope: '*') }
it { is_expected.to be_truthy }
end
end
end
end
end
......@@ -976,4 +976,38 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do
expect(cluster.kubernetes_namespaces).to be_empty
end
end
describe '#clusterable' do
subject { cluster.clusterable }
context 'project type' do
let(:cluster) { create(:cluster, :project) }
it { is_expected.to eq(cluster.project) }
end
context 'group type' do
let(:cluster) { create(:cluster, :group) }
it { is_expected.to eq(cluster.group) }
end
context 'instance type' do
let(:cluster) { create(:cluster, :instance) }
it { is_expected.to be_a(Clusters::Instance) }
end
context 'unknown type' do
let(:cluster) { create(:cluster, :project) }
before do
allow(cluster).to receive(:cluster_type).and_return('unknown_type')
end
it 'raises NotImplementedError' do
expect { subject }.to raise_error(NotImplementedError)
end
end
end
end
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