debian-mirror-gitlab/app/models/clusters/applications/prometheus.rb

137 lines
3.9 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
module Applications
2019-07-07 11:18:12 +05:30
class Prometheus < ApplicationRecord
2021-04-29 21:17:54 +05:30
include ::Clusters::Concerns::PrometheusClient
2018-03-27 19:54:05 +05:30
2020-11-24 15:15:51 +05:30
VERSION = '10.4.1'
2018-03-17 18:26:18 +05:30
self.table_name = 'clusters_applications_prometheus'
include ::Clusters::Concerns::ApplicationCore
include ::Clusters::Concerns::ApplicationStatus
2018-11-18 11:00:15 +05:30
include ::Clusters::Concerns::ApplicationVersion
2018-03-27 19:54:05 +05:30
include ::Clusters::Concerns::ApplicationData
2020-01-01 13:55:28 +05:30
include AfterCommitQueue
2018-03-17 18:26:18 +05:30
default_value_for :version, VERSION
2020-07-28 23:09:34 +05:30
scope :preload_cluster_platform, -> { preload(cluster: [:platform_kubernetes]) }
scope :with_clusters_with_cilium, -> { joins(:cluster).merge(Clusters::Cluster.with_available_cilium) }
2020-04-08 14:13:33 +05:30
attr_encrypted :alert_manager_token,
mode: :per_attribute_iv,
key: Settings.attr_encrypted_db_key_base_truncated,
algorithm: 'aes-256-gcm'
2020-01-01 13:55:28 +05:30
after_destroy do
run_after_commit do
disable_prometheus_integration
end
end
2019-07-31 22:56:46 +05:30
2018-03-17 18:26:18 +05:30
state_machine :status do
2021-04-29 21:17:54 +05:30
after_transition any => [:installed, :externally_installed] do |application|
2020-01-01 13:55:28 +05:30
application.run_after_commit do
Clusters::Applications::ActivateServiceWorker
.perform_async(application.cluster_id, ::PrometheusService.to_param) # rubocop:disable CodeReuse/ServiceClass
2018-03-17 18:26:18 +05:30
end
end
2020-04-22 19:07:51 +05:30
after_transition any => :updating do |application|
2020-06-23 00:09:42 +05:30
application.update(last_update_started_at: Time.current)
2020-04-22 19:07:51 +05:30
end
end
def updated_since?(timestamp)
last_update_started_at &&
last_update_started_at > timestamp &&
!update_errored?
2018-03-17 18:26:18 +05:30
end
def chart
2020-10-24 23:57:45 +05:30
"#{name}/prometheus"
end
def repository
'https://gitlab-org.gitlab.io/cluster-integration/helm-stable-archive'
2018-03-17 18:26:18 +05:30
end
def install_command
2021-01-29 00:20:46 +05:30
helm_command_module::InstallCommand.new(
2018-11-18 11:00:15 +05:30
name: name,
2020-10-24 23:57:45 +05:30
repository: repository,
2018-11-18 11:00:15 +05:30
version: VERSION,
2018-11-20 20:47:30 +05:30
rbac: cluster.platform_kubernetes_rbac?,
2018-03-27 19:54:05 +05:30
chart: chart,
2019-02-15 15:39:39 +05:30
files: files,
2020-10-24 23:57:45 +05:30
postinstall: install_knative_metrics
2018-03-27 19:54:05 +05:30
)
2018-03-17 18:26:18 +05:30
end
2020-01-01 13:55:28 +05:30
def patch_command(values)
2021-01-29 00:20:46 +05:30
helm_command_module::PatchCommand.new(
2019-03-02 22:35:43 +05:30
name: name,
2020-10-24 23:57:45 +05:30
repository: repository,
2020-01-01 13:55:28 +05:30
version: version,
2019-03-02 22:35:43 +05:30
rbac: cluster.platform_kubernetes_rbac?,
chart: chart,
2020-10-24 23:57:45 +05:30
files: files_with_replaced_values(values)
2019-03-02 22:35:43 +05:30
)
end
2019-10-12 21:52:04 +05:30
def uninstall_command
2021-01-29 00:20:46 +05:30
helm_command_module::DeleteCommand.new(
2019-10-12 21:52:04 +05:30
name: name,
rbac: cluster.platform_kubernetes_rbac?,
files: files,
2020-10-24 23:57:45 +05:30
predelete: delete_knative_istio_metrics
2019-10-12 21:52:04 +05:30
)
end
2019-03-02 22:35:43 +05:30
# Returns a copy of files where the values of 'values.yaml'
# are replaced by the argument.
#
# See #values for the data format required
def files_with_replaced_values(replaced_values)
files.merge('values.yaml': replaced_values)
end
2020-04-08 14:13:33 +05:30
def generate_alert_manager_token!
unless alert_manager_token.present?
update!(alert_manager_token: generate_token)
end
end
2018-03-17 18:26:18 +05:30
private
2020-04-08 14:13:33 +05:30
def generate_token
SecureRandom.hex
end
2019-07-31 22:56:46 +05:30
def disable_prometheus_integration
2020-01-01 13:55:28 +05:30
::Clusters::Applications::DeactivateServiceWorker
.perform_async(cluster_id, ::PrometheusService.to_param) # rubocop:disable CodeReuse/ServiceClass
2019-07-31 22:56:46 +05:30
end
2019-02-15 15:39:39 +05:30
def install_knative_metrics
2019-10-12 21:52:04 +05:30
return [] unless cluster.application_knative_available?
[Gitlab::Kubernetes::KubectlCmd.apply_file(Clusters::Applications::Knative::METRICS_CONFIG)]
end
def delete_knative_istio_metrics
return [] unless cluster.application_knative_available?
2019-12-21 20:55:43 +05:30
[
Gitlab::Kubernetes::KubectlCmd.delete(
"-f", Clusters::Applications::Knative::METRICS_CONFIG,
"--ignore-not-found"
)
]
2019-02-15 15:39:39 +05:30
end
2018-03-17 18:26:18 +05:30
end
end
end