Commit 172ebcb8 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'ac-k8s-namespace-validator' into 'master'

Sanitize k8s default_namespace

Closes #38692

See merge request gitlab-org/gitlab-ce!15053
parents fa3b4736 3aafcc16
...@@ -153,7 +153,10 @@ class KubernetesService < DeploymentService ...@@ -153,7 +153,10 @@ class KubernetesService < DeploymentService
end end
def default_namespace def default_namespace
"#{project.path}-#{project.id}" if project.present? return unless project
slug = "#{project.path}-#{project.id}".downcase
slug.gsub(/[^-a-z0-9]/, '-').gsub(/^-+/, '')
end end
def build_kubeclient!(api_path: 'api', api_version: 'v1') def build_kubeclient!(api_path: 'api', api_version: 'v1')
......
---
title: Auto Devops kubernetes default namespace is now correctly built out of gitlab
project group-name
merge_request: 14642
author: Mircea Danila Dumitrescu
type: fixed
...@@ -99,45 +99,34 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do ...@@ -99,45 +99,34 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do
describe '#actual_namespace' do describe '#actual_namespace' do
subject { service.actual_namespace } subject { service.actual_namespace }
it "returns the default namespace" do shared_examples 'a correctly formatted namespace' do
is_expected.to eq(service.send(:default_namespace)) it 'returns a valid Kubernetes namespace name' do
expect(subject).to match(Gitlab::Regex.kubernetes_namespace_regex)
expect(subject).to eq(expected_namespace)
end end
context 'when namespace is specified' do
before do
service.namespace = 'my-namespace'
end end
it "returns the user-namespace" do it_behaves_like 'a correctly formatted namespace' do
is_expected.to eq('my-namespace') let(:expected_namespace) { service.send(:default_namespace) }
end
end end
context 'when service is not assigned to project' do context 'when the project path contains forbidden characters' do
before do before do
service.project = nil project.path = '-a_Strange.Path--forSure'
end end
it "does not return namespace" do it_behaves_like 'a correctly formatted namespace' do
is_expected.to be_nil let(:expected_namespace) { "a-strange-path--forsure-#{project.id}" }
end
end end
end end
describe '#actual_namespace' do
subject { service.actual_namespace }
it "returns the default namespace" do
is_expected.to eq(service.send(:default_namespace))
end
context 'when namespace is specified' do context 'when namespace is specified' do
before do before do
service.namespace = 'my-namespace' service.namespace = 'my-namespace'
end end
it "returns the user-namespace" do it_behaves_like 'a correctly formatted namespace' do
is_expected.to eq('my-namespace') let(:expected_namespace) { 'my-namespace' }
end end
end end
...@@ -146,7 +135,7 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do ...@@ -146,7 +135,7 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do
service.project = nil service.project = nil
end end
it "does not return namespace" do it 'does not return namespace' do
is_expected.to be_nil is_expected.to be_nil
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