Commit 0469a938 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch '31728-nullify-cluster-namespace-if-blank' into 'master'

Nullify platform Kubernetes namespace if blank

See merge request gitlab-org/gitlab!17657
parents 87be6d9e f114bb5a
...@@ -23,11 +23,12 @@ module Clusters ...@@ -23,11 +23,12 @@ module Clusters
key: Settings.attr_encrypted_db_key_base_truncated, key: Settings.attr_encrypted_db_key_base_truncated,
algorithm: 'aes-256-cbc' algorithm: 'aes-256-cbc'
before_validation :nullify_blank_namespace
before_validation :enforce_namespace_to_lower_case before_validation :enforce_namespace_to_lower_case
before_validation :enforce_ca_whitespace_trimming before_validation :enforce_ca_whitespace_trimming
validates :namespace, validates :namespace,
allow_blank: true, allow_nil: true,
length: 1..63, length: 1..63,
format: { format: {
with: Gitlab::Regex.kubernetes_namespace_regex, with: Gitlab::Regex.kubernetes_namespace_regex,
...@@ -190,6 +191,10 @@ module Clusters ...@@ -190,6 +191,10 @@ module Clusters
true true
end end
def nullify_blank_namespace
self.namespace = nil if namespace.blank?
end
end end
end end
end end
......
---
title: Nullify platform Kubernetes namespace if blank
merge_request: 17657
author:
type: fixed
...@@ -19,14 +19,23 @@ describe Clusters::Platforms::Kubernetes do ...@@ -19,14 +19,23 @@ describe Clusters::Platforms::Kubernetes do
it_behaves_like 'having unique enum values' it_behaves_like 'having unique enum values'
describe 'before_validation' do describe 'before_validation' do
let(:kubernetes) { create(:cluster_platform_kubernetes, :configured, namespace: namespace) }
context 'when namespace includes upper case' do context 'when namespace includes upper case' do
let(:kubernetes) { create(:cluster_platform_kubernetes, :configured, namespace: namespace) }
let(:namespace) { 'ABC' } let(:namespace) { 'ABC' }
it 'converts to lower case' do it 'converts to lower case' do
expect(kubernetes.namespace).to eq('abc') expect(kubernetes.namespace).to eq('abc')
end end
end end
context 'when namespace is blank' do
let(:namespace) { '' }
it 'nullifies the namespace' do
expect(kubernetes.namespace).to be_nil
end
end
end end
describe 'validation' do describe 'validation' do
...@@ -35,8 +44,8 @@ describe Clusters::Platforms::Kubernetes do ...@@ -35,8 +44,8 @@ describe Clusters::Platforms::Kubernetes do
context 'when validates namespace' do context 'when validates namespace' do
let(:kubernetes) { build(:cluster_platform_kubernetes, :configured, namespace: namespace) } let(:kubernetes) { build(:cluster_platform_kubernetes, :configured, namespace: namespace) }
context 'when namespace is blank' do context 'when namespace is nil' do
let(:namespace) { '' } let(:namespace) { nil }
it { is_expected.to be_truthy } it { is_expected.to be_truthy }
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