Commit 1a7f4bfe authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'use-environment-name-instead-of-slug-in-kubernetes-namespace-finder' into 'master'

Find namespace by env name instead of slug

See merge request gitlab-org/gitlab!17271
parents 94f3df64 15718065
......@@ -2,12 +2,12 @@
module Clusters
class KubernetesNamespaceFinder
attr_reader :cluster, :project, :environment_slug
attr_reader :cluster, :project, :environment_name
def initialize(cluster, project:, environment_slug:, allow_blank_token: false)
def initialize(cluster, project:, environment_name:, allow_blank_token: false)
@cluster = cluster
@project = project
@environment_slug = environment_slug
@environment_name = environment_name
@allow_blank_token = allow_blank_token
end
......@@ -20,7 +20,11 @@ module Clusters
attr_reader :allow_blank_token
def find_namespace(with_environment:)
relation = with_environment ? namespaces.with_environment_slug(environment_slug) : namespaces
relation = if with_environment
namespaces.with_environment_name(environment_name)
else
namespaces
end
relation.find_by_project_id(project.id)
end
......
......@@ -172,7 +172,7 @@ module Clusters
persisted_namespace = Clusters::KubernetesNamespaceFinder.new(
self,
project: project,
environment_slug: environment.slug
environment_name: environment.name
).execute
persisted_namespace&.namespace || Gitlab::Kubernetes::DefaultNamespace.new(self, project: project).from_environment_slug(environment.slug)
......
......@@ -27,7 +27,7 @@ module Clusters
algorithm: 'aes-256-cbc'
scope :has_service_account_token, -> { where.not(encrypted_service_account_token: nil) }
scope :with_environment_slug, -> (slug) { joins(:environment).where(environments: { slug: slug }) }
scope :with_environment_name, -> (name) { joins(:environment).where(environments: { name: name }) }
def token_name
"#{namespace}-token"
......
......@@ -105,19 +105,11 @@ module Clusters
private
##
# Environment slug can be predicted given an environment
# name, so even if the environment isn't persisted yet we
# still know what to look for.
def environment_slug(name)
Gitlab::Slug::Environment.new(name).generate
end
def find_persisted_namespace(project, environment_name:)
Clusters::KubernetesNamespaceFinder.new(
cluster,
project: project,
environment_slug: environment_slug(environment_name)
environment_name: environment_name
).execute
end
......
......@@ -36,7 +36,7 @@ module Gitlab
Clusters::KubernetesNamespaceFinder.new(
deployment_cluster,
project: environment.project,
environment_slug: environment.slug,
environment_name: environment.name,
allow_blank_token: true
).execute
end
......
......@@ -7,7 +7,7 @@ RSpec.describe Clusters::KubernetesNamespaceFinder do
described_class.new(
cluster,
project: project,
environment_slug: 'production',
environment_name: 'production',
allow_blank_token: allow_blank_token
)
end
......@@ -22,8 +22,8 @@ RSpec.describe Clusters::KubernetesNamespaceFinder do
end
describe '#execute' do
let(:production) { create(:environment, project: project, slug: 'production') }
let(:staging) { create(:environment, project: project, slug: 'staging') }
let(:production) { create(:environment, project: project, name: 'production') }
let(:staging) { create(:environment, project: project, name: 'staging') }
let(:cluster) { create(:cluster, :group, :provided_by_user) }
let(:project) { create(:project) }
......
......@@ -546,7 +546,7 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do
before do
expect(Clusters::KubernetesNamespaceFinder).to receive(:new)
.with(cluster, project: environment.project, environment_slug: environment.slug)
.with(cluster, project: environment.project, environment_name: environment.name)
.and_return(double(execute: persisted_namespace))
end
......
......@@ -24,13 +24,13 @@ RSpec.describe Clusters::KubernetesNamespace, type: :model do
end
end
describe '.with_environment_slug' do
describe '.with_environment_name' do
let(:cluster) { create(:cluster, :group) }
let(:environment) { create(:environment, slug: slug) }
let(:environment) { create(:environment, name: name) }
let(:slug) { 'production' }
let(:name) { 'production' }
subject { described_class.with_environment_slug(slug) }
subject { described_class.with_environment_name(name) }
context 'there is no associated environment' do
let!(:namespace) { create(:cluster_kubernetes_namespace, cluster: cluster, project: environment.project) }
......@@ -48,12 +48,12 @@ RSpec.describe Clusters::KubernetesNamespace, type: :model do
)
end
context 'with a matching slug' do
context 'with a matching name' do
it { is_expected.to eq [namespace] }
end
context 'without a matching slug' do
let(:environment) { create(:environment, slug: 'staging') }
context 'without a matching name' do
let(:environment) { create(:environment, name: 'staging') }
it { is_expected.to be_empty }
end
......
......@@ -218,7 +218,7 @@ describe Clusters::Platforms::Kubernetes do
before do
allow(Clusters::KubernetesNamespaceFinder).to receive(:new)
.with(cluster, project: project, environment_slug: environment_slug)
.with(cluster, project: project, environment_name: environment_name)
.and_return(double(execute: persisted_namespace))
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