debian-mirror-gitlab/app/models/clusters/cluster.rb

171 lines
5.3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Clusters
class Cluster < ActiveRecord::Base
include Presentable
2018-12-13 13:39:08 +05:30
include Gitlab::Utils::StrongMemoize
2018-03-17 18:26:18 +05:30
self.table_name = 'clusters'
APPLICATIONS = {
Applications::Helm.application_name => Applications::Helm,
Applications::Ingress.application_name => Applications::Ingress,
2018-03-27 19:54:05 +05:30
Applications::Prometheus.application_name => Applications::Prometheus,
2018-11-08 19:23:39 +05:30
Applications::Runner.application_name => Applications::Runner,
2018-12-13 13:39:08 +05:30
Applications::Jupyter.application_name => Applications::Jupyter,
Applications::Knative.application_name => Applications::Knative
2018-03-17 18:26:18 +05:30
}.freeze
2018-05-01 15:08:00 +05:30
DEFAULT_ENVIRONMENT = '*'.freeze
2018-03-17 18:26:18 +05:30
belongs_to :user
has_many :cluster_projects, class_name: 'Clusters::Project'
has_many :projects, through: :cluster_projects, class_name: '::Project'
2018-12-13 13:39:08 +05:30
has_one :cluster_project, -> { order(id: :desc) }, class_name: 'Clusters::Project'
has_many :cluster_groups, class_name: 'Clusters::Group'
has_many :groups, through: :cluster_groups, class_name: '::Group'
2018-03-17 18:26:18 +05:30
# we force autosave to happen when we save `Cluster` model
has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp', autosave: true
has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes', autosave: true
has_one :application_helm, class_name: 'Clusters::Applications::Helm'
has_one :application_ingress, class_name: 'Clusters::Applications::Ingress'
has_one :application_prometheus, class_name: 'Clusters::Applications::Prometheus'
2018-03-27 19:54:05 +05:30
has_one :application_runner, class_name: 'Clusters::Applications::Runner'
2018-11-08 19:23:39 +05:30
has_one :application_jupyter, class_name: 'Clusters::Applications::Jupyter'
2018-12-13 13:39:08 +05:30
has_one :application_knative, class_name: 'Clusters::Applications::Knative'
has_many :kubernetes_namespaces
has_one :kubernetes_namespace, -> { order(id: :desc) }, class_name: 'Clusters::KubernetesNamespace'
2018-03-17 18:26:18 +05:30
accepts_nested_attributes_for :provider_gcp, update_only: true
accepts_nested_attributes_for :platform_kubernetes, update_only: true
validates :name, cluster_name: true
2018-12-13 13:39:08 +05:30
validates :cluster_type, presence: true
2018-03-17 18:26:18 +05:30
validate :restrict_modification, on: :update
2018-12-13 13:39:08 +05:30
validate :no_groups, unless: :group_type?
validate :no_projects, unless: :project_type?
2018-03-17 18:26:18 +05:30
delegate :status, to: :provider, allow_nil: true
delegate :status_reason, to: :provider, allow_nil: true
delegate :on_creation?, to: :provider, allow_nil: true
delegate :active?, to: :platform_kubernetes, prefix: true, allow_nil: true
2018-11-20 20:47:30 +05:30
delegate :rbac?, to: :platform_kubernetes, prefix: true, allow_nil: true
2018-12-13 13:39:08 +05:30
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
enum cluster_type: {
instance_type: 1,
group_type: 2,
project_type: 3
}
2018-03-17 18:26:18 +05:30
enum platform_type: {
kubernetes: 1
}
enum provider_type: {
user: 0,
gcp: 1
}
scope :enabled, -> { where(enabled: true) }
scope :disabled, -> { where(enabled: false) }
2018-05-09 12:01:36 +05:30
scope :user_provided, -> { where(provider_type: ::Clusters::Cluster.provider_types[:user]) }
scope :gcp_provided, -> { where(provider_type: ::Clusters::Cluster.provider_types[:gcp]) }
scope :gcp_installed, -> { gcp_provided.includes(:provider_gcp).where(cluster_providers_gcp: { status: ::Clusters::Providers::Gcp.state_machines[:status].states[:created].value }) }
2018-05-01 15:08:00 +05:30
scope :default_environment, -> { where(environment_scope: DEFAULT_ENVIRONMENT) }
2018-03-17 18:26:18 +05:30
def status_name
if provider
provider.status_name
else
:created
end
end
def created?
status_name == :created
end
def applications
[
application_helm || build_application_helm,
application_ingress || build_application_ingress,
2018-03-27 19:54:05 +05:30
application_prometheus || build_application_prometheus,
2018-11-08 19:23:39 +05:30
application_runner || build_application_runner,
2018-12-13 13:39:08 +05:30
application_jupyter || build_application_jupyter,
application_knative || build_application_knative
2018-03-17 18:26:18 +05:30
]
end
def provider
return provider_gcp if gcp?
end
def platform
return platform_kubernetes if kubernetes?
end
def managed?
!user?
end
def first_project
2018-12-13 13:39:08 +05:30
strong_memoize(:first_project) do
projects.first
end
2018-03-17 18:26:18 +05:30
end
alias_method :project, :first_project
2018-12-13 13:39:08 +05:30
def first_group
strong_memoize(:first_group) do
groups.first
end
end
alias_method :group, :first_group
2018-03-17 18:26:18 +05:30
def kubeclient
platform_kubernetes.kubeclient if kubernetes?
end
2018-12-13 13:39:08 +05:30
def find_or_initialize_kubernetes_namespace(cluster_project)
kubernetes_namespaces.find_or_initialize_by(
project: cluster_project.project,
cluster_project: cluster_project
)
end
2018-03-17 18:26:18 +05:30
private
def restrict_modification
if provider&.on_creation?
errors.add(:base, "cannot modify during creation")
return false
end
true
end
2018-12-13 13:39:08 +05:30
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
2018-03-17 18:26:18 +05:30
end
end