diff --git a/apps/backend/agent/manager.py b/apps/backend/agent/manager.py index 97ad0c99d..1fa8393d3 100644 --- a/apps/backend/agent/manager.py +++ b/apps/backend/agent/manager.py @@ -191,9 +191,12 @@ def start_nginx(cls): return act @classmethod - def render_and_push_gse_config(cls, name=components.RenderAndPushGseConfigComponent.name): + def render_and_push_gse_config( + cls, is_need_request_agent_version: bool = False, name=components.RenderAndPushGseConfigComponent.name + ): """渲染并下载 agent 配置""" act = AgentServiceActivity(component_code=components.RenderAndPushGseConfigComponent.code, name=name) + act.component.inputs.is_need_request_agent_version = Var(type=Var.PLAIN, value=is_need_request_agent_version) return act @classmethod diff --git a/apps/backend/components/collections/agent_new/render_and_push_gse_config.py b/apps/backend/components/collections/agent_new/render_and_push_gse_config.py index ced816bc9..de6bae4f9 100644 --- a/apps/backend/components/collections/agent_new/render_and_push_gse_config.py +++ b/apps/backend/components/collections/agent_new/render_and_push_gse_config.py @@ -9,16 +9,45 @@ specific language governing permissions and limitations under the License. """ +from dataclasses import dataclass, fields from typing import Any, Dict, List from apps.node_man import models +from apps.node_man.periodic_tasks.sync_agent_status_task import ( + update_or_create_host_agent_status, +) from apps.utils.files import PathHandler from .base import AgentCommonData, AgentPushConfigService +@dataclass +class RenderAndPushConfigCommonData(AgentCommonData): + # 主机Agent版本信息 + host_id__agent_state_info: Dict[int, Dict[str, Any]] + + class RenderAndPushGseConfigService(AgentPushConfigService): - def get_config_info_list(self, data, common_data: AgentCommonData, host: models.Host) -> List[Dict[str, Any]]: + def _execute(self, data, parent_data, common_data: AgentCommonData): + # 2.0重载配置实时获取Agent版本 + is_need_request_agent_version: bool = data.get_one_of_inputs("is_need_request_agent_version") + if not is_need_request_agent_version or common_data.agent_step_adapter.is_legacy: + host_id__agent_state_info: Dict = {} + else: + host_id__agent_state_info: Dict[int, Dict[str, Any]] = update_or_create_host_agent_status( + task_id="[reload_agent_fill_agent_state_info_to_hosts]", + host_queryset=models.Host.objects.filter(bk_host_id__in=common_data.bk_host_ids), + ) + render_and_push_common_data = RenderAndPushConfigCommonData( + host_id__agent_state_info=host_id__agent_state_info, + **{field.name: getattr(common_data, field.name) for field in fields(common_data)} + ) + + return super()._execute(data, parent_data, render_and_push_common_data) + + def get_config_info_list( + self, data, common_data: RenderAndPushConfigCommonData, host: models.Host + ) -> List[Dict[str, Any]]: file_name_list: List[str] = common_data.agent_step_adapter.get_config_filename_by_node_type(host.node_type) general_node_type = self.get_general_node_type(host.node_type) host_ap: models.AccessPoint = self.get_host_ap(common_data=common_data, host=host) @@ -29,13 +58,17 @@ def get_config_info_list(self, data, common_data: AgentCommonData, host: models. { "file_name": file_name, "content": common_data.agent_step_adapter.get_config( - host=host, filename=file_name, node_type=general_node_type, ap=host_ap + host=host, + filename=file_name, + node_type=general_node_type, + ap=host_ap, + target_version=common_data.host_id__agent_state_info.get(host.bk_host_id, {}).get("version"), ), } ) return config_file_list - def get_file_target_path(self, data, common_data: AgentCommonData, host: models.Host) -> str: + def get_file_target_path(self, data, common_data: RenderAndPushConfigCommonData, host: models.Host) -> str: general_node_type = self.get_general_node_type(host.node_type) path_handler = PathHandler(host.os_type) host_ap: models.AccessPoint = self.get_host_ap(common_data=common_data, host=host) diff --git a/apps/backend/subscription/steps/agent.py b/apps/backend/subscription/steps/agent.py index 30e7fcabf..ec6bb466b 100644 --- a/apps/backend/subscription/steps/agent.py +++ b/apps/backend/subscription/steps/agent.py @@ -316,6 +316,7 @@ class UpgradeAgent(ReinstallAgent): def _generate_activities(self, agent_manager: AgentManager): activities = [ agent_manager.push_upgrade_package(), + agent_manager.render_and_push_gse_config(), agent_manager.run_upgrade_command(), agent_manager.wait(30), agent_manager.get_agent_status(expect_status=constants.ProcStateType.RUNNING), @@ -457,6 +458,7 @@ class UpgradeProxy(ReinstallProxy): def _generate_activities(self, agent_manager: AgentManager): activities = [ agent_manager.push_upgrade_package(), + agent_manager.render_and_push_gse_config(), agent_manager.run_upgrade_command(), agent_manager.wait(30), agent_manager.get_agent_status(expect_status=constants.ProcStateType.RUNNING), @@ -533,7 +535,7 @@ def _generate_activities(self, agent_manager: AgentManager): activities = [ agent_manager.check_agent_status(), agent_manager.update_install_info(), - agent_manager.render_and_push_gse_config(), + agent_manager.render_and_push_gse_config(is_need_request_agent_version=True), agent_manager.reload_agent(skip_polling_result=True), agent_manager.wait(5), agent_manager.get_agent_status(expect_status=constants.ProcStateType.RUNNING), diff --git a/apps/backend/subscription/steps/agent_adapter/adapter.py b/apps/backend/subscription/steps/agent_adapter/adapter.py index e1e1e4dcd..aea7b186b 100644 --- a/apps/backend/subscription/steps/agent_adapter/adapter.py +++ b/apps/backend/subscription/steps/agent_adapter/adapter.py @@ -102,9 +102,13 @@ def _get_config( ap: models.AccessPoint, proxies: typing.List[models.Host], install_channel: typing.Tuple[typing.Optional[models.Host], typing.Dict[str, typing.List]], + target_version: typing.Optional[typing.Dict[int, str]] = None, ) -> str: agent_setup_info: base.AgentSetupInfo = self.setup_info - config_handler: GseConfigHandler = self.get_config_handler(agent_setup_info.name, agent_setup_info.version) + # 目标版本优先使用传入版本,传入版本必不会是标签所以可直接使用 + config_handler: GseConfigHandler = self.get_config_handler( + agent_setup_info.name, target_version or agent_setup_info.version + ) config_tmpl_obj: base.AgentConfigTemplate = config_handler.get_matching_config_tmpl( os_type=host.os_type, cpu_arch=host.cpu_arch, @@ -134,6 +138,7 @@ def get_config( ap: typing.Optional[models.AccessPoint] = None, proxies: typing.Optional[typing.List[models.Host]] = None, install_channel: typing.Tuple[typing.Optional[models.Host], typing.Dict[str, typing.List]] = None, + target_version: typing.Optional[str] = None, ) -> str: """ 获取配置 @@ -152,7 +157,13 @@ def get_config( func: typing.Callable[..., str] = (self._get_config, legacy.generate_gse_config)[self.is_legacy] return func( - host=host, filename=filename, node_type=node_type, ap=ap, proxies=proxies, install_channel=install_channel + host=host, + filename=filename, + node_type=node_type, + ap=ap, + proxies=proxies, + install_channel=install_channel, + target_version=target_version, ) @property diff --git a/apps/backend/subscription/steps/agent_adapter/config_context/context_dataclass.py b/apps/backend/subscription/steps/agent_adapter/config_context/context_dataclass.py index 522278be8..21dab5159 100644 --- a/apps/backend/subscription/steps/agent_adapter/config_context/context_dataclass.py +++ b/apps/backend/subscription/steps/agent_adapter/config_context/context_dataclass.py @@ -194,6 +194,12 @@ class DataAgentConfigContext(GseConfigContext, TlsBaseConfigContext): tcp_server_thread_num: int = 32 tcp_server_max_message_size: int = 10485760 + # 兼容Agent 2.0 >= 2.1.5 gse_data_proxy 配置文件结构变更 + bind_port: int = 0 + bind_ip: str = "::" + + thread_num: int = 32 + @dataclass class DataProxyConfigContext(GseConfigContext, TlsBaseConfigContext): diff --git a/apps/backend/subscription/steps/agent_adapter/config_context/context_helper.py b/apps/backend/subscription/steps/agent_adapter/config_context/context_helper.py index b273bc8e3..7e355732b 100644 --- a/apps/backend/subscription/steps/agent_adapter/config_context/context_helper.py +++ b/apps/backend/subscription/steps/agent_adapter/config_context/context_helper.py @@ -153,6 +153,8 @@ def __post_init__(self): context_dataclass.DataMetricConfigContext(exporter_bind_port=self.ap.port_config["data_prometheus_port"]), context_dataclass.DataAgentConfigContext( tcp_bind_port=self.ap.port_config["data_port"], + # 兼容Agent 2.0 >= 2.1.5 gse_data_proxy 配置文件结构变更 + bind_port=self.ap.port_config["data_port"], tls_ca_file=proxy_tls_ca_file, tls_cert_file=proxy_tls_cert_file, tls_key_file=proxy_tls_key_file, diff --git a/apps/backend/subscription/steps/agent_adapter/legacy.py b/apps/backend/subscription/steps/agent_adapter/legacy.py index 993240247..a9a1e4cc5 100644 --- a/apps/backend/subscription/steps/agent_adapter/legacy.py +++ b/apps/backend/subscription/steps/agent_adapter/legacy.py @@ -458,6 +458,7 @@ def generate_gse_config( ap: models.AccessPoint, proxies: List[models.Host], install_channel: Tuple[Optional[models.Host], Dict[str, List]], + target_version: Optional[Dict[int, str]] = None, ): """ 生成 GSE 相关配置