diff options
-rw-r--r-- | app/models/deployment.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/ci/build/prerequisite/base.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/ci/build/prerequisite/kubernetes_namespace.rb | 22 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb | 37 | ||||
-rw-r--r-- | spec/models/deployment_spec.rb | 28 |
5 files changed, 61 insertions, 32 deletions
diff --git a/app/models/deployment.rb b/app/models/deployment.rb index 811e623b7f7..428edfd88de 100644 --- a/app/models/deployment.rb +++ b/app/models/deployment.rb @@ -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 diff --git a/lib/gitlab/ci/build/prerequisite/base.rb b/lib/gitlab/ci/build/prerequisite/base.rb index 156aa22d95b..d3c37a3e02e 100644 --- a/lib/gitlab/ci/build/prerequisite/base.rb +++ b/lib/gitlab/ci/build/prerequisite/base.rb @@ -5,8 +5,6 @@ module Gitlab module Build module Prerequisite class Base - include Utils::StrongMemoize - attr_reader :build def initialize(build) diff --git a/lib/gitlab/ci/build/prerequisite/kubernetes_namespace.rb b/lib/gitlab/ci/build/prerequisite/kubernetes_namespace.rb index d1b59d0a64c..3d66b13caa6 100644 --- a/lib/gitlab/ci/build/prerequisite/kubernetes_namespace.rb +++ b/lib/gitlab/ci/build/prerequisite/kubernetes_namespace.rb @@ -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 diff --git a/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb b/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb index 6f6e4abc0c8..ba87863c978 100644 --- a/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb +++ b/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb @@ -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 diff --git a/spec/models/deployment_spec.rb b/spec/models/deployment_spec.rb index a8d53cfcd7d..5fce9504334 100644 --- a/spec/models/deployment_spec.rb +++ b/spec/models/deployment_spec.rb @@ -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 |