Commit b38b5ceb authored by Pawel Chojnacki's avatar Pawel Chojnacki

Move client creation to Prometheus Application, manufacture proper rest client

parent db2433c3
...@@ -14,10 +14,6 @@ module Clusters ...@@ -14,10 +14,6 @@ module Clusters
'stable/prometheus' 'stable/prometheus'
end end
def namespace
Gitlab::Kubernetes::Helm::NAMESPACE
end
def service_name def service_name
'prometheus-prometheus-server' 'prometheus-prometheus-server'
end end
...@@ -33,6 +29,17 @@ module Clusters ...@@ -33,6 +29,17 @@ module Clusters
def install_command def install_command
Gitlab::Kubernetes::Helm::InstallCommand.new(name, chart: chart, chart_values_file: chart_values_file) Gitlab::Kubernetes::Helm::InstallCommand.new(name, chart: chart, chart_values_file: chart_values_file)
end 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 end
end end
...@@ -86,7 +86,7 @@ class PrometheusService < MonitoringService ...@@ -86,7 +86,7 @@ class PrometheusService < MonitoringService
# Cache metrics for specific environment # Cache metrics for specific environment
def calculate_reactive_cache(query_class_name, environment_id, *args) def calculate_reactive_cache(query_class_name, environment_id, *args)
return unless active? && project && !project.pending_delete? 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) data = Kernel.const_get(query_class_name).new(client).query(environment_id, *args)
...@@ -101,12 +101,16 @@ class PrometheusService < MonitoringService ...@@ -101,12 +101,16 @@ class PrometheusService < MonitoringService
def client(environment_id) def client(environment_id)
if manual_mode? if manual_mode?
Gitlab::PrometheusClient.new(api_url: api_url) Gitlab::PrometheusClient.new(RestClient::Resource.new(api_url))
else else
cluster(environment_id) cluster = find_cluster_with_prometheus(environment_id)
Gitlab::PrometheusClient.new(cluster.application_prometheus.proxy_client) if cluster
end end
end end
private
def find_cluster_with_prometheus(environment_id) def find_cluster_with_prometheus(environment_id)
clusters = if environment_id clusters = if environment_id
::Environment.find_by(id: environment_id).try(:enabled_clusters) || [] ::Environment.find_by(id: environment_id).try(:enabled_clusters) || []
...@@ -117,33 +121,6 @@ class PrometheusService < MonitoringService ...@@ -117,33 +121,6 @@ class PrometheusService < MonitoringService
clusters.detect { |cluster| cluster.application_prometheus.installed? } clusters.detect { |cluster| cluster.application_prometheus.installed? }
end 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) def rename_data_to_metrics(metrics)
metrics[:metrics] = metrics.delete :data metrics[:metrics] = metrics.delete :data
metrics metrics
......
...@@ -3,12 +3,10 @@ module Gitlab ...@@ -3,12 +3,10 @@ module Gitlab
# Helper methods to interact with Prometheus network services & resources # Helper methods to interact with Prometheus network services & resources
class PrometheusClient class PrometheusClient
attr_reader :api_url, :rest_client, :headers attr_reader :rest_client, :headers
def initialize(api_url:, rest_client: nil, headers: nil) def initialize(rest_client)
@api_url = api_url
@rest_client = rest_client || RestClient::Resource.new(api_url) @rest_client = rest_client || RestClient::Resource.new(api_url)
@headers = headers || {}
end end
def ping def ping
...@@ -49,7 +47,7 @@ module Gitlab ...@@ -49,7 +47,7 @@ module Gitlab
end end
def get(path, args) def get(path, args)
response = rest_client[path].get(headers.merge(params: args)) response = rest_client[path].get(params: args)
handle_response(response) handle_response(response)
rescue SocketError rescue SocketError
raise PrometheusError, "Can't connect to #{url}" raise PrometheusError, "Can't connect to #{url}"
...@@ -60,7 +58,7 @@ module Gitlab ...@@ -60,7 +58,7 @@ module Gitlab
end end
def handle_response(response) def handle_response(response)
json_data = json_response(response) json_data = JSON.parse(response.body)
if response.code == 200 && json_data['status'] == 'success' if response.code == 200 && json_data['status'] == 'success'
json_data['data'] || {} json_data['data'] || {}
elsif response.code == 400 elsif response.code == 400
...@@ -70,10 +68,6 @@ module Gitlab ...@@ -70,10 +68,6 @@ module Gitlab
end end
end end
def json_response(response)
JSON.parse(response.body)
end
def get_result(expected_type) def get_result(expected_type)
data = yield data = yield
data['result'] if data['resultType'] == expected_type data['result'] if data['resultType'] == expected_type
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment