Class: Clusters::Cluster

Inherits:
ApplicationRecord show all
Includes:
AfterCommitQueue, FromUnion, Gitlab::Utils::StrongMemoize, HasEnvironmentScope, Presentable, ReactiveCaching
Defined in:
app/models/clusters/cluster.rb

Constant Summary collapse

DEFAULT_ENVIRONMENT =
'*'
KUBE_INGRESS_BASE_DOMAIN =
'KUBE_INGRESS_BASE_DOMAIN'

Constants included from ReactiveCaching

ReactiveCaching::ExceededReactiveCacheLimit, ReactiveCaching::InvalidateReactiveCache, ReactiveCaching::WORK_TYPE

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods included from Presentable

#present

Methods included from HasEnvironmentScope

#environment_scope=

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, nullable_column?, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.ancestor_clusters_for_clusterable(clusterable, hierarchy_order: :asc) ⇒ Object



115
116
117
118
119
120
121
122
# File 'app/models/clusters/cluster.rb', line 115

def self.ancestor_clusters_for_clusterable(clusterable, hierarchy_order: :asc)
  return [] if clusterable.is_a?(Instance)

  hierarchy_groups = clusterable.ancestors_upto(hierarchy_order: hierarchy_order).eager_load(:clusters)
  hierarchy_groups = hierarchy_groups.merge(current_scope) if current_scope

  hierarchy_groups.flat_map(&:clusters) + Instance.new.clusters
end

.has_one_cluster_application(name) ⇒ Object

rubocop:disable Naming/PredicateName



41
42
43
44
# File 'app/models/clusters/cluster.rb', line 41

def self.has_one_cluster_application(name) # rubocop:disable Naming/PredicateName
  application = APPLICATIONS[name.to_s]
  has_one application.association_name, class_name: application.to_s, inverse_of: :cluster
end

Instance Method Details

#all_projectsObject



161
162
163
164
165
166
# File 'app/models/clusters/cluster.rb', line 161

def all_projects
  return projects if project_type?
  return groups_projects if group_type?

  ::Project.all
end

#allow_user_defined_namespace?Boolean

Returns:

  • (Boolean)


279
280
281
# File 'app/models/clusters/cluster.rb', line 279

def allow_user_defined_namespace?
  project_type? || !managed?
end

#calculate_reactive_cacheObject



205
206
207
208
209
# File 'app/models/clusters/cluster.rb', line 205

def calculate_reactive_cache
  return unless enabled?

  connection_data.merge(Gitlab::Kubernetes::Node.new(self).all)
end

#clusterableObject



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'app/models/clusters/cluster.rb', line 299

def clusterable
  return unless cluster_type

  case cluster_type
  when 'project_type'
    project
  when 'group_type'
    group
  when 'instance_type'
    instance
  else
    raise NotImplementedError
  end
end

#connection_errorObject



175
176
177
178
179
# File 'app/models/clusters/cluster.rb', line 175

def connection_error
  with_reactive_cache do |data|
    data[:connection_error]
  end
end

#connection_statusObject



193
194
195
196
197
# File 'app/models/clusters/cluster.rb', line 193

def connection_status
  with_reactive_cache do |data|
    data[:connection_status]
  end
end

#delete_cached_resources!Object



295
296
297
# File 'app/models/clusters/cluster.rb', line 295

def delete_cached_resources!
  kubernetes_namespaces.delete_all(:delete_all)
end

#find_or_build_integration_prometheusObject



211
212
213
# File 'app/models/clusters/cluster.rb', line 211

def find_or_build_integration_prometheus
  integration_prometheus || build_integration_prometheus
end

#first_groupObject Also known as: group



254
255
256
257
258
# File 'app/models/clusters/cluster.rb', line 254

def first_group
  strong_memoize(:first_group) do
    groups.first
  end
end

#first_projectObject Also known as: project



247
248
249
250
251
# File 'app/models/clusters/cluster.rb', line 247

def first_project
  strong_memoize(:first_project) do
    projects.first
  end
end

#instanceObject



261
262
263
# File 'app/models/clusters/cluster.rb', line 261

def instance
  Instance.new if instance_type?
end

#integration_prometheus_available?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'app/models/clusters/cluster.rb', line 231

def integration_prometheus_available?
  !!integration_prometheus&.available?
end

#knative_pre_installed?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'app/models/clusters/cluster.rb', line 219

def knative_pre_installed?
  !!provider&.knative_pre_installed?
end

#kube_ingress_domainObject



283
284
285
# File 'app/models/clusters/cluster.rb', line 283

def kube_ingress_domain
  @kube_ingress_domain ||= domain.presence || instance_domain
end

#kubeclientObject



265
266
267
# File 'app/models/clusters/cluster.rb', line 265

def kubeclient
  platform_kubernetes&.kubeclient if kubernetes?
end

#kubernetes_namespace_for(environment, deployable: environment.last_deployable) ⇒ Object



269
270
271
272
273
274
275
276
277
# File 'app/models/clusters/cluster.rb', line 269

def kubernetes_namespace_for(environment, deployable: environment.last_deployable)
  if deployable && environment.project_id != deployable.project_id
    raise ArgumentError, 'environment.project_id must match deployable.project_id'
  end

  managed_namespace(environment) ||
    ci_configured_namespace(deployable) ||
    default_namespace(environment)
end

#metrics_connection_errorObject



187
188
189
190
191
# File 'app/models/clusters/cluster.rb', line 187

def metrics_connection_error
  with_reactive_cache do |data|
    data[:metrics_connection_error]
  end
end

#node_connection_errorObject



181
182
183
184
185
# File 'app/models/clusters/cluster.rb', line 181

def node_connection_error
  with_reactive_cache do |data|
    data[:node_connection_error]
  end
end

#nodesObject



199
200
201
202
203
# File 'app/models/clusters/cluster.rb', line 199

def nodes
  with_reactive_cache do |data|
    data[:nodes]
  end
end

#on_creation?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'app/models/clusters/cluster.rb', line 215

def on_creation?
  !!provider&.on_creation?
end

#platformObject



243
244
245
# File 'app/models/clusters/cluster.rb', line 243

def platform
  return platform_kubernetes if kubernetes?
end

#platform_kubernetes_active?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'app/models/clusters/cluster.rb', line 223

def platform_kubernetes_active?
  !!platform_kubernetes&.active?
end

#platform_kubernetes_rbac?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'app/models/clusters/cluster.rb', line 227

def platform_kubernetes_rbac?
  !!platform_kubernetes&.rbac?
end

#predefined_variablesObject



287
288
289
290
291
292
293
# File 'app/models/clusters/cluster.rb', line 287

def predefined_variables
  Gitlab::Ci::Variables::Collection.new.tap do |variables|
    break variables unless kube_ingress_domain

    variables.append(key: KUBE_INGRESS_BASE_DOMAIN, value: kube_ingress_domain)
  end
end

#prometheus_adapterObject



314
315
316
# File 'app/models/clusters/cluster.rb', line 314

def prometheus_adapter
  integration_prometheus
end

#providerObject



235
236
237
238
239
240
241
# File 'app/models/clusters/cluster.rb', line 235

def provider
  if gcp?
    provider_gcp
  elsif aws?
    provider_aws
  end
end

#status_nameObject



168
169
170
171
172
173
# File 'app/models/clusters/cluster.rb', line 168

def status_name
  return cleanup_status_name if cleanup_errored?
  return :cleanup_ongoing unless cleanup_not_started?

  provider&.status_name || connection_status.presence || :created
end