From 21bfdfa86fa79fff17444874cbe64abe7ab29904 Mon Sep 17 00:00:00 2001 From: adevjoe Date: Tue, 18 Jul 2023 18:10:45 +0800 Subject: [PATCH] feat: change get_gse_agent_status api (merge request !1289) Squash merge branch 'fix/gse-1.28' into 'v1.28.x' feat: change get_gse_agent_status api --- bcs-ui/backend/components/cc/constants.py | 1 + bcs-ui/backend/components/gse.py | 16 ++++--- .../clusters/cc_host/utils.py | 44 ++++++++++--------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/bcs-ui/backend/components/cc/constants.py b/bcs-ui/backend/components/cc/constants.py index e6d09cf968..009852c827 100644 --- a/bcs-ui/backend/components/cc/constants.py +++ b/bcs-ui/backend/components/cc/constants.py @@ -37,6 +37,7 @@ 'bk_host_outerip', 'rack', 'bk_cloud_id', + 'bk_agent_id' ] # 默认从 0 开始查询 diff --git a/bcs-ui/backend/components/gse.py b/bcs-ui/backend/components/gse.py index 13cf54f6cd..97f033601c 100644 --- a/bcs-ui/backend/components/gse.py +++ b/bcs-ui/backend/components/gse.py @@ -25,22 +25,24 @@ GSE_HOST = settings.COMPONENT_HOST BK_APP_CODE = settings.APP_ID BK_APP_SECRET = settings.APP_TOKEN -PREFIX_PATH = "/api/c/compapi" -FUNCTION_PATH_MAP = {"agent_status": "/v2/gse/get_agent_status"} +PREFIX_PATH = "/api/bk-gse/prod/api" +FUNCTION_PATH_MAP = {"agent_status": "/v2/cluster/list_agent_state"} -def get_agent_status(username, hosts, bk_supplier_id=0): +def get_agent_status(username, agentIDList): url = "{host}{prefix_path}{path}".format( host=GSE_HOST, prefix_path=PREFIX_PATH, path=FUNCTION_PATH_MAP["agent_status"] ) - data = {"bk_app_code": BK_APP_CODE, "bk_app_secret": BK_APP_SECRET, "bk_username": username, "hosts": hosts} - if bk_supplier_id is not None: - data["bk_supplier_id"] = bk_supplier_id + if len(agentIDList) == 0: + return [] + + data = {"bk_app_code": BK_APP_CODE, "bk_app_secret": BK_APP_SECRET, "bk_username": username, + "agent_id_list": agentIDList} resp = http_post(url, json=data) if resp.get("code") != ErrorCode.NoError: raise error_codes.APIError.f(resp.get("message")) - return resp.get("data", {}).values() + return resp.get("data", []) try: diff --git a/bcs-ui/backend/container_service/clusters/cc_host/utils.py b/bcs-ui/backend/container_service/clusters/cc_host/utils.py index 1172715178..b5b254b67e 100644 --- a/bcs-ui/backend/container_service/clusters/cc_host/utils.py +++ b/bcs-ui/backend/container_service/clusters/cc_host/utils.py @@ -133,28 +133,30 @@ def is_host_selectable(host: Dict) -> bool: def update_gse_agent_status(username, host_list: List) -> List: """更新 GSE Agent 状态信息""" - gse_params = [] - for info in host_list: - bk_cloud_id = info.get('bk_cloud_id') or 0 - gse_params.extend( - [ - {'plat_id': bk_cloud_id, 'bk_cloud_id': bk_cloud_id, 'ip': ip} - for ip in info.get('bk_host_innerip', '').split(',') - ] - ) - gse_host_status_map = {info['ip']: info for info in gse.get_agent_status(username, gse_params)} + agentIDList = [] + for host in host_list: + if not host.get('bk_agent_id'): + continue + agentIDList.append(host.get('bk_agent_id', "")) + gse_host_status_array = gse.get_agent_status(username, agentIDList) + gse_host_status_map = {} + for agent_status in gse_host_status_array: + # Agent current status code: + # -1:UNKNOWN 0:INIT 1:STARTING 2:RUNNING 3:DAMAGED 4:BUSY 5:UPGRADING 6:STOPPING 7:UNINIT + status = 1 + if agent_status.get("status_code", -1) != 2: + status = 0 + gse_host_status_map[agent_status.get("bk_agent_id")] = status + # 根据 IP 匹配更新 Agent 信息 - cc_host_map = {host['bk_host_innerip']: host for host in host_list} - for ips in cc_host_map: - # 同主机可能存在多个 IP,任一 IP Agent 正常即可 - for ip in ips.split(','): - if ip not in gse_host_status_map: - continue - ip_status = gse_host_status_map[ip] - cc_host_map[ips]['agent_alive'] = ip_status.get('bk_agent_alive') - break - - return list(cc_host_map.values()) + result = [] + for host in host_list: + host['agent_alive'] = 0 + if host.get('bk_agent_id'): + host['agent_alive'] = gse_host_status_map.get(host.get('bk_agent_id'), 0) + result.append(host) + + return result try: