Skip to content

Commit

Permalink
feature: Agent 2.0 Proxy 配置结构变更、升级、重载适配 (closed TencentBlueKing#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyyalt committed Jan 3, 2024
1 parent 0175c84 commit dd4fa34
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 7 deletions.
5 changes: 4 additions & 1 deletion apps/backend/agent/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion apps/backend/subscription/steps/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
15 changes: 13 additions & 2 deletions apps/backend/subscription/steps/agent_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
"""
获取配置
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions apps/backend/subscription/steps/agent_adapter/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 相关配置
Expand Down

0 comments on commit dd4fa34

Please sign in to comment.