Commit 89b0bc04 authored by Tiger's avatar Tiger

Create one Kubernetes namespace for a deployment

Instead of creating a Kubernetes namespace on every
cluster related to a project, only create one on the
cluster the project is about to be deployed to.
parent 759dab5b
......@@ -78,6 +78,10 @@ class Deployment < ActiveRecord::Base
Commit.truncate_sha(sha)
end
def cluster
project.deployment_platform(environment: environment.name)&.cluster
end
def last?
self == environment.last_deployment
end
......
......@@ -5,8 +5,6 @@ module Gitlab
module Build
module Prerequisite
class Base
include Utils::StrongMemoize
attr_reader :build
def initialize(build)
......
......@@ -10,37 +10,29 @@ module Gitlab
# so we must always ensure the namespace is up to date.
#
def unmet?
build.has_deployment? && clusters_missing_namespaces.present?
deployment_cluster.present?
end
def complete!
return unless unmet?
clusters_missing_namespaces.each do |cluster|
create_or_update_namespace(cluster)
end
create_or_update_namespace
end
private
def project
build.project
def deployment_cluster
build.deployment&.cluster
end
def create_or_update_namespace(cluster)
kubernetes_namespace = cluster.find_or_initialize_kubernetes_namespace_for_project(project)
def create_or_update_namespace
kubernetes_namespace = deployment_cluster.find_or_initialize_kubernetes_namespace_for_project(build.project)
Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService.new(
cluster: cluster,
cluster: deployment_cluster,
kubernetes_namespace: kubernetes_namespace
).execute
end
def clusters_missing_namespaces
strong_memoize(:clusters_missing_namespaces) do
project.all_clusters.missing_kubernetes_namespace(project.kubernetes_namespaces).to_a
end
end
end
end
end
......
......@@ -16,34 +16,41 @@ describe Gitlab::Ci::Build::Prerequisite::KubernetesNamespace do
it { is_expected.to be_falsey }
end
context 'build has a deployment, and no existing kubernetes namespace' do
context 'build has a deployment' do
let!(:deployment) { create(:deployment, deployable: build) }
let!(:cluster) { create(:cluster, projects: [build.project]) }
before do
expect(build.project.kubernetes_namespaces).to be_empty
end
context 'and a cluster to deploy to' do
let(:cluster) { create(:cluster, projects: [build.project]) }
it { is_expected.to be_truthy }
end
before do
allow(build.deployment).to receive(:cluster).and_return(cluster)
end
context 'build has a deployment and kubernetes namespaces' do
let!(:deployment) { create(:deployment, deployable: build) }
let!(:cluster) { create(:cluster, projects: [build.project]) }
let!(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, cluster: cluster) }
it { is_expected.to be_truthy }
end
it { is_expected.to be_falsey }
context 'and no cluster to deploy to' do
before do
expect(deployment.cluster).to be_nil
end
it { is_expected.to be_falsey }
end
end
end
describe '#complete!' do
let(:cluster) { create(:cluster, projects: [build.project]) }
let!(:deployment) { create(:deployment, deployable: build) }
let(:service) { double(execute: true) }
subject { described_class.new(build).complete! }
context 'completion is required' do
let!(:deployment) { create(:deployment, deployable: build) }
let(:cluster) { create(:cluster, projects: [build.project]) }
before do
allow(build.deployment).to receive(:cluster).and_return(cluster)
end
it 'creates a kubernetes namespace' do
expect(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService)
......@@ -59,7 +66,7 @@ describe Gitlab::Ci::Build::Prerequisite::KubernetesNamespace do
context 'completion is not required' do
before do
expect(build.deployment).to be_nil
expect(deployment.cluster).to be_nil
end
it 'does not create a namespace' do
......
......@@ -356,4 +356,32 @@ describe Deployment do
end
end
end
describe '#cluster' do
let(:deployment) { create(:deployment) }
let(:project) { deployment.project }
let(:environment) { deployment.environment }
subject { deployment.cluster }
before do
expect(project).to receive(:deployment_platform)
.with(environment: environment.name).and_call_original
end
context 'project has no deployment platform' do
before do
expect(project.clusters).to be_empty
end
it { is_expected.to be_nil }
end
context 'project has a deployment platform' do
let!(:cluster) { create(:cluster, projects: [project]) }
let!(:platform) { create(:cluster_platform_kubernetes, cluster: cluster) }
it { is_expected.to eq cluster }
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