summaryrefslogtreecommitdiff
path: root/app/models/concerns/deployment_platform.rb
blob: 9ac0d612db3a2dcef3b64ad2c6c0f30a9846d7a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true

module DeploymentPlatform
  # EE would override this and utilize environment argument
  # rubocop:disable Gitlab/ModuleWithInstanceVariables
  def deployment_platform(environment: nil)
    @deployment_platform ||= {}

    @deployment_platform[environment] ||= find_deployment_platform(environment)
  end

  private

  def find_deployment_platform(environment)
    find_cluster_platform_kubernetes(environment: environment) ||
      find_group_cluster_platform_kubernetes_with_feature_guard(environment: environment) ||
      find_instance_cluster_platform_kubernetes_with_feature_guard(environment: environment) ||
      find_kubernetes_service_integration ||
      build_cluster_and_deployment_platform
  end

  # EE would override this and utilize environment argument
  def find_cluster_platform_kubernetes(environment: nil)
    clusters.enabled.default_environment
      .last&.platform_kubernetes
  end

  def find_group_cluster_platform_kubernetes_with_feature_guard(environment: nil)
    return unless group_clusters_enabled?

    find_group_cluster_platform_kubernetes(environment: environment)
  end

  # EE would override this and utilize environment argument
  def find_group_cluster_platform_kubernetes(environment: nil)
    Clusters::Cluster.enabled.default_environment.ancestor_clusters_for_clusterable(self)
      .first&.platform_kubernetes
  end

  def find_instance_cluster_platform_kubernetes_with_feature_guard(environment: nil)
    return unless Clusters::Instance.enabled?

    find_instance_cluster_platform_kubernetes(environment: environment)
  end

  # EE would override this and utilize environment argument
  def find_instance_cluster_platform_kubernetes(environment: nil)
    Clusters::Instance.new.clusters.enabled.default_environment
      .first&.platform_kubernetes
  end

  def find_kubernetes_service_integration
    services.deployment.reorder(nil).find_by(active: true)
  end

  def build_cluster_and_deployment_platform
    return unless kubernetes_service_template

    cluster = ::Clusters::Cluster.create(cluster_attributes_from_service_template)
    cluster.platform_kubernetes if cluster.persisted?
  end

  def kubernetes_service_template
    @kubernetes_service_template ||= KubernetesService.active.find_by_template
  end

  def cluster_attributes_from_service_template
    {
      name: 'kubernetes-template',
      projects: [self],
      cluster_type: :project_type,
      provider_type: :user,
      platform_type: :kubernetes,
      platform_kubernetes_attributes: platform_kubernetes_attributes_from_service_template
    }
  end

  def platform_kubernetes_attributes_from_service_template
    {
      api_url: kubernetes_service_template.api_url,
      ca_pem: kubernetes_service_template.ca_pem,
      token: kubernetes_service_template.token,
      namespace: kubernetes_service_template.namespace
    }
  end
end