prometheus.rb 3.2 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

P
wip  
Pawel Chojnacki 已提交
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 53 54 55 56 57 58 59
      def uninstall_command
        Gitlab::Kubernetes::Helm::DeleteCommand.new(
          name: name,
          rbac: cluster.platform_kubernetes_rbac?,
          files: files
        )
      end

60
      def upgrade_command(values)
61 62
        ::Gitlab::Kubernetes::Helm::InstallCommand.new(
          name: name,
63 64
          version: VERSION,
          rbac: cluster.platform_kubernetes_rbac?,
65
          chart: chart,
66 67 68 69 70 71 72 73 74 75 76 77
          files: files_with_replaced_values(values)
        )
      end

      # 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

78
      def prometheus_client
79
        return unless kube_client
80 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)
        RestClient::Resource.new(proxy_url, options)
86 87 88 89 90
      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
91
      end
92 93 94

      private

95 96
      def disable_prometheus_integration
        cluster.projects.each do |project|
97
          project.prometheus_service&.update!(active: false)
98 99 100
        end
      end

101
      def kube_client
102
        cluster&.kubeclient&.core_client
103
      end
104 105 106 107

      def install_knative_metrics
        ["kubectl apply -f #{Clusters::Applications::Knative::METRICS_CONFIG}"] if cluster.application_knative_available?
      end
108 109 110
    end
  end
end