From e1c10ff45e618cc3d409cc722947d1f0a22b73cb Mon Sep 17 00:00:00 2001 From: iSecloud <869820505@qq.com> Date: Mon, 6 Nov 2023 19:45:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20machin=E8=A1=A5=E5=85=85agent?= =?UTF-8?q?=20id=E4=BF=A1=E6=81=AF=20#1673?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-ui/backend/db_meta/api/machine/apis.py | 2 ++ .../migrations/0024_machine_bk_agent_id.py | 18 +++++++++++++ dbm-ui/backend/db_meta/models/machine.py | 2 ++ .../db_periodic_task/local_tasks/db_proxy.py | 25 ++++++++++++++----- helm-charts/bk-dbm/Chart.yaml | 4 +-- helm-charts/bk-dbm/charts/dbm/Chart.yaml | 2 +- 6 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 dbm-ui/backend/db_meta/migrations/0024_machine_bk_agent_id.py diff --git a/dbm-ui/backend/db_meta/api/machine/apis.py b/dbm-ui/backend/db_meta/api/machine/apis.py index 07618b5138..50d5639ae4 100644 --- a/dbm-ui/backend/db_meta/api/machine/apis.py +++ b/dbm-ui/backend/db_meta/api/machine/apis.py @@ -56,6 +56,7 @@ def create( "idc_id", "bk_cloud_id", "net_device_id", + "bk_agent_id", ], "host_property_filter": { "condition": "AND", @@ -105,6 +106,7 @@ def create( bk_idc_name=inf.get("idc_name") or "", bk_idc_id=inf.get("idc_id") or 0, bk_cloud_id=inf.get("bk_cloud_id") or 0, + bk_agent_id=inf.get("bk_agent_id") or "", net_device_id=inf.get("net_device_id") or "", # 这个 id 是个逗号分割的字符串 spec_id=spec_id, spec_config=spec_config, diff --git a/dbm-ui/backend/db_meta/migrations/0024_machine_bk_agent_id.py b/dbm-ui/backend/db_meta/migrations/0024_machine_bk_agent_id.py new file mode 100644 index 0000000000..50f43554f9 --- /dev/null +++ b/dbm-ui/backend/db_meta/migrations/0024_machine_bk_agent_id.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.19 on 2023-11-06 11:43 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("db_meta", "0023_cluster_disaster_tolerance_level"), + ] + + operations = [ + migrations.AddField( + model_name="machine", + name="bk_agent_id", + field=models.CharField(default="", help_text="Agent ID", max_length=128), + ), + ] diff --git a/dbm-ui/backend/db_meta/models/machine.py b/dbm-ui/backend/db_meta/models/machine.py index 6fd78a6006..973cc689f8 100644 --- a/dbm-ui/backend/db_meta/models/machine.py +++ b/dbm-ui/backend/db_meta/models/machine.py @@ -45,6 +45,7 @@ class Machine(AuditedModel): bk_idc_name = models.CharField(max_length=128, default="", help_text=_("机房")) bk_idc_id = models.IntegerField(default=0, help_text=_("机房 ID")) bk_cloud_id = models.IntegerField(default=0, help_text=_("云区域 ID")) + bk_agent_id = models.CharField(max_length=128, default="", help_text=_("Agent ID")) net_device_id = models.CharField(max_length=256, default="") # 这个 id 是个逗号分割的字符串 spec_id = models.PositiveBigIntegerField(default=0, help_text=_("虚拟规格ID")) spec_config = models.JSONField(default=dict, help_text=_("当前的虚拟规格配置")) @@ -141,6 +142,7 @@ def get_host_info_from_cmdb(cls, bk_host_id: int) -> dict: "bk_cpu", "bk_disk", "bk_mem", + "bk_agent_id", ], "host_property_filter": { "condition": "AND", diff --git a/dbm-ui/backend/db_periodic_task/local_tasks/db_proxy.py b/dbm-ui/backend/db_periodic_task/local_tasks/db_proxy.py index 36083cc317..8811edb584 100644 --- a/dbm-ui/backend/db_periodic_task/local_tasks/db_proxy.py +++ b/dbm-ui/backend/db_periodic_task/local_tasks/db_proxy.py @@ -23,6 +23,7 @@ from backend.components import JobApi from backend.configuration.constants import DBType from backend.core.consts import BK_PUSH_CONFIG_PAYLOAD +from backend.db_meta.models import Machine from backend.db_periodic_task.local_tasks import register_periodic_task from backend.db_proxy import nginxconf_tpl from backend.db_proxy.constants import JOB_INSTANCE_EXPIRE_TIME, NGINX_PUSH_TARGET_PATH, ExtensionType @@ -38,10 +39,15 @@ def fill_cluster_service_nginx_conf(): """填充集群额外服务的配置信息""" - def _job_push_config_file(_cloud_id, _file_list, _nginx): + def _job_push_config_file(_cloud_id, _agent_id, _file_list, _nginx): # 如果当前nginx的机器agent异常,则抛出日志且不下发。避免阻塞job nginx_ip_list = [ - {"bk_cloud_id": _cloud_id, "ip": _nginx.internal_address, "bk_host_innerip": _nginx.internal_address} + { + "bk_cloud_id": _cloud_id, + "bk_agent_id": _agent_id, + "ip": _nginx.internal_address, + "bk_host_innerip": _nginx.internal_address, + } ] ResourceQueryHelper.fill_agent_status(nginx_ip_list) if not nginx_ip_list[0]["status"]: @@ -71,9 +77,14 @@ def _job_push_config_file(_cloud_id, _file_list, _nginx): for cloud_id in cloud__db_type__extension.keys(): # 获取下发nginx conf的机器 TODO: 后续要改为clb的地址进行转发 nginx = DBCloudProxy.objects.filter(bk_cloud_id=cloud_id).last() - nginx_detail = DBExtension.get_latest_extension( - bk_cloud_id=cloud_id, extension_type=ExtensionType.NGINX - ).details + nginx_extension = DBExtension.get_latest_extension(bk_cloud_id=cloud_id, extension_type=ExtensionType.NGINX) + # 获取nginx的bk_agent_id(兼容gse2.0的agent查询) + if "bk_agent_id" not in nginx_extension.details: + host_info = Machine.get_host_info_from_cmdb(bk_host_id=nginx_extension.details["bk_host_id"]) + nginx_extension.details["bk_agent_id"] = host_info.get("bk_agent_id", "") + nginx_extension.save(update_fields=["details"]) + + nginx_detail = nginx_extension.details file_list: List[Dict[str, str]] = [] extension_ids: List[int] = [] @@ -105,7 +116,9 @@ def _job_push_config_file(_cloud_id, _file_list, _nginx): extension_ids.append(extension.id) # 下发nginx服务配置 - resp = _job_push_config_file(_cloud_id=cloud_id, _file_list=file_list, _nginx=nginx) + resp = _job_push_config_file( + _cloud_id=cloud_id, _agent_id=nginx_detail["bk_agent_id"], _file_list=file_list, _nginx=nginx + ) if resp: # 缓存inst_id和nginx id,用于回调job,默认缓存时间和定时周期一致 RedisConn.lpush(resp["data"]["job_instance_id"], *extension_ids, nginx.id) diff --git a/helm-charts/bk-dbm/Chart.yaml b/helm-charts/bk-dbm/Chart.yaml index db7eda2f4f..12aaf2de83 100644 --- a/helm-charts/bk-dbm/Chart.yaml +++ b/helm-charts/bk-dbm/Chart.yaml @@ -79,5 +79,5 @@ dependencies: description: A Helm chart for bkdbm name: bk-dbm type: application -version: 1.2.0-alpha.78 -appVersion: 1.2.0-alpha.78 +version: 1.2.0-alpha.79 +appVersion: 1.2.0-alpha.79 diff --git a/helm-charts/bk-dbm/charts/dbm/Chart.yaml b/helm-charts/bk-dbm/charts/dbm/Chart.yaml index e67e34220c..d84f52cc41 100644 --- a/helm-charts/bk-dbm/charts/dbm/Chart.yaml +++ b/helm-charts/bk-dbm/charts/dbm/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v2 -appVersion: 1.2.0-alpha.449 +appVersion: 1.2.0-alpha.450 description: A Helm chart for dbm name: dbm type: application