From efbe7a57f3e4f5e3e5d1c982e17c73ef83f2e073 Mon Sep 17 00:00:00 2001 From: dcd <1151627903@qq.com> Date: Mon, 28 Oct 2024 15:45:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dproxy=E4=B8=BB?= =?UTF-8?q?=E6=9C=BA=E8=BD=AC=E7=A7=BB=E6=A8=A1=E5=9D=97=E5=90=8E=E5=8F=98?= =?UTF-8?q?=E6=88=90pagent=E9=97=AE=E9=A2=98=20(closed=20#2474)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/node_man/periodic_tasks/sync_cmdb_host.py | 9 ++++++++- .../tests/test_pericdic_tasks/test_sync_cmdb_host.py | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) 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):