diff options
author | Pawel Chojnacki <pawel@chojnacki.ws> | 2018-01-02 21:42:24 +0100 |
---|---|---|
committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2018-01-02 21:42:24 +0100 |
commit | b38b5ceb8e039283e90dd323327e59c8f608c103 (patch) | |
tree | cc7525f7681e7bdc15add9577fd5bc74705fa0c6 /app | |
parent | db2433c36da6410c803163139e41228f9ae3f26b (diff) | |
download | gitlab-ce-b38b5ceb8e039283e90dd323327e59c8f608c103.tar.gz |
Move client creation to Prometheus Application, manufacture proper rest client
Diffstat (limited to 'app')
-rw-r--r-- | app/models/clusters/applications/prometheus.rb | 15 | ||||
-rw-r--r-- | app/models/project_services/prometheus_service.rb | 37 |
2 files changed, 18 insertions, 34 deletions
diff --git a/app/models/clusters/applications/prometheus.rb b/app/models/clusters/applications/prometheus.rb index 72651a92e54..94cac9277a5 100644 --- a/app/models/clusters/applications/prometheus.rb +++ b/app/models/clusters/applications/prometheus.rb @@ -14,10 +14,6 @@ module Clusters 'stable/prometheus' end - def namespace - Gitlab::Kubernetes::Helm::NAMESPACE - end - def service_name 'prometheus-prometheus-server' end @@ -33,6 +29,17 @@ module Clusters def install_command Gitlab::Kubernetes::Helm::InstallCommand.new(name, chart: chart, chart_values_file: chart_values_file) end + + def proxy_client + return unless cluster.kubeclient + + kube_client = cluster.kubeclient + 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) + end end end end diff --git a/app/models/project_services/prometheus_service.rb b/app/models/project_services/prometheus_service.rb index bcb19eb8909..5e5aa92fd1a 100644 --- a/app/models/project_services/prometheus_service.rb +++ b/app/models/project_services/prometheus_service.rb @@ -86,7 +86,7 @@ class PrometheusService < MonitoringService # Cache metrics for specific environment def calculate_reactive_cache(query_class_name, environment_id, *args) return unless active? && project && !project.pending_delete? - client = client_for_environment(environment_id) + client = client(environment_id) data = Kernel.const_get(query_class_name).new(client).query(environment_id, *args) @@ -101,12 +101,16 @@ class PrometheusService < MonitoringService def client(environment_id) if manual_mode? - Gitlab::PrometheusClient.new(api_url: api_url) + Gitlab::PrometheusClient.new(RestClient::Resource.new(api_url)) else - cluster(environment_id) + cluster = find_cluster_with_prometheus(environment_id) + + Gitlab::PrometheusClient.new(cluster.application_prometheus.proxy_client) if cluster end end + private + def find_cluster_with_prometheus(environment_id) clusters = if environment_id ::Environment.find_by(id: environment_id).try(:enabled_clusters) || [] @@ -117,33 +121,6 @@ class PrometheusService < MonitoringService clusters.detect { |cluster| cluster.application_prometheus.installed? } end - private - - def client_for_environment(environment_id) - cluster = find_cluster_with_prometheus(environment_id) - return unless cluster - - prometheus = cluster.application_prometheus - - client_through_kube_proxy(cluster.kubeclient, - 'service', - prometheus.service_name, - prometheus.service_port, - prometheus.namespace) if cluster.kubeclient - end - - def client_through_kube_proxy(kube_client, kind, name, port, namespace = '') - rest_client = kube_client.rest_client - base_url = rest_client.url - proxy_url = kube_client.proxy_url(kind, name, port, namespace) - - Rails.logger.warn rest_client[proxy_url.sub(base_url, '')] - Rails.logger.warn proxy_url.sub(base_url, '') - - Gitlab::PrometheusClient.new(api_url: api_url, rest_client: rest_client[proxy_url.sub(base_url, '')], headers: kube_client.headers) - end - - def rename_data_to_metrics(metrics) metrics[:metrics] = metrics.delete :data metrics |