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
|
2018-03-27 19:54:05 +05:30
|
|
|
include PrometheusAdapter
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
VERSION = '6.7.3'
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
default_value_for :version, VERSION
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
after_destroy :disable_prometheus_integration
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
state_machine :status do
|
|
|
|
after_transition any => [:installed] do |application|
|
|
|
|
application.cluster.projects.each do |project|
|
2019-07-31 22:56:46 +05:30
|
|
|
project.find_or_initialize_service('prometheus').update!(active: true)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def chart
|
|
|
|
'stable/prometheus'
|
|
|
|
end
|
|
|
|
|
|
|
|
def service_name
|
|
|
|
'prometheus-prometheus-server'
|
|
|
|
end
|
|
|
|
|
|
|
|
def service_port
|
|
|
|
80
|
|
|
|
end
|
|
|
|
|
|
|
|
def install_command
|
2018-03-27 19:54:05 +05:30
|
|
|
Gitlab::Kubernetes::Helm::InstallCommand.new(
|
2018-11-18 11:00:15 +05:30
|
|
|
name: name,
|
|
|
|
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,
|
|
|
|
postinstall: install_knative_metrics
|
2018-03-27 19:54:05 +05:30
|
|
|
)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def upgrade_command(values)
|
|
|
|
::Gitlab::Kubernetes::Helm::InstallCommand.new(
|
|
|
|
name: name,
|
|
|
|
version: VERSION,
|
|
|
|
rbac: cluster.platform_kubernetes_rbac?,
|
|
|
|
chart: chart,
|
|
|
|
files: files_with_replaced_values(values)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def uninstall_command
|
|
|
|
Gitlab::Kubernetes::Helm::DeleteCommand.new(
|
|
|
|
name: name,
|
|
|
|
rbac: cluster.platform_kubernetes_rbac?,
|
|
|
|
files: files,
|
|
|
|
predelete: delete_knative_istio_metrics
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def prometheus_client
|
2018-03-17 18:26:18 +05:30
|
|
|
return unless kube_client
|
|
|
|
|
|
|
|
proxy_url = kube_client.proxy_url('service', service_name, service_port, Gitlab::Kubernetes::Helm::NAMESPACE)
|
|
|
|
|
|
|
|
# ensures headers containing auth data are appended to original k8s client options
|
|
|
|
options = kube_client.rest_client.options.merge(headers: kube_client.headers)
|
2019-10-12 21:52:04 +05:30
|
|
|
Gitlab::PrometheusClient.new(proxy_url, options)
|
2018-10-15 14:42:47 +05:30
|
|
|
rescue Kubeclient::HttpError
|
|
|
|
# If users have mistakenly set parameters or removed the depended clusters,
|
|
|
|
# `proxy_url` could raise an exception because gitlab can not communicate with the cluster.
|
|
|
|
# Since `PrometheusAdapter#can_query?` is eargely loaded on environement pages in gitlab,
|
|
|
|
# we need to silence the exceptions
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def disable_prometheus_integration
|
|
|
|
cluster.projects.each do |project|
|
|
|
|
project.prometheus_service&.update!(active: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def kube_client
|
2018-11-20 20:47:30 +05:30
|
|
|
cluster&.kubeclient&.core_client
|
2018-03-17 18:26:18 +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
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
Clusters::Applications::Prometheus.prepend_if_ee('EE::Clusters::Applications::Prometheus')
|