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

feature: 订阅支持按操作系统等主机属性进行范围筛选(closed #1452) #1619

Conversation

neko12583
Copy link
Collaborator

No description provided.

@codecov-commenter
Copy link

codecov-commenter commented Jun 15, 2023

Codecov Report

Merging #1619 (c280abd) into v2.3.2-dev (a64bb17) will decrease coverage by 36.59%.
The diff coverage is 23.80%.

@@               Coverage Diff               @@
##           v2.3.2-dev    #1619       +/-   ##
===============================================
- Coverage       73.42%   36.84%   -36.59%     
===============================================
  Files             389      308       -81     
  Lines           26244    21452     -4792     
===============================================
- Hits            19270     7904    -11366     
- Misses           6974    13548     +6574     
Impacted Files Coverage Δ
apps/node_man/handlers/policy.py 14.07% <ø> (-67.66%) ⬇️
apps/backend/subscription/tools.py 12.77% <5.88%> (-44.55%) ⬇️
apps/node_man/models.py 50.98% <100.00%> (-25.60%) ⬇️
apps/node_man/serializers/base.py 57.57% <100.00%> (+1.32%) ⬆️

... and 247 files with indirect coverage changes

@neko12583 neko12583 force-pushed the _v2.3.2-dev/feature_issue_1452 branch 2 times, most recently from c280abd to ef894d8 Compare June 26, 2023 03:13
@@ -45,8 +45,17 @@ def validate(self, data):
return data


# 订阅任务范围主机属性筛选
class SubscriptionScopeInstanceSelectorSerializer(serializers.Serializer):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里考虑缩写?
SubScopeInstSelectorSerializer

@@ -21,12 +21,20 @@
from apps.utils import basic


class SubscriptionScopeInstanceSelectorSerializer(serializers.Serializer):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apps/node_man/serializers/base.py 中新增的是同一个序列化器,可统一为同一个,订阅偏向后台动作,可以保留此处

instances_dict[create_node_id(data)] = instance

# 对 instances 进行二次过滤
if scope.get("instance_selector") and bk_host_id_list:
Copy link
Collaborator

@CohleRustW CohleRustW Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scope.get("instance_selector") 执行了两次,可做一次赋值
并且这里可以准确判断下 None
if instance_selector is not None:

conditions = [
{
"key": key, "value": value
} for selector_param in scope["instance_selector"] for key, value in selector_param.items()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,做赋值

biz_scope=[bk_biz_id],
return_all_node_type=True
)
instance_selector_host_id_set = {host.bk_host_id for host in host_queryset}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里用list

for node_id, instance in instances_dict.items():
instance_data = instance[
"host"
] if data["object_type"] == models.Subscription.ObjectType.HOST else instance["service"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data["object_type"] 可以做赋值,避免取两次


selector_instances_dict = {}
for node_id, instance in instances_dict.items():
instance_data = instance[
Copy link
Collaborator

@CohleRustW CohleRustW Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

selector_host_id: int = instance["host"]["bk_host_id"]....
if selector_host_id in instance_selector_host_id_set

@@ -467,7 +468,7 @@ def migrate_preview(cls, query_params: Dict[str, Any]) -> List[Dict[str, Any]]:
step_obj.subscription = subscription
subscription.steps = step_objs
preview_result = tasks.run_subscription_task_and_create_instance(
subscription, subscription_task, preview_only=True
subscription, subscription_task, scope, preview_only=True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要显式传入 scope,我看这个函数里会取 subscription.scope

@@ -84,6 +84,7 @@ def create_subscription(self, request):
object_type=scope["object_type"],
node_type=scope["node_type"],
nodes=scope["nodes"],
instance_selector=scope.get("instance_selector"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update 接口也要支持更新

@@ -21,12 +21,20 @@
from apps.utils import basic


class SubscriptionScopeInstanceSelectorSerializer(serializers.Serializer):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件下还有 UpdateScopeSerializer / RunScopeSerializer 等 Scope 序列化器,也要相应补充

Update 功能要注意,只有这个参数明确传入,才去做更新

@ZhuoZhuoCrayon ZhuoZhuoCrayon removed the request for review from wyyalt July 13, 2023 03:38
@neko12583 neko12583 force-pushed the _v2.3.2-dev/feature_issue_1452 branch 3 times, most recently from 303c1b4 to 25dfb30 Compare August 8, 2023 12:19
@neko12583 neko12583 changed the base branch from v2.3.2-dev to v2.4.0-dev August 8, 2023 12:26
@neko12583 neko12583 force-pushed the _v2.3.2-dev/feature_issue_1452 branch from 25dfb30 to 1a5865f Compare August 8, 2023 12:47
@neko12583
Copy link
Collaborator Author

自测

安装 agent

1、新安装 agent 不过滤
第一次安装agent不过滤

2、重装 agent 不过滤
重装agent不过滤

安装 agent 实际请求后台创建订阅接口为 saas 层代码控制,不带 instance_selector

安装插件

3、安装插件,不带主机属性过滤
安装插件正常执行预览

正常下发插件

4、修改其中一台主机操作系统为 windows
修改主机操作系统

5、安装插件,携带主机属性过滤
安装插件增加过滤条件

预安装增加筛选条件

安装插件过滤结果

策略部署

6、策略部署,不带主机属性过滤
部署策略不带主机属性筛选

7、策略部署,携带主机属性过滤
策略部署带主机属性过滤

策略部署带主机属性过滤效果

任务巡检

8、将主机属性变更回linux
将主机属性变更回linux

9、执行任务巡检
由于会检测 is_running 仍需将前面插件进程由成功改为失败
主动触发任务巡检

任务巡检,由于将 71 主机操作系统改回 linux,计算动作之后执行插件下发
任务巡检根据主机属性进行计算

服务模板

10、查看服务模板下有哪些主机
服务模板下主机情况

11、将 71 的操作系统改为 windows
修改主机架构为windows

12、组织参数直接请求后台创建订阅接口
saas发起任务

saas发起任务1

13、查看主机过滤情况
数据库任务详情

数据库任务详情1

@ZhuoZhuoCrayon ZhuoZhuoCrayon changed the base branch from v2.4.0-dev to v2.4.1-dev August 15, 2023 07:28
@neko12583 neko12583 force-pushed the _v2.3.2-dev/feature_issue_1452 branch from 1a5865f to 8a9f209 Compare August 15, 2023 07:49
Copy link
Member

@ZhuoZhuoCrayon ZhuoZhuoCrayon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ZhuoZhuoCrayon ZhuoZhuoCrayon merged commit cca52bf into TencentBlueKing:v2.4.1-dev Aug 15, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants