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..466ae1e8b 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):