diff --git a/apps/node_man/periodic_tasks/sync_cmdb_host.py b/apps/node_man/periodic_tasks/sync_cmdb_host.py index b08c12b57..e35a54943 100644 --- a/apps/node_man/periodic_tasks/sync_cmdb_host.py +++ b/apps/node_man/periodic_tasks/sync_cmdb_host.py @@ -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() @@ -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" diff --git a/apps/node_man/tests/test_pericdic_tasks/test_sync_cmdb_host.py b/apps/node_man/tests/test_pericdic_tasks/test_sync_cmdb_host.py index c9920c7e1..85e0ef0cc 100644 --- a/apps/node_man/tests/test_pericdic_tasks/test_sync_cmdb_host.py +++ b/apps/node_man/tests/test_pericdic_tasks/test_sync_cmdb_host.py @@ -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): @@ -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)