Skip to content

Commit

Permalink
feat: change get_gse_agent_status api (merge request !1289)
Browse files Browse the repository at this point in the history
Squash merge branch 'fix/gse-1.28' into 'v1.28.x'
feat: change get_gse_agent_status api
  • Loading branch information
adevjoe authored and ifooth committed Jul 18, 2023
1 parent 0bd3e34 commit 21bfdfa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
1 change: 1 addition & 0 deletions bcs-ui/backend/components/cc/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'bk_host_outerip',
'rack',
'bk_cloud_id',
'bk_agent_id'
]

# 默认从 0 开始查询
Expand Down
16 changes: 9 additions & 7 deletions bcs-ui/backend/components/gse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
44 changes: 23 additions & 21 deletions bcs-ui/backend/container_service/clusters/cc_host/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 21bfdfa

Please sign in to comment.