Skip to content

Commit

Permalink
bugfix: 修复渲染插件配置文件缺失 control_info 变量问题(fixed #654)
Browse files Browse the repository at this point in the history
  • Loading branch information
neko12583 committed Oct 8, 2023
1 parent dbe5cff commit 0513473
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
10 changes: 7 additions & 3 deletions apps/backend/subscription/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,16 +1046,20 @@ def get_all_subscription_steps_context(
all_step_data[step.step_id] = step_context

plugin_path = get_plugin_path(plugin_name, target_host, agent_config)
# 当前step_id的数据单独拎出来,作为 shortcut

step_params = policy_step_adapter.get_matching_step_params(target_host.os_type.lower(), target_host.cpu_arch)
# 将 step.params 中 context 提取到第一层,提供给模板渲染
step_params = policy_step_adapter.get_matching_step_params(
target_host.os_type.lower(),
target_host.cpu_arch
)

context.update(step_params.get("context", {}))
context.update(all_step_data[subscription_step.step_id])
context.update(
cmdb_instance=instance_info,
step_data=all_step_data,
target=instance_info,
plugin_path=plugin_path,
control_info=all_step_data[subscription_step.step_id].get("control_info", {}),
nodeman={
"host": {
"bk_host_id": target_host.bk_host_id,
Expand Down
66 changes: 66 additions & 0 deletions apps/backend/tests/plugin/views/test_template_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
)
from apps.node_man import constants, models
from apps.utils.unittest.testcase import CustomAPITestCase
from apps.node_man.tests.utils import create_host
from apps.backend.subscription.tools import get_all_subscription_steps_context

PLATFORMS = ["linux_x86_64", "linux_x86", "windows_x86_64"]

Expand Down Expand Up @@ -343,6 +345,70 @@ def test_plugin_version__without_tmpl_case(self):
match_config_tmpl_version="1.2",
)

def test_get_all_subscription_steps_context(self):
"""测试获取渲染配置模板参数"""
# 创建主机数据
host_list, _, _ = create_host(
1,
bk_host_id=1,
bk_cloud_id=0,
ip="127.0.0.1",
os_type=constants.OsType.LINUX,
)
target_host = host_list[0]

# 创建订阅数据
subscription = models.Subscription.objects.create(
bk_biz_id=target_host.bk_biz_id,
object_type=models.Subscription.ObjectType.HOST,
node_type=models.Subscription.NodeType.TOPO,
nodes=[{"bk_host_id": target_host.bk_host_id}],
from_system="blueking",
enable=False,
creator="admin",
)
subscription_step = models.SubscriptionStep.objects.create(
subscription_id=subscription.id,
index=0,
step_id=common_unit.plugin.PLUGIN_NAME,
type="PLUGIN",
config={
"plugin_name": common_unit.plugin.PLUGIN_NAME,
"plugin_version": common_unit.plugin.PACKAGE_VERSION,
"config_templates": [{
"name": common_unit.plugin.GSE_PLUGIN_DESC_MODEL_DATA["config_file"],
"version": common_unit.plugin.PACKAGE_VERSION
}]
},
params={"context": {"params_context": "test"}},
)

# 创建插件相关数据数据
models.GsePluginDesc.objects.create(**common_unit.plugin.GSE_PLUGIN_DESC_MODEL_DATA)
models.Packages.objects.create(**common_unit.plugin.PACKAGES_MODEL_DATA)
models.ProcControl.objects.create(**common_unit.plugin.PROC_CONTROL_MODEL_DATA)

proc_status_model_data = deepcopy(common_unit.plugin.PROC_STATUS_MODEL_DATA)
proc_status_model_data["group_id"] = f"sub_{subscription.id}_host_{target_host.bk_host_id}"
proc_status_model_data["source_id"] = subscription.id
models.ProcessStatus.objects.create(**proc_status_model_data)

policy_step_adapter = PolicyStepAdapter(subscription_step)
context = get_all_subscription_steps_context(
subscription_step,
{"host": {"bk_host_id": target_host.bk_host_id}},
target_host,
common_unit.plugin.PLUGIN_NAME,
target_host.agent_config,
policy_step_adapter,
)

# 验证 context 中包含 control_info
self.assertTrue("control_info" in context.keys())

# 验证 step.params.context 处于 context 根节点下
self.assertTrue("params_context" in context.keys())

# 当前没有指定具体插件模板版本好的需求 有需要再打开

# def test_plugin_version__lower_tmpl_case(self):
Expand Down
18 changes: 18 additions & 0 deletions apps/mock_data/common_unit/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,21 @@
"health_cmd": f"./{PLUGIN_NAME} -z",
"debug_cmd": f"./{PLUGIN_NAME} -d",
}

PROC_STATUS_MODEL_DATA = {
"name": PLUGIN_NAME,
"status": "RUNNING",
"is_auto": "AUTO",
"version": PACKAGE_VERSION,
"proc_type": "PLUGIN",
"configs": [],
"setup_path": "/usr/local/gse/plugins/bin",
"log_path": "/var/log/gse",
"data_path": "/var/lib/gse",
"pid_path": f"/var/run/gse/{PLUGIN_NAME}.pid",
"group_id": "sub_1_host_1",
"source_type": "subscription",
"source_id": 1,
"bk_host_id": 1,
"is_latest": True,
}
6 changes: 5 additions & 1 deletion apps/node_man/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def create_host_from_a_to_b(
outer_ip=None,
login_ip=None,
proc_type=None,
os_type=None,
):
# 若传了bk_host_id,number必须为1
host_to_create = []
Expand All @@ -90,7 +91,7 @@ def create_host_from_a_to_b(
login_ip or f"{random.randint(1, 250)}.{random.randint(1, 250)}." f"{random.randint(1, 250)}.1"
),
"node_type": node_type or ["AGENT", "PAGENT", "PROXY"][random.randint(0, 2)],
"os_type": constants.OS_TUPLE[random.randint(0, 1)],
"os_type": os_type or constants.OS_TUPLE[random.randint(0, 1)],
"ap_id": 1,
"upstream_nodes": upstream_nodes if upstream_nodes else [i for i in range(100)],
"extra_data": {
Expand Down Expand Up @@ -143,6 +144,7 @@ def create_host(
outer_ip=None,
login_ip=None,
proc_type=None,
os_type=None,
):
# 若传了bk_host_id,number必须为1
host_to_create = []
Expand All @@ -164,6 +166,7 @@ def create_host(
outer_ip=outer_ip,
login_ip=login_ip,
proc_type=proc_type,
os_type=os_type,
)
else:
host, process, identity = create_host_from_a_to_b(
Expand All @@ -178,6 +181,7 @@ def create_host(
outer_ip=outer_ip,
login_ip=login_ip,
proc_type=proc_type,
os_type=os_type,
)
host_to_create.extend(host)
process_to_create.extend(process)
Expand Down

0 comments on commit 0513473

Please sign in to comment.