-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: 支持 upgrade_to_agent_id 升级为 AgentID 配置 (closed #1309)
- Loading branch information
1 parent
872d446
commit 5f907a5
Showing
15 changed files
with
531 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
apps/backend/components/collections/agent_new/upgrade_to_agent_id.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
|
||
from typing import Dict, List, Union | ||
|
||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from apps.adapters.api.gse import GseApiHelper | ||
|
||
from .base import AgentBaseService, AgentCommonData | ||
|
||
|
||
class UpgradeToAgentIDService(AgentBaseService): | ||
def _execute(self, data, parent_data, common_data: AgentCommonData): | ||
upgrade_hosts: List[Dict[str, Union[int, str]]] = [] | ||
cloud_ip__sub_inst_id_map: Dict[str, int] = {} | ||
# 如果主机有AgentID,则调用 upgrade_to_agent_id 将基于 Host IP 的配置升级到基于 Agent-ID 的配置 | ||
for host_id, sub_inst_id in common_data.host_id__sub_inst_id_map.items(): | ||
host = common_data.host_id_obj_map[host_id] | ||
upgrade_hosts.append( | ||
{"ip": host.inner_ip, "bk_cloud_id": host.bk_cloud_id, "bk_agent_id": host.bk_agent_id} | ||
) | ||
cloud_ip__sub_inst_id_map[f"{host.bk_cloud_id}:{host.inner_ip}"] = sub_inst_id | ||
|
||
upgrade_hosts = [ | ||
{"ip": host.inner_ip, "bk_cloud_id": host.bk_cloud_id, "bk_agent_id": host.bk_agent_id} | ||
for host in common_data.host_id_obj_map.values() | ||
if host.bk_agent_id | ||
] | ||
|
||
if not upgrade_hosts: | ||
return True | ||
result = GseApiHelper.upgrade_to_agent_id(hosts=upgrade_hosts) | ||
failed_cloud_ips = result.get("failed") or [] | ||
failed_sub_inst_ids = [cloud_ip__sub_inst_id_map[cloud_ip] for cloud_ip in failed_cloud_ips] | ||
self.move_insts_to_failed(failed_sub_inst_ids, log_content=_("升级 Agent-ID 配置失败")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
apps/backend/tests/components/collections/agent_new/test_upgrade_to_agent_id.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
from typing import Callable, Dict, List | ||
|
||
import mock | ||
|
||
from apps.adapters.api import gse | ||
from apps.backend.components.collections.agent_new.components import ( | ||
UpgradeToAgentIDComponent, | ||
) | ||
from apps.mock_data import api_mkd | ||
from apps.mock_data import utils as mock_data_utils | ||
from apps.node_man import models | ||
from env.constants import GseVersion | ||
from pipeline.component_framework.test import ComponentTestCase, ExecuteAssertion | ||
|
||
from . import utils | ||
|
||
|
||
class UpgradeToAgentIDTestCaseMixin: | ||
GSE_API_MOCK_PATHS: List[str] = ["apps.backend.components.collections.agent_new.upgrade_to_agent_id.GseApiHelper"] | ||
GSE_VERSION = GseVersion.V2.value | ||
upgrade_to_agent_id_func: Callable[[Dict], Dict] = lambda: {"success": [], "failed": []} | ||
|
||
@classmethod | ||
def setup_obj_factory(cls): | ||
"""设置 obj_factory""" | ||
cls.obj_factory.init_host_num = 5 | ||
|
||
@classmethod | ||
def get_default_case_name(cls) -> str: | ||
return UpgradeToAgentIDComponent.name | ||
|
||
@classmethod | ||
def adjust_test_data_in_db(cls): | ||
""" | ||
调整DB中的测试数据 | ||
:return: | ||
""" | ||
for host_obj in cls.obj_factory.host_objs: | ||
host_obj.bk_agent_id = f"{host_obj.bk_cloud_id}:{host_obj.inner_ip}" | ||
models.Host.objects.bulk_update(cls.obj_factory.host_objs, fields=["bk_agent_id"]) | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
super().setUpTestData() | ||
cls.adjust_test_data_in_db() | ||
|
||
def fetch_succeeded_sub_inst_ids(self) -> List[int]: | ||
return self.common_inputs["subscription_instance_ids"] | ||
|
||
def component_cls(self): | ||
return UpgradeToAgentIDComponent | ||
|
||
def init_mock_clients(self): | ||
self.gse_api_mock_client = api_mkd.gse.utils.GseApiMockClient( | ||
v2_proc_upgrade_to_agent_id_return=mock_data_utils.MockReturn( | ||
return_type=mock_data_utils.MockReturnType.SIDE_EFFECT.value, return_obj=self.upgrade_to_agent_id_func | ||
) | ||
) | ||
|
||
def setUp(self) -> None: | ||
self.init_mock_clients() | ||
for gse_api_mock_path in self.GSE_API_MOCK_PATHS: | ||
mock.patch( | ||
gse_api_mock_path, | ||
gse.get_gse_api_helper(self.GSE_VERSION)( | ||
version=self.GSE_VERSION, gse_api_obj=self.gse_api_mock_client | ||
), | ||
).start() | ||
super().setUp() | ||
|
||
|
||
class UpgradeToAgentIDSuccessTestCase(UpgradeToAgentIDTestCaseMixin, utils.AgentServiceBaseTestCase): | ||
@staticmethod | ||
def upgrade_to_agent_id_func(params): | ||
return { | ||
"success": [f'{host_info["bk_cloud_id"]}:{host_info["ip"]}' for host_info in params["hosts"]], | ||
"failed": [], | ||
} | ||
|
||
@classmethod | ||
def get_default_case_name(cls) -> str: | ||
return "升级Agent-ID全部成功" | ||
|
||
def cases(self): | ||
return [ | ||
ComponentTestCase( | ||
name=self.get_default_case_name(), | ||
inputs=self.common_inputs, | ||
parent_data={}, | ||
execute_assertion=ExecuteAssertion( | ||
success=bool(self.fetch_succeeded_sub_inst_ids()), | ||
outputs={"succeeded_subscription_instance_ids": self.fetch_succeeded_sub_inst_ids()}, | ||
), | ||
schedule_assertion=None, | ||
execute_call_assertion=None, | ||
) | ||
] | ||
|
||
|
||
class UpgradeToAgentIDFailedTestCase(UpgradeToAgentIDTestCaseMixin, utils.AgentServiceBaseTestCase): | ||
@staticmethod | ||
def upgrade_to_agent_id_func(params): | ||
return { | ||
"success": [], | ||
"failed": [f'{host_info["bk_cloud_id"]}:{host_info["ip"]}' for host_info in params["hosts"]], | ||
} | ||
|
||
@classmethod | ||
def get_default_case_name(cls) -> str: | ||
return "升级Agent-ID全部失败" | ||
|
||
def cases(self): | ||
return [ | ||
ComponentTestCase( | ||
name=self.get_default_case_name(), | ||
inputs=self.common_inputs, | ||
parent_data={}, | ||
execute_assertion=ExecuteAssertion( | ||
success=False, | ||
outputs={"succeeded_subscription_instance_ids": []}, | ||
), | ||
schedule_assertion=None, | ||
execute_call_assertion=None, | ||
) | ||
] | ||
|
||
|
||
class UpgradeToAgentIDHalfSuccessTestCase(UpgradeToAgentIDTestCaseMixin, utils.AgentServiceBaseTestCase): | ||
@staticmethod | ||
def upgrade_to_agent_id_func(params): | ||
hosts = sorted(params["hosts"], key=lambda host: host["ip"]) | ||
# mock 前一半的主机为失败,后一半为成功 | ||
half_index = int(len(hosts) / 2) | ||
return { | ||
"success": [f'{host_info["bk_cloud_id"]}:{host_info["ip"]}' for host_info in hosts[:half_index]], | ||
"failed": [f'{host_info["bk_cloud_id"]}:{host_info["ip"]}' for host_info in hosts[half_index:]], | ||
} | ||
|
||
@classmethod | ||
def get_default_case_name(cls) -> str: | ||
return "升级Agent-ID部分成功部分失败" | ||
|
||
def fetch_succeeded_sub_inst_ids(self) -> List[int]: | ||
# 最终成功的订阅实例只有前一半 | ||
return self.common_inputs["subscription_instance_ids"][: int(self.obj_factory.init_host_num / 2)] | ||
|
||
def cases(self): | ||
return [ | ||
ComponentTestCase( | ||
name=self.get_default_case_name(), | ||
inputs=self.common_inputs, | ||
parent_data={}, | ||
execute_assertion=ExecuteAssertion( | ||
success=bool(self.fetch_succeeded_sub_inst_ids()), | ||
outputs={"succeeded_subscription_instance_ids": self.fetch_succeeded_sub_inst_ids()}, | ||
), | ||
schedule_assertion=None, | ||
execute_call_assertion=None, | ||
) | ||
] | ||
|
||
|
||
class UpgradeToAgentIDWithV1GseTestCase(UpgradeToAgentIDTestCaseMixin, utils.AgentServiceBaseTestCase): | ||
GSE_VERSION = GseVersion.V1.value | ||
|
||
@classmethod | ||
def get_default_case_name(cls) -> str: | ||
return "V1 GSE API 无 upgrade_to_agent_id,执行失败" | ||
|
||
def cases(self): | ||
return [ | ||
ComponentTestCase( | ||
name=self.get_default_case_name(), | ||
inputs=self.common_inputs, | ||
parent_data={}, | ||
execute_assertion=ExecuteAssertion( | ||
success=False, | ||
outputs={}, | ||
), | ||
schedule_assertion=None, | ||
execute_call_assertion=None, | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" |
Oops, something went wrong.