Skip to content

Commit

Permalink
feature: 订阅对于业务范围的优先级调整 (closed TencentBlueKing#1944)
Browse files Browse the repository at this point in the history
  • Loading branch information
CohleRustW committed Nov 20, 2023
1 parent 1e25657 commit 5bd15f2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
19 changes: 15 additions & 4 deletions apps/backend/subscription/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from django.db.models import Q
from django.utils import timezone

from apps.backend.constants import InstNodeType
from apps.backend.subscription import task_tools
from apps.backend.subscription.commons import get_host_by_inst, list_biz_hosts
from apps.backend.subscription.constants import SUBSCRIPTION_SCOPE_CACHE_TIME
Expand Down Expand Up @@ -673,13 +674,24 @@ def get_host_relation(bk_biz_id, nodes):
return data


def full_nodes_biz_info(nodes: typing.List[typing.Dict[str, typing.Union[str, int]]]):
for node in nodes:
bk_biz_id: typing.Optional[int] = node.get("bk_biz_id")
bk_obj_id: typing.Optional[str] = node.get("bk_obj_id")
bk_inst_id: typing.Optional[int] = node.get("bk_inst_id")
if not bk_biz_id and bk_obj_id == InstNodeType.BIZ and bk_inst_id:
# 兜底类似 [{"ip": 127.0.0.1, "bk_inst_id": x, "bk_obj_id": biz}] 场景
# 填充为 [{"ip": 127.0.0.1, "bk_inst_id": x, "bk_obj_id": biz, "bk_biz_id": x}]
node["bk_biz_id"] = bk_inst_id


def support_multi_biz(get_instances_by_scope_func):
"""支持scope多范围"""

@wraps(get_instances_by_scope_func)
def wrapper(scope: Dict[str, Union[Dict, Any]], *args, **kwargs) -> Dict[str, Dict[str, Union[Dict, Any]]]:
if scope.get("bk_biz_id") is not None:
return get_instances_by_scope_func(scope, **kwargs)
nodes: typing.List[typing.Dict[str, typing.Union[str, int]]] = scope["nodes"]
full_nodes_biz_info(nodes=nodes)
# 兼容只传bk_host_id的情况
if (
scope["object_type"] == models.Subscription.ObjectType.HOST
Expand All @@ -689,7 +701,6 @@ def wrapper(scope: Dict[str, Union[Dict, Any]], *args, **kwargs) -> Dict[str, Di
return get_instances_by_scope_func(scope, **kwargs)

instance_id_info_map = {}
nodes = sorted(scope["nodes"], key=lambda node: node["bk_biz_id"])
params_list = [
{
"scope": {
Expand All @@ -701,7 +712,7 @@ def wrapper(scope: Dict[str, Union[Dict, Any]], *args, **kwargs) -> Dict[str, Di
},
**kwargs,
}
for bk_biz_id, nodes in groupby(nodes, key=lambda x: x["bk_biz_id"])
for bk_biz_id, nodes in groupby(nodes, key=lambda x: x.get("bk_biz_id") or scope.get("bk_biz_id"))
]
results = request_multi_thread(get_instances_by_scope_func, params_list, get_data=lambda x: [x])
for result in results:
Expand Down
30 changes: 29 additions & 1 deletion apps/backend/tests/subscription/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
CmdbClient,
list_biz_hosts_without_info_client,
)
from apps.node_man import models, constants
from apps.node_man import constants, models

# 全局使用的mock
run_task = mock.patch("apps.backend.subscription.tasks.run_subscription_task").start()
Expand Down Expand Up @@ -203,3 +203,31 @@ def test_get_empty_list_instance_selector_scope(self):
}
)
self.assertEqual(len(list(instances.keys())), 0)

def test_sub_biz_priority(self):
# 之前订阅优先使用 scope.bk_biz_id 作为整个订阅的业务范围,后面调整为优先使用 scope.nodes 内的业务范围
instances = get_instances_by_scope(
{
"object_type": "HOST",
"node_type": "INSTANCE",
"bk_biz_id": 2,
"nodes": [
{"ip": "127.0.0.1", "bk_cloud_id": 0, "bk_supplier_id": 0, "bk_biz_id": 2},
{"ip": "127.0.0.2", "bk_cloud_id": 0, "bk_supplier_id": 0, "bk_biz_id": 3},
],
}
)

self.assertEqual(len(list(instances.keys())), 1)
instances = get_instances_by_scope(
{
"object_type": "HOST",
"node_type": "INSTANCE",
"nodes": [
{"ip": "127.0.0.1", "bk_cloud_id": 0, "bk_supplier_id": 0, "bk_inst_id": 2, "bk_obj_id": "biz"},
],
}
)
pass

self.assertEqual(len(list(instances.keys())), 1)

0 comments on commit 5bd15f2

Please sign in to comment.