2018-08-03 03:15:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
module Clusters
|
2019-03-28 09:17:42 -04:00
|
|
|
class Cluster < ApplicationRecord
|
2017-10-13 13:21:23 -04:00
|
|
|
include Presentable
|
2018-11-03 06:13:05 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2018-11-25 03:26:28 -05:00
|
|
|
include FromUnion
|
2019-05-27 04:23:17 -04:00
|
|
|
include ReactiveCaching
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
self.table_name = 'clusters'
|
|
|
|
|
2019-04-01 09:32:20 -04:00
|
|
|
PROJECT_ONLY_APPLICATIONS = {
|
|
|
|
}.freeze
|
2017-11-02 10:10:46 -04:00
|
|
|
APPLICATIONS = {
|
2017-11-06 04:41:27 -05:00
|
|
|
Applications::Helm.application_name => Applications::Helm,
|
2017-12-22 12:23:43 -05:00
|
|
|
Applications::Ingress.application_name => Applications::Ingress,
|
2018-11-02 23:38:21 -04:00
|
|
|
Applications::CertManager.application_name => Applications::CertManager,
|
2019-09-01 21:42:36 -04:00
|
|
|
Applications::Prometheus.application_name => Applications::Prometheus,
|
2019-05-06 12:24:14 -04:00
|
|
|
Applications::Runner.application_name => Applications::Runner,
|
2019-08-22 17:40:59 -04:00
|
|
|
Applications::Jupyter.application_name => Applications::Jupyter,
|
2019-10-22 11:06:06 -04:00
|
|
|
Applications::Knative.application_name => Applications::Knative,
|
|
|
|
Applications::ElasticStack.application_name => Applications::ElasticStack
|
2019-04-01 09:32:20 -04:00
|
|
|
}.merge(PROJECT_ONLY_APPLICATIONS).freeze
|
2019-08-31 15:57:00 -04:00
|
|
|
DEFAULT_ENVIRONMENT = '*'
|
|
|
|
KUBE_INGRESS_BASE_DOMAIN = 'KUBE_INGRESS_BASE_DOMAIN'
|
2017-11-02 10:10:46 -04:00
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
belongs_to :user
|
2019-10-14 08:06:14 -04:00
|
|
|
belongs_to :management_project, class_name: '::Project', optional: true
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
has_many :cluster_projects, class_name: 'Clusters::Project'
|
2018-04-18 09:41:42 -04:00
|
|
|
has_many :projects, through: :cluster_projects, class_name: '::Project'
|
2018-11-02 11:46:15 -04:00
|
|
|
has_one :cluster_project, -> { order(id: :desc) }, class_name: 'Clusters::Project'
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2018-10-14 16:42:29 -04:00
|
|
|
has_many :cluster_groups, class_name: 'Clusters::Group'
|
|
|
|
has_many :groups, through: :cluster_groups, class_name: '::Group'
|
|
|
|
|
2017-11-01 07:57:05 -04:00
|
|
|
# we force autosave to happen when we save `Cluster` model
|
|
|
|
has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp', autosave: true
|
2019-10-16 17:07:22 -04:00
|
|
|
has_one :provider_aws, class_name: 'Clusters::Providers::Aws', autosave: true
|
2017-11-01 07:57:05 -04:00
|
|
|
|
2018-11-06 03:55:10 -05:00
|
|
|
has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes', inverse_of: :cluster, autosave: true
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2019-09-17 08:06:48 -04:00
|
|
|
def self.has_one_cluster_application(name) # rubocop:disable Naming/PredicateName
|
|
|
|
application = APPLICATIONS[name.to_s]
|
2019-09-20 08:05:52 -04:00
|
|
|
has_one application.association_name, class_name: application.to_s, inverse_of: :cluster # rubocop:disable Rails/ReflectionClassName
|
2019-09-17 08:06:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
has_one_cluster_application :helm
|
|
|
|
has_one_cluster_application :ingress
|
|
|
|
has_one_cluster_application :cert_manager
|
|
|
|
has_one_cluster_application :prometheus
|
|
|
|
has_one_cluster_application :runner
|
|
|
|
has_one_cluster_application :jupyter
|
|
|
|
has_one_cluster_application :knative
|
2019-10-22 11:06:06 -04:00
|
|
|
has_one_cluster_application :elastic_stack
|
2017-11-02 10:10:46 -04:00
|
|
|
|
2018-10-16 16:03:59 -04:00
|
|
|
has_many :kubernetes_namespaces
|
|
|
|
|
2017-10-31 04:47:48 -04:00
|
|
|
accepts_nested_attributes_for :provider_gcp, update_only: true
|
2017-10-30 08:55:18 -04:00
|
|
|
accepts_nested_attributes_for :platform_kubernetes, update_only: true
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
validates :name, cluster_name: true
|
2018-10-14 16:42:29 -04:00
|
|
|
validates :cluster_type, presence: true
|
2019-02-14 16:10:35 -05:00
|
|
|
validates :domain, allow_blank: true, hostname: { allow_numeric_hostname: true }
|
2019-08-07 00:40:29 -04:00
|
|
|
validates :namespace_per_environment, inclusion: { in: [true, false] }
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2019-01-14 12:59:35 -05:00
|
|
|
validate :restrict_modification, on: :update
|
2018-10-14 16:42:29 -04:00
|
|
|
validate :no_groups, unless: :group_type?
|
|
|
|
validate :no_projects, unless: :project_type?
|
2019-10-14 08:06:14 -04:00
|
|
|
validate :unique_management_project_environment_scope
|
2018-10-14 16:42:29 -04:00
|
|
|
|
2019-05-27 04:23:17 -04:00
|
|
|
after_save :clear_reactive_cache!
|
|
|
|
|
2017-11-03 04:22:11 -04:00
|
|
|
delegate :status, to: :provider, allow_nil: true
|
2017-10-13 13:21:23 -04:00
|
|
|
delegate :status_reason, to: :provider, allow_nil: true
|
2017-10-23 04:36:35 -04:00
|
|
|
delegate :on_creation?, to: :provider, allow_nil: true
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-11-06 09:48:44 -05:00
|
|
|
delegate :active?, to: :platform_kubernetes, prefix: true, allow_nil: true
|
2018-09-06 06:03:38 -04:00
|
|
|
delegate :rbac?, to: :platform_kubernetes, prefix: true, allow_nil: true
|
2018-10-15 05:03:15 -04:00
|
|
|
delegate :available?, to: :application_helm, prefix: true, allow_nil: true
|
|
|
|
delegate :available?, to: :application_ingress, prefix: true, allow_nil: true
|
|
|
|
delegate :available?, to: :application_prometheus, prefix: true, allow_nil: true
|
2018-12-20 22:38:50 -05:00
|
|
|
delegate :available?, to: :application_knative, prefix: true, allow_nil: true
|
2019-01-23 11:28:19 -05:00
|
|
|
delegate :external_ip, to: :application_ingress, prefix: true, allow_nil: true
|
2019-03-07 16:51:43 -05:00
|
|
|
delegate :external_hostname, to: :application_ingress, prefix: true, allow_nil: true
|
2017-11-06 09:48:44 -05:00
|
|
|
|
2019-01-31 09:58:58 -05:00
|
|
|
alias_attribute :base_domain, :domain
|
2019-03-29 14:23:18 -04:00
|
|
|
alias_attribute :provided_by_user?, :user?
|
2019-01-31 09:58:58 -05:00
|
|
|
|
2018-10-14 16:42:29 -04:00
|
|
|
enum cluster_type: {
|
|
|
|
instance_type: 1,
|
|
|
|
group_type: 2,
|
|
|
|
project_type: 3
|
|
|
|
}
|
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
enum platform_type: {
|
|
|
|
kubernetes: 1
|
|
|
|
}
|
|
|
|
|
|
|
|
enum provider_type: {
|
|
|
|
user: 0,
|
2019-10-16 17:07:22 -04:00
|
|
|
gcp: 1,
|
|
|
|
aws: 2
|
2017-10-29 14:48:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
scope :enabled, -> { where(enabled: true) }
|
|
|
|
scope :disabled, -> { where(enabled: false) }
|
2019-10-16 17:07:22 -04:00
|
|
|
|
|
|
|
scope :user_provided, -> { where(provider_type: :user) }
|
|
|
|
scope :gcp_provided, -> { where(provider_type: :gcp) }
|
|
|
|
scope :aws_provided, -> { where(provider_type: :aws) }
|
|
|
|
|
2019-09-24 02:06:02 -04:00
|
|
|
scope :gcp_installed, -> { gcp_provided.joins(:provider_gcp).merge(Clusters::Providers::Gcp.with_status(:created)) }
|
2019-10-16 17:07:22 -04:00
|
|
|
scope :aws_installed, -> { aws_provided.joins(:provider_aws).merge(Clusters::Providers::Aws.with_status(:created)) }
|
|
|
|
|
2019-05-02 21:05:53 -04:00
|
|
|
scope :managed, -> { where(managed: true) }
|
2018-03-30 11:34:10 -04:00
|
|
|
|
2018-03-21 10:59:40 -04:00
|
|
|
scope :default_environment, -> { where(environment_scope: DEFAULT_ENVIRONMENT) }
|
2017-10-29 14:48:45 -04:00
|
|
|
|
2019-10-24 08:06:03 -04:00
|
|
|
scope :for_project_namespace, -> (namespace_id) { joins(:projects).where(projects: { namespace_id: namespace_id }) }
|
|
|
|
|
2018-12-02 16:22:33 -05:00
|
|
|
def self.ancestor_clusters_for_clusterable(clusterable, hierarchy_order: :asc)
|
2019-04-17 22:45:31 -04:00
|
|
|
return [] if clusterable.is_a?(Instance)
|
|
|
|
|
2018-12-03 06:38:20 -05:00
|
|
|
hierarchy_groups = clusterable.ancestors_upto(hierarchy_order: hierarchy_order).eager_load(:clusters)
|
|
|
|
hierarchy_groups = hierarchy_groups.merge(current_scope) if current_scope
|
2018-10-28 22:28:36 -04:00
|
|
|
|
2019-04-17 22:45:31 -04:00
|
|
|
hierarchy_groups.flat_map(&:clusters) + Instance.new.clusters
|
2018-10-28 22:28:36 -04:00
|
|
|
end
|
|
|
|
|
2017-11-02 10:10:46 -04:00
|
|
|
def status_name
|
2019-05-27 04:23:17 -04:00
|
|
|
provider&.status_name || connection_status.presence || :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def connection_status
|
|
|
|
with_reactive_cache do |data|
|
|
|
|
data[:connection_status]
|
2017-11-02 10:10:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-27 04:23:17 -04:00
|
|
|
def calculate_reactive_cache
|
|
|
|
return unless enabled?
|
|
|
|
|
|
|
|
{ connection_status: retrieve_connection_status }
|
2017-12-01 10:49:56 -05:00
|
|
|
end
|
|
|
|
|
2017-11-02 12:57:50 -04:00
|
|
|
def applications
|
2019-09-17 08:06:48 -04:00
|
|
|
APPLICATIONS.values.map do |application_class|
|
|
|
|
public_send(application_class.association_name) || public_send("build_#{application_class.association_name}") # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
2017-11-02 12:57:50 -04:00
|
|
|
end
|
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
def provider
|
2019-10-16 17:07:22 -04:00
|
|
|
if gcp?
|
|
|
|
provider_gcp
|
|
|
|
elsif aws?
|
|
|
|
provider_aws
|
|
|
|
end
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def platform
|
2017-10-23 04:36:35 -04:00
|
|
|
return platform_kubernetes if kubernetes?
|
|
|
|
end
|
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
def first_project
|
2018-11-03 06:13:05 -04:00
|
|
|
strong_memoize(:first_project) do
|
|
|
|
projects.first
|
|
|
|
end
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
2017-10-29 14:48:45 -04:00
|
|
|
alias_method :project, :first_project
|
2017-10-23 04:36:35 -04:00
|
|
|
|
2018-11-03 06:13:05 -04:00
|
|
|
def first_group
|
|
|
|
strong_memoize(:first_group) do
|
|
|
|
groups.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias_method :group, :first_group
|
|
|
|
|
2019-04-09 22:13:43 -04:00
|
|
|
def instance
|
|
|
|
Instance.new if instance_type?
|
|
|
|
end
|
|
|
|
|
2017-11-02 10:10:46 -04:00
|
|
|
def kubeclient
|
|
|
|
platform_kubernetes.kubeclient if kubernetes?
|
|
|
|
end
|
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
def kubernetes_namespace_for(environment)
|
|
|
|
project = environment.project
|
|
|
|
persisted_namespace = Clusters::KubernetesNamespaceFinder.new(
|
|
|
|
self,
|
|
|
|
project: project,
|
2019-09-23 11:06:32 -04:00
|
|
|
environment_name: environment.name
|
2019-08-07 00:40:29 -04:00
|
|
|
).execute
|
2019-06-13 20:18:50 -04:00
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
persisted_namespace&.namespace || Gitlab::Kubernetes::DefaultNamespace.new(self, project: project).from_environment_slug(environment.slug)
|
2018-11-02 11:46:15 -04:00
|
|
|
end
|
|
|
|
|
2018-11-06 18:30:36 -05:00
|
|
|
def allow_user_defined_namespace?
|
2019-05-24 04:39:59 -04:00
|
|
|
project_type? || !managed?
|
2018-11-06 18:30:36 -05:00
|
|
|
end
|
|
|
|
|
2019-02-05 14:11:33 -05:00
|
|
|
def kube_ingress_domain
|
2019-05-19 23:55:11 -04:00
|
|
|
@kube_ingress_domain ||= domain.presence || instance_domain
|
2019-01-23 11:28:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def predefined_variables
|
|
|
|
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
2019-02-05 14:11:33 -05:00
|
|
|
break variables unless kube_ingress_domain
|
2019-01-23 11:28:19 -05:00
|
|
|
|
2019-02-05 14:11:33 -05:00
|
|
|
variables.append(key: KUBE_INGRESS_BASE_DOMAIN, value: kube_ingress_domain)
|
2019-01-23 11:28:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-22 20:06:29 -04:00
|
|
|
def knative_pre_installed?
|
|
|
|
provider&.knative_pre_installed?
|
|
|
|
end
|
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
private
|
|
|
|
|
2019-10-14 08:06:14 -04:00
|
|
|
def unique_management_project_environment_scope
|
|
|
|
return unless management_project
|
|
|
|
|
|
|
|
duplicate_management_clusters = management_project.management_clusters
|
|
|
|
.where(environment_scope: environment_scope)
|
|
|
|
.where.not(id: id)
|
|
|
|
|
|
|
|
if duplicate_management_clusters.any?
|
|
|
|
errors.add(:environment_scope, "cannot add duplicated environment scope")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-23 11:28:19 -05:00
|
|
|
def instance_domain
|
2019-02-05 14:11:33 -05:00
|
|
|
@instance_domain ||= Gitlab::CurrentSettings.auto_devops_domain
|
|
|
|
end
|
|
|
|
|
2019-05-27 04:23:17 -04:00
|
|
|
def retrieve_connection_status
|
|
|
|
kubeclient.core_client.discover
|
|
|
|
rescue *Gitlab::Kubernetes::Errors::CONNECTION
|
|
|
|
:unreachable
|
|
|
|
rescue *Gitlab::Kubernetes::Errors::AUTHENTICATION
|
|
|
|
:authentication_failure
|
|
|
|
rescue Kubeclient::HttpError => e
|
|
|
|
kubeclient_error_status(e.message)
|
|
|
|
rescue => e
|
|
|
|
Gitlab::Sentry.track_acceptable_exception(e, extra: { cluster_id: id })
|
|
|
|
|
|
|
|
:unknown_failure
|
|
|
|
else
|
|
|
|
:connected
|
|
|
|
end
|
|
|
|
|
|
|
|
# KubeClient uses the same error class
|
|
|
|
# For connection errors (eg. timeout) and
|
|
|
|
# for Kubernetes errors.
|
|
|
|
def kubeclient_error_status(message)
|
|
|
|
if message&.match?(/timed out|timeout/i)
|
|
|
|
:unreachable
|
|
|
|
else
|
|
|
|
:authentication_failure
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# To keep backward compatibility with AUTO_DEVOPS_DOMAIN
|
|
|
|
# environment variable, we need to ensure KUBE_INGRESS_BASE_DOMAIN
|
|
|
|
# is set if AUTO_DEVOPS_DOMAIN is set on any of the following options:
|
|
|
|
# ProjectAutoDevops#Domain, project variables or group variables,
|
|
|
|
# as the AUTO_DEVOPS_DOMAIN is needed for CI_ENVIRONMENT_URL
|
|
|
|
#
|
|
|
|
# This method should is scheduled to be removed on
|
2019-09-18 10:02:45 -04:00
|
|
|
# https://gitlab.com/gitlab-org/gitlab-foss/issues/56959
|
2019-05-27 04:23:17 -04:00
|
|
|
def legacy_auto_devops_domain
|
|
|
|
if project_type?
|
|
|
|
project&.auto_devops&.domain.presence ||
|
|
|
|
project.variables.find_by(key: 'AUTO_DEVOPS_DOMAIN')&.value.presence ||
|
|
|
|
project.group&.variables&.find_by(key: 'AUTO_DEVOPS_DOMAIN')&.value.presence
|
|
|
|
elsif group_type?
|
|
|
|
group.variables.find_by(key: 'AUTO_DEVOPS_DOMAIN')&.value.presence
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
def restrict_modification
|
|
|
|
if provider&.on_creation?
|
|
|
|
errors.add(:base, "cannot modify during creation")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2018-10-14 16:42:29 -04:00
|
|
|
|
|
|
|
def no_groups
|
|
|
|
if groups.any?
|
|
|
|
errors.add(:cluster, 'cannot have groups assigned')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def no_projects
|
|
|
|
if projects.any?
|
|
|
|
errors.add(:cluster, 'cannot have projects assigned')
|
|
|
|
end
|
|
|
|
end
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Clusters::Cluster.prepend_if_ee('EE::Clusters::Cluster')
|