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.
This commit is contained in:
Tiger 2019-03-18 13:02:39 +11:00
parent 89b0bc04b9
commit 325d504c3c
3 changed files with 15 additions and 7 deletions

View file

@ -5,6 +5,8 @@ module Gitlab
module Build
module Prerequisite
class Base
include Utils::StrongMemoize
attr_reader :build
def initialize(build)

View file

@ -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

View file

@ -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