Skip to content

Commit 9360e90

Browse files
author
Aashish Sharma
committed
mgr/dashboard: fetch prometheus api host with ip addr
Check if the PROMETHEUS_API_HOST value has the ip addr of the host if not replace hostnmame with ip addr Signed-off-by: Aashish Sharma <[email protected]>
1 parent 1fab5ce commit 9360e90

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/pybind/mgr/dashboard/controllers/multi_cluster.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import base64
44
import json
5+
import re
56
import time
7+
from urllib.parse import urlparse
68

79
import requests
810

911
from .. import mgr
1012
from ..exceptions import DashboardException
1113
from ..security import Scope
14+
from ..services.orchestrator import OrchClient
1215
from ..settings import Settings
1316
from ..tools import configure_cors
1417
from . import APIDoc, APIRouter, CreatePermission, DeletePermission, Endpoint, \
@@ -89,13 +92,13 @@ def auth(self, url: str, cluster_alias: str, username: str,
8992
verify=ssl_verify, cert=ssl_certificate)
9093

9194
# add prometheus targets
92-
prometheus_url = self._proxy('GET', url, 'api/settings/PROMETHEUS_API_HOST',
95+
prometheus_url = self._proxy('GET', url, 'api/multi-cluster/get_prometheus_api_url',
9396
token=cluster_token)
9497

95-
_set_prometheus_targets(prometheus_url['value'])
98+
_set_prometheus_targets(prometheus_url)
9699

97100
self.set_multi_cluster_config(fsid, username, url, cluster_alias,
98-
cluster_token, prometheus_url['value'],
101+
cluster_token, prometheus_url,
99102
ssl_verify, ssl_certificate)
100103
return True
101104

@@ -320,6 +323,25 @@ def check_token_status(self, clustersTokenMap=None):
320323
clusters_token_map = json.loads(clustersTokenMap)
321324
return self.check_token_status_array(clusters_token_map)
322325

326+
@Endpoint()
327+
@ReadPermission
328+
def get_prometheus_api_url(self):
329+
prometheus_url = Settings.PROMETHEUS_API_HOST
330+
if prometheus_url is not None:
331+
# check if is url is already in IP format
332+
pattern = r'^(?:https?|http):\/\/(?:\d{1,3}\.){3}\d{1,3}:\d+$'
333+
valid_ip_url = bool(re.match(pattern, prometheus_url))
334+
if not valid_ip_url:
335+
parsed_url = urlparse(prometheus_url)
336+
hostname = parsed_url.hostname
337+
orch = OrchClient.instance()
338+
inventory_hosts = [host.to_json() for host in orch.hosts.list()]
339+
for host in inventory_hosts:
340+
if host['hostname'] == hostname or host['hostname'] in hostname:
341+
node_ip = host['addr']
342+
prometheus_url = prometheus_url.replace(hostname, node_ip)
343+
return prometheus_url
344+
323345

324346
@UIRouter('/multi-cluster', Scope.CONFIG_OPT)
325347
class MultiClusterUi(RESTController):

src/pybind/mgr/dashboard/openapi.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7277,6 +7277,28 @@ paths:
72777277
- jwt: []
72787278
tags:
72797279
- Multi-cluster
7280+
/api/multi-cluster/get_prometheus_api_url:
7281+
get:
7282+
parameters: []
7283+
responses:
7284+
'200':
7285+
content:
7286+
application/vnd.ceph.api.v1.0+json:
7287+
type: object
7288+
description: OK
7289+
'400':
7290+
description: Operation exception. Please check the response body for details.
7291+
'401':
7292+
description: Unauthenticated access. Please login first.
7293+
'403':
7294+
description: Unauthorized access. Please check your permissions.
7295+
'500':
7296+
description: Unexpected error. Please check the response body for the stack
7297+
trace.
7298+
security:
7299+
- jwt: []
7300+
tags:
7301+
- Multi-cluster
72807302
/api/multi-cluster/reconnect_cluster:
72817303
put:
72827304
parameters: []

0 commit comments

Comments
 (0)