Commit 325d504c authored by Tiger's avatar Tiger

Don't recreate Kubernetes namespaces if they exist

Instead of attempting to create or update a Kubernetes
namespace on every deploy, only do so when we know it
doesn't exist yet.
parent 89b0bc04
......@@ -5,6 +5,8 @@ module Gitlab
module Build
module Prerequisite
class Base
include Utils::StrongMemoize
attr_reader :build
def initialize(build)
......
......@@ -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
......
......@@ -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
......
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