Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend): machin补充agent id信息 #1673 #1674

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dbm-ui/backend/db_meta/api/machine/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def create(
"idc_id",
"bk_cloud_id",
"net_device_id",
"bk_agent_id",
],
"host_property_filter": {
"condition": "AND",
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions dbm-ui/backend/db_meta/migrations/0024_machine_bk_agent_id.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
2 changes: 2 additions & 0 deletions dbm-ui/backend/db_meta/models/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=_("当前的虚拟规格配置"))
Expand Down Expand Up @@ -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",
Expand Down
25 changes: 19 additions & 6 deletions dbm-ui/backend/db_periodic_task/local_tasks/db_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]:
Expand Down Expand Up @@ -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] = []
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions helm-charts/bk-dbm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/dbm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading