Skip to content

Commit

Permalink
perf: gse 1.0接口下线切换 #7440 (#7441)
Browse files Browse the repository at this point in the history
* perf: gse 1.0接口下线切换 #7440

* perf: gse 1.0接口下线切换 #7440

* perf: gse 1.0接口下线切换 #7440

* perf: gse 1.0接口下线切换 #7440

* perf: gse 1.0接口下线切换 #7440

* perf: gse 1.0接口下线切换 #7440
  • Loading branch information
lTimej authored May 16, 2024
1 parent 0c997b0 commit 6cc1016
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
3 changes: 3 additions & 0 deletions api/collections/nodeman.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,6 @@ def get_rsa_public_key(self, executor):

def install_channel(self):
return self._request(method="get", url=_get_nodeman_api_v2("install_channel"), data={})

def get_ipchooser_host_details(self, params: dict):
return self._request(method="post", url=_get_nodeman_api("core/api/ipchooser_host/details"), data=params)
22 changes: 15 additions & 7 deletions pipeline_plugins/cmdb_ip_picker/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from iam.contrib.http import HTTP_AUTH_FORBIDDEN_CODE
from iam.exceptions import RawAuthFailedException

from api.collections.nodeman import BKNodeManClient
from api.utils.request import batch_request
from gcloud.conf import settings
from gcloud.utils import cmdb
Expand All @@ -27,6 +28,7 @@

