From 9360e90df597e4cbfd1f115184ae597c84557a63 Mon Sep 17 00:00:00 2001 From: Aashish Sharma Date: Thu, 4 Apr 2024 11:22:24 +0530 Subject: [PATCH] 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 --- .../dashboard/controllers/multi_cluster.py | 28 +++++++++++++++++-- src/pybind/mgr/dashboard/openapi.yaml | 22 +++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/pybind/mgr/dashboard/controllers/multi_cluster.py b/src/pybind/mgr/dashboard/controllers/multi_cluster.py index de29496e5e7a8..05477c44a001b 100644 --- a/src/pybind/mgr/dashboard/controllers/multi_cluster.py +++ b/src/pybind/mgr/dashboard/controllers/multi_cluster.py @@ -2,13 +2,16 @@ import base64 import json +import re import time +from urllib.parse import urlparse import requests from .. import mgr from ..exceptions import DashboardException from ..security import Scope +from ..services.orchestrator import OrchClient from ..settings import Settings from ..tools import configure_cors from . import APIDoc, APIRouter, CreatePermission, DeletePermission, Endpoint, \ @@ -89,13 +92,13 @@ class MultiCluster(RESTController): verify=ssl_verify, cert=ssl_certificate) # add prometheus targets - prometheus_url = self._proxy('GET', url, 'api/settings/PROMETHEUS_API_HOST', + prometheus_url = self._proxy('GET', url, 'api/multi-cluster/get_prometheus_api_url', token=cluster_token) - _set_prometheus_targets(prometheus_url['value']) + _set_prometheus_targets(prometheus_url) self.set_multi_cluster_config(fsid, username, url, cluster_alias, - cluster_token, prometheus_url['value'], + cluster_token, prometheus_url, ssl_verify, ssl_certificate) return True @@ -320,6 +323,25 @@ class MultiCluster(RESTController): clusters_token_map = json.loads(clustersTokenMap) return self.check_token_status_array(clusters_token_map) + @Endpoint() + @ReadPermission + def get_prometheus_api_url(self): + prometheus_url = Settings.PROMETHEUS_API_HOST + if prometheus_url is not None: + # check if is url is already in IP format + pattern = r'^(?:https?|http):\/\/(?:\d{1,3}\.){3}\d{1,3}:\d+$' + valid_ip_url = bool(re.match(pattern, prometheus_url)) + if not valid_ip_url: + parsed_url = urlparse(prometheus_url) + hostname = parsed_url.hostname + orch = OrchClient.instance() + inventory_hosts = [host.to_json() for host in orch.hosts.list()] + for host in inventory_hosts: + if host['hostname'] == hostname or host['hostname'] in hostname: + node_ip = host['addr'] + prometheus_url = prometheus_url.replace(hostname, node_ip) + return prometheus_url + @UIRouter('/multi-cluster', Scope.CONFIG_OPT) class MultiClusterUi(RESTController): diff --git a/src/pybind/mgr/dashboard/openapi.yaml b/src/pybind/mgr/dashboard/openapi.yaml index 94adf99969bbc..13d787a429716 100644 --- a/src/pybind/mgr/dashboard/openapi.yaml +++ b/src/pybind/mgr/dashboard/openapi.yaml @@ -7277,6 +7277,28 @@ paths: - jwt: [] tags: - Multi-cluster + /api/multi-cluster/get_prometheus_api_url: + get: + parameters: [] + responses: + '200': + content: + application/vnd.ceph.api.v1.0+json: + type: object + description: OK + '400': + description: Operation exception. Please check the response body for details. + '401': + description: Unauthenticated access. Please login first. + '403': + description: Unauthorized access. Please check your permissions. + '500': + description: Unexpected error. Please check the response body for the stack + trace. + security: + - jwt: [] + tags: + - Multi-cluster /api/multi-cluster/reconnect_cluster: put: parameters: [] -- 2.39.5