Skip to content

Commit

Permalink
fix: 修复proxy主机转移模块后变成pagent问题 (closed TencentBlueKing#2474)
Browse files Browse the repository at this point in the history
  • Loading branch information
Huayeaaa committed Oct 29, 2024
1 parent 7f47072 commit b63276f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
9 changes: 8 additions & 1 deletion apps/node_man/periodic_tasks/sync_cmdb_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,13 @@ def sync_cmdb_host(bk_biz_id=None, task_id=None):
# 节点管理需要删除的host_id
need_delete_host_ids = set(node_man_host_ids) - set(cc_bk_host_ids)
if need_delete_host_ids:
models.Host.objects.filter(bk_host_id__in=need_delete_host_ids).delete()
proxy_host_ids: typing.Set[int] = set(
models.Host.objects.filter(
bk_host_id__in=need_delete_host_ids, node_type=constants.NodeType.PROXY
).values_list("bk_host_id", flat=True)
)
need_delete_agent_host_ids = need_delete_host_ids - proxy_host_ids
models.Host.objects.filter(bk_host_id__in=need_delete_agent_host_ids).delete()
models.IdentityData.objects.filter(bk_host_id__in=need_delete_host_ids).delete()
if not LPUSH_AND_EXPIRE_FUNC:
models.ProcessStatus.objects.filter(bk_host_id__in=need_delete_host_ids).delete()
Expand Down Expand Up @@ -566,6 +572,7 @@ def query_cmdb_and_handle_need_delete_host_ids(host_ids: typing.List[int], task_
)
)
final_delete_host_ids = set(host_ids) - set(bk_host_ids_in_cmdb)
models.Host.objects.filter(bk_host_id__in=final_delete_host_ids).delete()
models.ProcessStatus.objects.filter(bk_host_id__in=final_delete_host_ids).delete()
logger.info(
"[clear_final_delete_host_ids] task_id -> %s, final_delete_host_ids -> %s, num -> %s"
Expand Down
43 changes: 41 additions & 2 deletions apps/node_man/tests/test_pericdic_tasks/test_sync_cmdb_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def test_sync_cmdb_host(self):
)
self.assertEqual(itf_results.sort(key=lambda t: t[0]), list(db_result).sort(key=lambda t: t[0]))

# 验证主机信息是否删除成功
self.assertEqual(Host.objects.filter(bk_host_id=-1).count(), 0)
# 验证主机信息是否删除成功/proxy主机信息先保留
self.assertEqual(Host.objects.filter(bk_host_id=-1).count(), 1)


class TestClearNeedDeleteHostIds(CustomBaseTestCase):
Expand Down Expand Up @@ -139,3 +139,42 @@ def test_sync_multi_outer_ip_host(self):
for data in agent_extra_data:
extra_data = data["extra_data"]
self.assertEqual(extra_data.get("bk_host_multi_outerip"), None)


class TestProxyTransferModule(CustomBaseTestCase):
@staticmethod
def init_db():
host_data = copy.deepcopy(MOCK_HOST)
host_list = []
for index in range(1, MOCK_HOST_NUM):
host_data["node_type"] = constants.NodeType.PROXY if index % 2 else constants.NodeType.AGENT
host_data["inner_ip"] = f"127.0.0.{index}"
host_data["bk_host_id"] = index
host_list.append(Host(**host_data))
host_data["bk_host_id"] = 100
host_list.append(Host(**host_data))

Host.objects.bulk_create(host_list)

@staticmethod
def list_hosts_without_biz(*args, **kwargs):
return {
"count": 1,
"info": [
{"bk_host_id": 100},
],
}

def start_patch(self):
MockClient.cc.list_hosts_without_biz = self.list_hosts_without_biz

@patch("apps.node_man.periodic_tasks.sync_cmdb_host.client_v2", MockClient)
def test_transfer_module_proxy(self):
self.init_db()
sync_cmdb_host_periodic_task(bk_biz_id=MOCK_BK_BIZ_ID)
# 验证需要删除的proxy主机暂存
self.assertEqual(Host.objects.filter(bk_host_id=100, node_type=constants.NodeType.PROXY).count(), 1)
self.start_patch()
clear_need_delete_host_ids_task()
# 验证转移业务/模块的proxy主未被删除
self.assertEqual(Host.objects.filter(bk_host_id=100, node_type=constants.NodeType.PROXY).count(), 1)

0 comments on commit b63276f

Please sign in to comment.