Read about our upcoming Code of Conduct on this issue

kubernetes_namespace.rb 2.24 KB
Newer Older
1
2
3
4
5
6
7
8
# frozen_string_literal: true

module Gitlab
  module Ci
    module Build
      module Prerequisite
        class KubernetesNamespace < Base
          def unmet?
9
            deployment_cluster.present? &&
10
              deployment_cluster.managed? &&
11
              (missing_namespace? || need_knative_version_role_binding?)
12
13
14
15
16
          end

          def complete!
            return unless unmet?

17
            create_namespace
18
19
20
21
          end

          private

22
23
24
25
          def missing_namespace?
            kubernetes_namespace.nil? || kubernetes_namespace.service_account_token.blank?
          end

26
27
          def need_knative_version_role_binding?
            !knative_serving_namespace.nil? && knative_version_role_binding.nil?
28
29
          end

30
          def deployment_cluster
31
            build.deployment&.cluster
32
33
          end

34
35
36
37
          def environment
            build.deployment.environment
          end

38
39
40
41
42
43
44
45
          def knative_serving_namespace
            strong_memoize(:knative_serving_namespace) do
              Clusters::KnativeServingNamespaceFinder.new(
                deployment_cluster
              ).execute
            end
          end

46
47
48
49
50
51
52
53
          def knative_version_role_binding
            strong_memoize(:knative_version_role_binding) do
              Clusters::KnativeVersionRoleBindingFinder.new(
                deployment_cluster
              ).execute
            end
          end

54
55
          def kubernetes_namespace
            strong_memoize(:kubernetes_namespace) do
56
57
58
              Clusters::KubernetesNamespaceFinder.new(
                deployment_cluster,
                project: environment.project,
59
                environment_name: environment.name,
60
61
                allow_blank_token: true
              ).execute
62
63
            end
          end
64

65
          def create_namespace
66
            Clusters::Kubernetes::CreateOrUpdateNamespaceService.new(
67
              cluster: deployment_cluster,
68
69
70
71
72
73
74
75
              kubernetes_namespace: kubernetes_namespace || build_namespace_record
            ).execute
          end

          def build_namespace_record
            Clusters::BuildKubernetesNamespaceService.new(
              deployment_cluster,
              environment: environment
76
77
78
79
80
81
82
            ).execute
          end
        end
      end
    end
  end
end