Read about our upcoming Code of Conduct on this issue

prometheus.rb 3.78 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
module Clusters
  module Applications
5
    class Prometheus < ApplicationRecord
6
7
      include PrometheusAdapter

8
      VERSION = '6.7.3'
9
10
11
12
13

      self.table_name = 'clusters_applications_prometheus'

      include ::Clusters::Concerns::ApplicationCore
      include ::Clusters::Concerns::ApplicationStatus
14
      include ::Clusters::Concerns::ApplicationVersion
15
      include ::Clusters::Concerns::ApplicationData
16
17
18

      default_value_for :version, VERSION

19
20
      after_destroy :disable_prometheus_integration

21
22
23
      state_machine :status do
        after_transition any => [:installed] do |application|
          application.cluster.projects.each do |project|
24
            project.find_or_initialize_service('prometheus').update!(active: true)
25
26
27
28
          end
        end
      end

29
30
31
32
      def chart
        'stable/prometheus'
      end

Pawel Chojnacki's avatar
wip    
Pawel Chojnacki committed
33
34
35
36
37
38
39
40
      def service_name
        'prometheus-prometheus-server'
      end

      def service_port
        80
      end

41
      def install_command
42
        Gitlab::Kubernetes::Helm::InstallCommand.new(
43
          name: name,
44
          version: VERSION,
45
          rbac: cluster.platform_kubernetes_rbac?,
46
          chart: chart,
47
48
          files: files,
          postinstall: install_knative_metrics
49
        )
50
      end
51

52
      def upgrade_command(values)
53
54
        ::Gitlab::Kubernetes::Helm::InstallCommand.new(
          name: name,
55
56
          version: VERSION,
          rbac: cluster.platform_kubernetes_rbac?,
57
          chart: chart,
58
59
60
61
          files: files_with_replaced_values(values)
        )
      end

João Cunha's avatar
João Cunha committed
62
63
64
65
66
      def uninstall_command
        Gitlab::Kubernetes::Helm::DeleteCommand.new(
          name: name,
          rbac: cluster.platform_kubernetes_rbac?,
          files: files,
67
          predelete: delete_knative_istio_metrics
João Cunha's avatar
João Cunha committed
68
69
70
        )
      end

71
72
73
74
75
76
77
78
      # 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

79
      def prometheus_client
80
        return unless kube_client
81
82
83
84
85

        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)
86
        Gitlab::PrometheusClient.new(proxy_url, options)
87
      rescue Kubeclient::HttpError, Errno::ECONNRESET, Errno::ECONNREFUSED
88
89
90
91
        # 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
92
      end
93

94
95
96
97
      def configured?
        kube_client.present? && available?
      end

98
99
      private

100
101
      def disable_prometheus_integration
        cluster.projects.each do |project|
102
          project.prometheus_service&.update!(active: false)
103
104
105
        end
      end

106
      def kube_client
107
        cluster&.kubeclient&.core_client
108
      end
109
110

      def install_knative_metrics
111
112
        return [] unless cluster.application_knative_available?

João Cunha's avatar
João Cunha committed
113
        [Gitlab::Kubernetes::KubectlCmd.apply_file(Clusters::Applications::Knative::METRICS_CONFIG)]
114
      end
João Cunha's avatar
João Cunha committed
115
116

      def delete_knative_istio_metrics
117
118
        return [] unless cluster.application_knative_available?

119
120
121
122
123
124
        [
          Gitlab::Kubernetes::KubectlCmd.delete(
            "-f", Clusters::Applications::Knative::METRICS_CONFIG,
            "--ignore-not-found"
          )
        ]
João Cunha's avatar
João Cunha committed
125
      end
126
127
128
    end
  end
end
129
130

Clusters::Applications::Prometheus.prepend_if_ee('EE::Clusters::Applications::Prometheus')