diff options
author | Z.J. van de Weg <git@zjvandeweg.nl> | 2017-04-04 09:40:00 +0200 |
---|---|---|
committer | Z.J. van de Weg <git@zjvandeweg.nl> | 2017-04-04 11:33:16 +0200 |
commit | cb304e76dacfcec704b4559877bd3bbbdc73ba7b (patch) | |
tree | 19c687401a3bd7bdddfe6f4734ad10c92d07103d | |
parent | fa65b65b0f5e7095e2ec7c4ca0c269a4fe4baab1 (diff) | |
download | gitlab-ce-zj-kube-service-auto-fill.tar.gz |
Don't autofill kubernetes namespacezj-kube-service-auto-fill
-rw-r--r-- | app/models/project_services/kubernetes_service.rb | 41 | ||||
-rw-r--r-- | app/views/projects/edit.html.haml | 2 | ||||
-rw-r--r-- | changelogs/unreleased/zj-kube-service-auto-fill.yml | 4 | ||||
-rw-r--r-- | spec/models/project_services/kubernetes_service_spec.rb | 17 |
4 files changed, 39 insertions, 25 deletions
diff --git a/app/models/project_services/kubernetes_service.rb b/app/models/project_services/kubernetes_service.rb index 02fbd5497fa..a4fd087aaa8 100644 --- a/app/models/project_services/kubernetes_service.rb +++ b/app/models/project_services/kubernetes_service.rb @@ -22,22 +22,21 @@ class KubernetesService < DeploymentService with_options presence: true, if: :activated? do validates :api_url, url: true validates :token - - validates :namespace, - format: { - with: Gitlab::Regex.kubernetes_namespace_regex, - message: Gitlab::Regex.kubernetes_namespace_regex_message, - }, - length: 1..63 end + validates :namespace, + allow_blank: true, + length: 1..63, + if: :activated?, + format: { + with: Gitlab::Regex.kubernetes_namespace_regex, + message: Gitlab::Regex.kubernetes_namespace_regex_message + } + after_save :clear_reactive_cache! def initialize_properties - if properties.nil? - self.properties = {} - self.namespace = "#{project.path}-#{project.id}" if project.present? - end + self.properties = {} if properties.nil? end def title @@ -62,7 +61,7 @@ class KubernetesService < DeploymentService { type: 'text', name: 'namespace', title: 'Kubernetes namespace', - placeholder: 'Kubernetes namespace' }, + placeholder: namespace_placeholder }, { type: 'text', name: 'api_url', title: 'API URL', @@ -92,7 +91,7 @@ class KubernetesService < DeploymentService variables = [ { key: 'KUBE_URL', value: api_url, public: true }, { key: 'KUBE_TOKEN', value: token, public: false }, - { key: 'KUBE_NAMESPACE', value: namespace, public: true } + { key: 'KUBE_NAMESPACE', value: fallback_namespace, public: true } ] if ca_pem.present? @@ -137,6 +136,22 @@ class KubernetesService < DeploymentService private + def namespace_placeholder + if template? + 'Kubernetes namespace' + else + fallback_namespace + end + end + + def fallback_namespace + if namespace.blank? + "#{project.path}-#{project.id}" + else + namespace + end + end + def build_kubeclient!(api_path: 'api', api_version: 'v1') raise "Incomplete settings" unless api_url && namespace && token diff --git a/app/views/projects/edit.html.haml b/app/views/projects/edit.html.haml index b78de092a60..160345cfaa5 100644 --- a/app/views/projects/edit.html.haml +++ b/app/views/projects/edit.html.haml @@ -238,6 +238,8 @@ %ul %li Be careful. Renaming a project's repository can have unintended side effects. %li You will need to update your local repositories to point to the new location. + - if @project.deployment_services.any? + %li Your deployment services will be broken, you will need to manually fix the services after renaming. = f.submit 'Rename project', class: "btn btn-warning" - if can?(current_user, :change_namespace, @project) %hr diff --git a/changelogs/unreleased/zj-kube-service-auto-fill.yml b/changelogs/unreleased/zj-kube-service-auto-fill.yml new file mode 100644 index 00000000000..7a2c7a5085b --- /dev/null +++ b/changelogs/unreleased/zj-kube-service-auto-fill.yml @@ -0,0 +1,4 @@ +--- +title: Don't fill in the default kubernetes namespace +merge_request: +author: diff --git a/spec/models/project_services/kubernetes_service_spec.rb b/spec/models/project_services/kubernetes_service_spec.rb index bf7950ef1c9..2f216041653 100644 --- a/spec/models/project_services/kubernetes_service_spec.rb +++ b/spec/models/project_services/kubernetes_service_spec.rb @@ -4,7 +4,7 @@ describe KubernetesService, models: true, caching: true do include KubernetesHelpers include ReactiveCachingHelpers - let(:project) { create(:kubernetes_project) } + let(:project) { build_stubbed(:kubernetes_project) } let(:service) { project.kubernetes_service } # We use Kubeclient to interactive with the Kubernetes API. It will @@ -32,7 +32,8 @@ describe KubernetesService, models: true, caching: true do describe 'Validations' do context 'when service is active' do before { subject.active = true } - it { is_expected.to validate_presence_of(:namespace) } + + it { is_expected.not_to validate_presence_of(:namespace) } it { is_expected.to validate_presence_of(:api_url) } it { is_expected.to validate_presence_of(:token) } @@ -55,7 +56,7 @@ describe KubernetesService, models: true, caching: true do 'a.b' => false, 'a*b' => false, }.each do |namespace, validity| - it "should validate #{namespace} as #{validity ? 'valid' : 'invalid'}" do + it "validates #{namespace} as #{validity ? 'valid' : 'invalid'}" do subject.namespace = namespace expect(subject.valid?).to eq(validity) @@ -66,21 +67,13 @@ describe KubernetesService, models: true, caching: true do context 'when service is inactive' do before { subject.active = false } - it { is_expected.not_to validate_presence_of(:namespace) } + it { is_expected.not_to validate_presence_of(:api_url) } it { is_expected.not_to validate_presence_of(:token) } end end describe '#initialize_properties' do - context 'with a project' do - let(:namespace_name) { "#{project.path}-#{project.id}" } - - it 'defaults to the project name with ID' do - expect(described_class.new(project: project).namespace).to eq(namespace_name) - end - end - context 'without a project' do it 'leaves the namespace unset' do expect(described_class.new.namespace).to be_nil |