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 ...@@ -5,6 +5,8 @@ module Gitlab
module Build module Build
module Prerequisite module Prerequisite
class Base class Base
include Utils::StrongMemoize
attr_reader :build attr_reader :build
def initialize(build) def initialize(build)
......
...@@ -5,12 +5,8 @@ module Gitlab ...@@ -5,12 +5,8 @@ module Gitlab
module Build module Build
module Prerequisite module Prerequisite
class KubernetesNamespace < Base 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? def unmet?
deployment_cluster.present? deployment_cluster.present? && kubernetes_namespace.new_record?
end end
def complete! def complete!
...@@ -25,9 +21,13 @@ module Gitlab ...@@ -25,9 +21,13 @@ module Gitlab
build.deployment&.cluster build.deployment&.cluster
end end
def create_or_update_namespace def kubernetes_namespace
kubernetes_namespace = deployment_cluster.find_or_initialize_kubernetes_namespace_for_project(build.project) 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( Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService.new(
cluster: deployment_cluster, cluster: deployment_cluster,
kubernetes_namespace: kubernetes_namespace kubernetes_namespace: kubernetes_namespace
......
...@@ -27,6 +27,12 @@ describe Gitlab::Ci::Build::Prerequisite::KubernetesNamespace do ...@@ -27,6 +27,12 @@ describe Gitlab::Ci::Build::Prerequisite::KubernetesNamespace do
end end
it { is_expected.to be_truthy } 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 end
context 'and no cluster to deploy to' do 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