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