2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Build
|
|
|
|
module Prerequisite
|
|
|
|
class KubernetesNamespace < Base
|
|
|
|
def unmet?
|
|
|
|
deployment_cluster.present? &&
|
2019-07-31 22:56:46 +05:30
|
|
|
deployment_cluster.managed? &&
|
2019-10-12 21:52:04 +05:30
|
|
|
missing_namespace?
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def complete!
|
|
|
|
return unless unmet?
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
create_namespace
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def missing_namespace?
|
|
|
|
kubernetes_namespace.nil? || kubernetes_namespace.service_account_token.blank?
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def deployment_cluster
|
|
|
|
build.deployment&.cluster
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def environment
|
|
|
|
build.deployment.environment
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def kubernetes_namespace
|
|
|
|
strong_memoize(:kubernetes_namespace) do
|
2019-10-12 21:52:04 +05:30
|
|
|
Clusters::KubernetesNamespaceFinder.new(
|
|
|
|
deployment_cluster,
|
|
|
|
project: environment.project,
|
|
|
|
environment_slug: environment.slug,
|
|
|
|
allow_blank_token: true
|
|
|
|
).execute
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def create_namespace
|
2019-07-07 11:18:12 +05:30
|
|
|
Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService.new(
|
|
|
|
cluster: deployment_cluster,
|
2019-10-12 21:52:04 +05:30
|
|
|
kubernetes_namespace: kubernetes_namespace || build_namespace_record
|
|
|
|
).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_namespace_record
|
|
|
|
Clusters::BuildKubernetesNamespaceService.new(
|
|
|
|
deployment_cluster,
|
|
|
|
environment: environment
|
2019-07-07 11:18:12 +05:30
|
|
|
).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|