From aba29b7edc3cc4c30615b23c7c5fe71eb6bd2f10 Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Fri, 17 May 2024 15:16:50 +0900 Subject: [PATCH] feat: fix get cluster_info error if there is no metric from start date Signed-off-by: Youngjin Jo --- src/plugin/connector/mimir_connector.py | 12 +++++++++++- src/plugin/manager/cost_manager.py | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/plugin/connector/mimir_connector.py b/src/plugin/connector/mimir_connector.py index 243044b..f7727a7 100644 --- a/src/plugin/connector/mimir_connector.py +++ b/src/plugin/connector/mimir_connector.py @@ -100,6 +100,7 @@ def _get_unix_timestamp(start: str) -> (str, str): def get_kubecost_cluster_info( self, prometheus_query_endpoint: str, + start: str, service_account_id: str, secret_data: dict, ) -> dict: @@ -118,7 +119,16 @@ def get_kubecost_cluster_info( response.raise_for_status() - result = response.json() + result = response.json().get("data", {}).get("result", [{}]) + + if result: + result = response.json() + else: + result = {} + _LOGGER.debug( + f"[get_kubecost_cluster_info] Agent has no metric on your start date: {start}" + ) + return result except requests.HTTPError as http_err: _LOGGER.error( diff --git a/src/plugin/manager/cost_manager.py b/src/plugin/manager/cost_manager.py index 171c91d..031155a 100644 --- a/src/plugin/manager/cost_manager.py +++ b/src/plugin/manager/cost_manager.py @@ -84,7 +84,7 @@ def get_data( prometheus_query_endpoint = f"{secret_data['mimir_endpoint']}/api/v1/query" cluster_info = self.mimir_connector.get_kubecost_cluster_info( - prometheus_query_endpoint, service_account_id, secret_data + prometheus_query_endpoint, start, service_account_id, secret_data ) if promql_response: @@ -153,7 +153,7 @@ def _make_cost_data( x_scope_orgid: str, ) -> dict: cluster_metric = ( - cluster_info.get("data", {}).get("result", [])[0].get("metric", {}) + cluster_info.get("data", {}).get("result", [{}])[0].get("metric", {}) ) costs_data = [] for result in results: