2019-03-03 19:00:40 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Build
|
|
|
|
module Prerequisite
|
|
|
|
class KubernetesNamespace < Base
|
|
|
|
def unmet?
|
2019-04-14 22:11:50 -04:00
|
|
|
deployment_cluster.present? &&
|
2019-05-02 21:05:53 -04:00
|
|
|
deployment_cluster.managed? &&
|
2019-08-07 00:40:29 -04:00
|
|
|
missing_namespace?
|
2019-03-03 19:00:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def complete!
|
|
|
|
return unless unmet?
|
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
create_namespace
|
2019-03-03 19:00:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
def missing_namespace?
|
|
|
|
kubernetes_namespace.nil? || kubernetes_namespace.service_account_token.blank?
|
|
|
|
end
|
|
|
|
|
2019-03-12 23:06:54 -04:00
|
|
|
def deployment_cluster
|
2019-06-25 01:06:37 -04:00
|
|
|
build.deployment&.cluster
|
2019-03-03 19:00:40 -05:00
|
|
|
end
|
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
def environment
|
|
|
|
build.deployment.environment
|
|
|
|
end
|
|
|
|
|
2019-03-17 22:02:39 -04:00
|
|
|
def kubernetes_namespace
|
|
|
|
strong_memoize(:kubernetes_namespace) do
|
2019-08-07 00:40:29 -04:00
|
|
|
Clusters::KubernetesNamespaceFinder.new(
|
|
|
|
deployment_cluster,
|
|
|
|
project: environment.project,
|
|
|
|
environment_slug: environment.slug,
|
|
|
|
allow_blank_token: true
|
|
|
|
).execute
|
2019-03-17 22:02:39 -04:00
|
|
|
end
|
|
|
|
end
|
2019-03-03 19:00:40 -05:00
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
def create_namespace
|
2019-03-03 19:00:40 -05:00
|
|
|
Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService.new(
|
2019-03-12 23:06:54 -04:00
|
|
|
cluster: deployment_cluster,
|
2019-08-07 00:40:29 -04:00
|
|
|
kubernetes_namespace: kubernetes_namespace || build_namespace_record
|
|
|
|
).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_namespace_record
|
|
|
|
Clusters::BuildKubernetesNamespaceService.new(
|
|
|
|
deployment_cluster,
|
|
|
|
environment: environment
|
2019-03-03 19:00:40 -05:00
|
|
|
).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|