Commit d2756680 authored by Stan Hu's avatar Stan Hu

Merge branch 'ensure-default-namespace-is-under-63-chars' into 'master'

Ensure default k8s namespace is <= 63 chars

See merge request gitlab-org/gitlab!30384
parents 5e7143ea cc5a9b2c
......@@ -36,14 +36,17 @@ module Gitlab
end
end
def default_project_namespace(slug)
namespace_slug = "#{project.path}-#{project.id}".downcase
if cluster.namespace_per_environment?
namespace_slug += "-#{slug}"
def default_project_namespace(environment_slug)
maybe_environment_suffix = cluster.namespace_per_environment? ? "-#{environment_slug}" : ''
suffix = "-#{project.id}#{maybe_environment_suffix}"
namespace = project_path_slug(63 - suffix.length) + suffix
Gitlab::NamespaceSanitizer.sanitize(namespace)
end
Gitlab::NamespaceSanitizer.sanitize(namespace_slug)
def project_path_slug(max_length)
Gitlab::NamespaceSanitizer
.sanitize(project.path.downcase)
.first(max_length)
end
##
......
......@@ -32,6 +32,14 @@ RSpec.describe Gitlab::Kubernetes::DefaultNamespace do
subject { generator.from_environment_slug(environment.slug) }
shared_examples_for 'handles very long project paths' do
before do
allow(project).to receive(:path).and_return 'x' * 100
end
it { is_expected.to satisfy { |s| s.length <= 63 } }
end
context 'namespace per environment is enabled' do
context 'platform namespace is specified' do
let(:platform_namespace) { 'platform-namespace' }
......@@ -47,15 +55,12 @@ RSpec.describe Gitlab::Kubernetes::DefaultNamespace do
context 'platform namespace is blank' do
let(:platform_namespace) { nil }
let(:mock_namespace) { 'mock-namespace' }
it 'constructs a namespace from the project and environment' do
expect(Gitlab::NamespaceSanitizer).to receive(:sanitize)
.with("#{project.path}-#{project.id}-#{environment.slug}".downcase)
.and_return(mock_namespace)
expect(subject).to eq mock_namespace
it 'constructs a namespace from the project and environment slug' do
expect(subject).to eq "path-with-capitals-#{project.id}-#{environment.slug}"
end
it_behaves_like 'handles very long project paths'
end
end
......@@ -70,15 +75,12 @@ RSpec.describe Gitlab::Kubernetes::DefaultNamespace do
context 'platform namespace is blank' do
let(:platform_namespace) { nil }
let(:mock_namespace) { 'mock-namespace' }
it 'constructs a namespace from the project and environment' do
expect(Gitlab::NamespaceSanitizer).to receive(:sanitize)
.with("#{project.path}-#{project.id}".downcase)
.and_return(mock_namespace)
expect(subject).to eq mock_namespace
it 'constructs a namespace from just the project' do
expect(subject).to eq "path-with-capitals-#{project.id}"
end
it_behaves_like 'handles very long project paths'
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