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 ResetOnUnionError

ResetOnUnionError::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, 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 SensitiveSerializableHash

#serializable_hash

Class Method Details

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



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

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



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

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 # rubocop:disable Rails/ReflectionClassName
end

Instance Method Details

#all_projectsObject



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

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

  ::Project.all
end

#allow_user_defined_namespace?Boolean

Returns:

  • (Boolean)


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

def allow_user_defined_namespace?
  project_type? || !managed?
end

#calculate_reactive_cacheObject



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

def calculate_reactive_cache
  return unless enabled?

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

#clusterableObject



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

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



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

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

#connection_statusObject



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

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

#delete_cached_resources!Object



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

def delete_cached_resources!
  kubernetes_namespaces.delete_all(:delete_all)
end

#find_or_build_integration_prometheusObject



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

def find_or_build_integration_prometheus
  integration_prometheus || build_integration_prometheus
end

#first_groupObject Also known as: group



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

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

#first_projectObject Also known as: project



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

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

#instanceObject



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

def instance
  Instance.new if instance_type?
end

#integration_prometheus_available?Boolean

Returns:

  • (Boolean)


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

def integration_prometheus_available?
  !!integration_prometheus&.available?
end

#knative_pre_installed?Boolean

Returns:

  • (Boolean)


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

def knative_pre_installed?
  !!provider&.knative_pre_installed?
end

#kube_ingress_domainObject



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

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

#kubeclientObject



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

def kubeclient
  platform_kubernetes&.kubeclient if kubernetes?
end

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



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

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



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

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

#node_connection_errorObject



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

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

#nodesObject



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

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

#on_creation?Boolean

Returns:

  • (Boolean)


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

def on_creation?
  !!provider&.on_creation?
end

#platformObject



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

def platform
  return platform_kubernetes if kubernetes?
end

#platform_kubernetes_active?Boolean

Returns:

  • (Boolean)


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

def platform_kubernetes_active?
  !!platform_kubernetes&.active?
end

#platform_kubernetes_rbac?Boolean

Returns:

  • (Boolean)


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

def platform_kubernetes_rbac?
  !!platform_kubernetes&.rbac?
end

#predefined_variablesObject



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

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



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

def prometheus_adapter
  integration_prometheus
end

#providerObject



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

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

#status_nameObject



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

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