]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: fetch prometheus api host with ip addr 56686/head
authorAashish Sharma <aasharma@li-e74156cc-2f67-11b2-a85c-e98659a63c5c.ibm.com>
Thu, 4 Apr 2024 05:52:24 +0000 (11:22 +0530)
committerAashish Sharma <aasharma@li-e74156cc-2f67-11b2-a85c-e98659a63c5c.ibm.com>
Fri, 12 Apr 2024 11:13:43 +0000 (16:43 +0530)
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 <aasharma@redhat.com>
src/pybind/mgr/dashboard/controllers/multi_cluster.py
src/pybind/mgr/dashboard/openapi.yaml

index de29496e5e7a8eeb0400665de25c988c6947d52b..05477c44a001bca9304348752a0365dd37d9d001 100644 (file)
@@ -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):
index 94adf99969bbc034792609a281ac511af0340524..13d787a42971692e7490fea740c57266e7d941da 100644 (file)
@@ -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: []