from .constants import ERROR_CODES, NO_ERROR
from .utils import (
format_agent_data,
get_cmdb_topo_tree,
get_gse_agent_status_ipv6,
get_modules_id,
Expand Down Expand Up @@ -159,22 +161,28 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
bk_agent_id = "{}:{}".format(host["bk_cloud_id"], host["bk_host_innerip"])
host["agent"] = agent_id_status_map.get(bk_agent_id, -1)
else:
client = BKNodeManClient(username=request.user.username)
agent_kwargs = {
"bk_biz_id": bk_biz_id,
"bk_supplier_id": bk_supplier_id,
"hosts": [
{"bk_cloud_id": host["bk_cloud_id"], "ip": host["bk_host_innerip"]}
"all_scope": True,
"host_list": [
{
"cloud_id": host["bk_cloud_id"],
"ip": host["bk_host_innerip"],
"meta": {"bk_biz_id": bk_biz_id, "scope_type": "biz", "scope_id": bk_biz_id},
}
for host in data
if host["bk_host_innerip"] != ""
],
}
agent_result = client.gse.get_agent_status(agent_kwargs)
agent_result = client.get_ipchooser_host_details(agent_kwargs)
if not agent_result["result"]:
message = handle_api_error(_("管控平台(GSE)"), "gse.get_agent_status", agent_kwargs, agent_result)
message = handle_api_error(
_("节点管理(nodeman)"), "nodeman.get_ipchooser_host_details", agent_kwargs, agent_result
)
result = {"result": False, "code": ERROR_CODES.API_GSE_ERROR, "message": message}
return JsonResponse(result)

agent_data = agent_result["data"]
agent_data = format_agent_data(agent_result["data"])
for host in data:
# agent在线状态,0为不在线,1为在线,-1为未知
agent_info = agent_data.get(
Expand Down
15 changes: 14 additions & 1 deletion pipeline_plugins/cmdb_ip_picker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,11 @@ def fetch_host_ip_with_property_filter(self):
fields.append("bk_host_innerip_v6")

host_info = cmdb.get_business_host_topo(
self.username, self.bk_biz_id, self.bk_supplier_account, fields, property_filters=self.property_filters,
self.username,
self.bk_biz_id,
self.bk_supplier_account,
fields,
property_filters=self.property_filters,
)
logger.info("[fetch_host_info] cmdb.get_business_host_topo return: {host_info}".format(host_info=host_info))

Expand Down Expand Up @@ -761,3 +765,12 @@ def send_request(agent_ids):
agent_id_status_map[item["bk_agent_id"]] = status_code

return agent_id_status_map


def format_agent_data(agents):
agent_data = {}
for agent in agents:
key = f"{agent['cloud_area']['id']}:{agent['ip']}"
val = {"ip": agent["ip"], "bk_cloud_id": agent["cloud_area"]["id"], "bk_agent_alive": agent["alive"]}
agent_data[key] = val
return agent_data
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ def setUp(self):
get_agent_status_result = {
"result": True,
"code": 0,
"data": {
"0:1.1.1.1": {"ip": "1.1.1.1", "bk_cloud_id": 0, "bk_agent_alive": 1},
"0:2.2.2.2": {"ip": "2.2.2.2", "bk_cloud_id": 0, "bk_agent_alive": 0},
"1:3.3.3.3": {"ip": "3.3.3.3", "bk_cloud_id": 1, "bk_agent_alive": 1},
"2:4.4.4.4": {"ip": "4.4.4.4", "bk_cloud_id": 2, "bk_agent_alive": 0},
},
"data": [
{"ip": "1.1.1.1", "alive": 1, "cloud_area": {"id": 0, "ip": "1.1.1.1"}},
{"ip": "2.2.2.2", "alive": 0, "cloud_area": {"id": 0, "ip": "2.2.2.2"}},
{"ip": "3.3.3.3", "alive": 1, "cloud_area": {"id": 1, "ip": "3.3.3.3"}},
{"ip": "4.4.4.4", "alive": 0, "cloud_area": {"id": 2, "ip": "3.3.3.3"}},
],
}
mock_project_obj = MagicMock()
mock_project_obj.from_cmdb = True
Expand All @@ -43,7 +43,7 @@ def setUp(self):
mock_project.objects.get = MagicMock(return_value=mock_project_obj)

client = MagicMock()
client.gse.get_agent_status = MagicMock(return_value=get_agent_status_result)
client.get_ipchooser_host_details = MagicMock(return_value=get_agent_status_result)

self.supplier_account_for_project_patcher = patch(
"pipeline_plugins.variables.collections.sites.open.ip_filter_base.supplier_id_for_project",
Expand All @@ -53,7 +53,7 @@ def setUp(self):
"pipeline_plugins.variables.collections.sites.open.ip_filter_base.Project", mock_project
)
self.client = patch(
"pipeline_plugins.variables.collections.sites.open.ip_filter_base.get_client_by_user",
"pipeline_plugins.variables.collections.sites.open.ip_filter_base.get_nodeman_client_by_user",
MagicMock(return_value=client),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from api.collections.nodeman import BKNodeManClient
from gcloud.conf import settings as gcloud_settings
from gcloud.constants import GseAgentStatus
from gcloud.core.models import Project
Expand All @@ -24,11 +25,12 @@
from gcloud.utils.handlers import handle_api_error
from gcloud.utils.ip import IpRegexType, extract_ip_from_ip_str, get_ip_by_regex_type
from pipeline_plugins.base.utils.inject import supplier_account_for_business, supplier_id_for_project
from pipeline_plugins.cmdb_ip_picker.utils import get_gse_agent_status_ipv6
from pipeline_plugins.cmdb_ip_picker.utils import format_agent_data, get_gse_agent_status_ipv6
from pipeline_plugins.components.collections.sites.open.cc.base import cc_get_host_by_innerip_with_ipv6

logger = logging.getLogger("root")
get_client_by_user = gcloud_settings.ESB_GET_CLIENT_BY_USER
get_nodeman_client_by_user = BKNodeManClient


class IpFilterBase(metaclass=ABCMeta):
Expand Down Expand Up @@ -95,18 +97,27 @@ def match_ges_v2(self, gse_agent_status, username, bk_biz_id, bk_supplier_id, or
def match_gse_v1(self, gse_agent_status, username, bk_biz_id, bk_supplier_id, origin_ip_list):
match_ip = origin_ip_list
if gse_agent_status in [GseAgentStatus.ONlINE.value, GseAgentStatus.OFFLINE.value]:
client = get_client_by_user(username)
client = get_nodeman_client_by_user(username=username)
agent_kwargs = {
"bk_biz_id": bk_biz_id,
"bk_supplier_id": bk_supplier_id,
"hosts": origin_ip_list,
"all_scope": True,
"host_list": [
{
"cloud_id": host["bk_cloud_id"],
"ip": host["ip"],
"meta": {"bk_biz_id": bk_biz_id, "scope_type": "biz", "scope_id": bk_biz_id},
}
for host in origin_ip_list
if host["ip"] != ""
],
}
agent_result = client.gse.get_agent_status(agent_kwargs)
agent_result = client.get_ipchooser_host_details(agent_kwargs)

if not agent_result["result"]:
message = handle_api_error(_("管控平台(GSE)"), "gse.get_agent_status", agent_kwargs, agent_result)
message = handle_api_error(
_("节点管理(nodeman)"), "nodeman.get_ipchooser_host_details", agent_kwargs, agent_result
)
raise ApiRequestError(f"ERROR:{message}")

agent_data = agent_result["data"]
agent_data = format_agent_data(agent_result["data"])
agent_online_ip_list = []
agent_offline_ip_list = []
for plat_ip, info in agent_data.items():
Expand Down

0 comments on commit 6cc1016

Please # to comment.