diff options
-rw-r--r-- | lib/gitlab/ci/build/prerequisite/base.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/ci/build/prerequisite/kubernetes_namespace.rb | 14 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb | 6 |
3 files changed, 15 insertions, 7 deletions
diff --git a/lib/gitlab/ci/build/prerequisite/base.rb b/lib/gitlab/ci/build/prerequisite/base.rb index d3c37a3e02e..156aa22d95b 100644 --- a/lib/gitlab/ci/build/prerequisite/base.rb +++ b/lib/gitlab/ci/build/prerequisite/base.rb @@ -5,6 +5,8 @@ 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 3d66b13caa6..41135ae62bb 100644 --- a/lib/gitlab/ci/build/prerequisite/kubernetes_namespace.rb +++ b/lib/gitlab/ci/build/prerequisite/kubernetes_namespace.rb @@ -5,12 +5,8 @@ module Gitlab module Build module Prerequisite class KubernetesNamespace < Base - ## - # Cluster settings may have changed since the last deploy, - # so we must always ensure the namespace is up to date. - # def unmet? - deployment_cluster.present? + deployment_cluster.present? && kubernetes_namespace.new_record? end def complete! @@ -25,9 +21,13 @@ module Gitlab build.deployment&.cluster end - def create_or_update_namespace - kubernetes_namespace = deployment_cluster.find_or_initialize_kubernetes_namespace_for_project(build.project) + def kubernetes_namespace + strong_memoize(:kubernetes_namespace) do + deployment_cluster.find_or_initialize_kubernetes_namespace_for_project(build.project) + end + end + def create_or_update_namespace Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService.new( cluster: deployment_cluster, kubernetes_namespace: kubernetes_namespace 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 ba87863c978..62dcd80fad7 100644 --- a/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb +++ b/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb @@ -27,6 +27,12 @@ describe Gitlab::Ci::Build::Prerequisite::KubernetesNamespace do end it { is_expected.to be_truthy } + + context 'and a namespace is already created for this project' do + let!(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, cluster: cluster, project: build.project) } + + it { is_expected.to be_falsey } + end end context 'and no cluster to deploy to' do |