Commit fea10b45 authored by Tiger's avatar Tiger

Allow CI specified k8s namespace for deploy boards, terminals & pod logs

https://gitlab.com/gitlab-org/gitlab/merge_requests/21460
parent 3c07ce6e
...@@ -249,14 +249,9 @@ module Clusters ...@@ -249,14 +249,9 @@ module Clusters
end end
def kubernetes_namespace_for(environment) def kubernetes_namespace_for(environment)
project = environment.project managed_namespace(environment) ||
persisted_namespace = Clusters::KubernetesNamespaceFinder.new( ci_configured_namespace(environment) ||
self, default_namespace(environment)
project: project,
environment_name: environment.name
).execute
persisted_namespace&.namespace || Gitlab::Kubernetes::DefaultNamespace.new(self, project: project).from_environment_slug(environment.slug)
end end
def allow_user_defined_namespace? def allow_user_defined_namespace?
...@@ -308,6 +303,25 @@ module Clusters ...@@ -308,6 +303,25 @@ module Clusters
end end
end end
def managed_namespace(environment)
Clusters::KubernetesNamespaceFinder.new(
self,
project: environment.project,
environment_name: environment.name
).execute&.namespace
end
def ci_configured_namespace(environment)
environment.last_deployable&.expanded_kubernetes_namespace
end
def default_namespace(environment)
Gitlab::Kubernetes::DefaultNamespace.new(
self,
project: environment.project
).from_environment_slug(environment.slug)
end
def instance_domain def instance_domain
@instance_domain ||= Gitlab::CurrentSettings.auto_devops_domain @instance_domain ||= Gitlab::CurrentSettings.auto_devops_domain
end end
......
---
title: Allow Kubernetes namespaces specified via CI template to be used for terminals,
pod logs and deploy boards
merge_request: 21460
author:
type: added
...@@ -674,7 +674,8 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do ...@@ -674,7 +674,8 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do
describe '#kubernetes_namespace_for' do describe '#kubernetes_namespace_for' do
let(:cluster) { create(:cluster, :group) } let(:cluster) { create(:cluster, :group) }
let(:environment) { create(:environment) } let(:environment) { create(:environment, last_deployable: build) }
let(:build) { create(:ci_build) }
subject { cluster.kubernetes_namespace_for(environment) } subject { cluster.kubernetes_namespace_for(environment) }
...@@ -682,16 +683,15 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do ...@@ -682,16 +683,15 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do
expect(Clusters::KubernetesNamespaceFinder).to receive(:new) expect(Clusters::KubernetesNamespaceFinder).to receive(:new)
.with(cluster, project: environment.project, environment_name: environment.name) .with(cluster, project: environment.project, environment_name: environment.name)
.and_return(double(execute: persisted_namespace)) .and_return(double(execute: persisted_namespace))
end
context 'a persisted namespace exists' do
let(:persisted_namespace) { create(:cluster_kubernetes_namespace) }
it { is_expected.to eq persisted_namespace.namespace } allow(build).to receive(:expanded_kubernetes_namespace)
.and_return(ci_configured_namespace)
end end
context 'no persisted namespace exists' do context 'no persisted namespace exists and namespace is not specified in CI template' do
let(:persisted_namespace) { nil } let(:persisted_namespace) { nil }
let(:ci_configured_namespace) { nil }
let(:namespace_generator) { double } let(:namespace_generator) { double }
let(:default_namespace) { 'a-default-namespace' } let(:default_namespace) { 'a-default-namespace' }
...@@ -706,6 +706,27 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do ...@@ -706,6 +706,27 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do
it { is_expected.to eq default_namespace } it { is_expected.to eq default_namespace }
end end
context 'persisted namespace exists' do
let(:persisted_namespace) { create(:cluster_kubernetes_namespace) }
let(:ci_configured_namespace) { nil }
it { is_expected.to eq persisted_namespace.namespace }
end
context 'namespace is specified in CI template' do
let(:persisted_namespace) { nil }
let(:ci_configured_namespace) { 'ci-configured-namespace' }
it { is_expected.to eq ci_configured_namespace }
end
context 'persisted namespace exists and namespace is also specifed in CI template' do
let(:persisted_namespace) { create(:cluster_kubernetes_namespace) }
let(:ci_configured_namespace) { 'ci-configured-namespace' }
it { is_expected.to eq persisted_namespace.namespace }
end
end end
describe '#predefined_variables' do describe '#predefined_variables' do
......
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