2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-04 17:35:41 -05:00
|
|
|
module DeploymentPlatform
|
2018-03-21 10:59:40 -04:00
|
|
|
# EE would override this and utilize environment argument
|
2018-03-26 18:14:23 -04:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2018-03-07 05:14:25 -05:00
|
|
|
def deployment_platform(environment: nil)
|
2018-03-26 18:14:23 -04:00
|
|
|
@deployment_platform ||= {}
|
|
|
|
|
|
|
|
@deployment_platform[environment] ||= find_deployment_platform(environment)
|
2018-01-04 17:35:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-03-26 18:14:23 -04:00
|
|
|
def find_deployment_platform(environment)
|
|
|
|
find_cluster_platform_kubernetes(environment: environment) ||
|
2018-10-28 22:28:36 -04:00
|
|
|
find_group_cluster_platform_kubernetes_with_feature_guard(environment: environment) ||
|
2019-04-17 22:45:31 -04:00
|
|
|
find_instance_cluster_platform_kubernetes_with_feature_guard(environment: environment) ||
|
2018-03-26 18:14:23 -04:00
|
|
|
find_kubernetes_service_integration ||
|
|
|
|
build_cluster_and_deployment_platform
|
|
|
|
end
|
|
|
|
|
2018-03-21 10:59:40 -04:00
|
|
|
# EE would override this and utilize environment argument
|
|
|
|
def find_cluster_platform_kubernetes(environment: nil)
|
|
|
|
clusters.enabled.default_environment
|
|
|
|
.last&.platform_kubernetes
|
2018-01-04 17:35:41 -05:00
|
|
|
end
|
|
|
|
|
2018-10-28 22:28:36 -04:00
|
|
|
def find_group_cluster_platform_kubernetes_with_feature_guard(environment: nil)
|
2018-11-28 19:50:19 -05:00
|
|
|
return unless group_clusters_enabled?
|
2018-10-28 22:28:36 -04:00
|
|
|
|
|
|
|
find_group_cluster_platform_kubernetes(environment: environment)
|
|
|
|
end
|
|
|
|
|
|
|
|
# EE would override this and utilize environment argument
|
|
|
|
def find_group_cluster_platform_kubernetes(environment: nil)
|
2018-12-02 16:22:33 -05:00
|
|
|
Clusters::Cluster.enabled.default_environment.ancestor_clusters_for_clusterable(self)
|
|
|
|
.first&.platform_kubernetes
|
2018-10-28 22:28:36 -04:00
|
|
|
end
|
|
|
|
|
2019-04-17 22:45:31 -04:00
|
|
|
def find_instance_cluster_platform_kubernetes_with_feature_guard(environment: nil)
|
2019-05-02 18:15:23 -04:00
|
|
|
return unless Clusters::Instance.enabled?
|
2019-04-17 22:45:31 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-01-04 17:35:41 -05:00
|
|
|
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],
|
2018-10-14 16:42:29 -04:00
|
|
|
cluster_type: :project_type,
|
2018-01-04 17:35:41 -05:00
|
|
|
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
|