Skip to content

Commit

Permalink
feat: 安装插件判断tag版本与业务设定最大版本(closed #2482)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpyoung3 committed Nov 4, 2024
1 parent 2d93b12 commit 698668e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
26 changes: 26 additions & 0 deletions apps/backend/components/collections/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from django.db.models import F, Q
from django.utils import timezone
from django.utils.translation import ugettext as _
from packaging import version

from apps.backend.api.constants import (
GSE_RUNNING_TASK_CODE,
Expand Down Expand Up @@ -280,6 +281,7 @@ def _execute(self, data, parent_data, common_data: PluginCommonData):
# target_host_objs 的长度通常为1或2,此处也不必担心时间复杂度问题
# 指定 target_host 主要用于远程采集的场景,常见于第三方插件,如拨测
for host in target_host_objs:
bk_biz_id = host.bk_biz_id
bk_host_id = host.bk_host_id
os_type = host.os_type.lower()
cpu_arch = host.cpu_arch
Expand All @@ -291,8 +293,24 @@ def _execute(self, data, parent_data, common_data: PluginCommonData):
)
# 如果版本号匹配到标签名称,取对应标签下的真实版本号
version_str = getattr(package, "version", "")
golbal_version_config = models.GlobalSettings.get_config(
models.GlobalSettings.KeyEnum.PLUGIN_VERSION_CONFIG.value
)
biz_version = None
if version_str in tag_name__obj_map:
version_str = tag_name__obj_map[package.version].target_version
if bk_biz_id in golbal_version_config:
biz_version_config = golbal_version_config[bk_biz_id]
for plugin_name, plugin_version in biz_version_config.items():
if plugin_name == package.project:
biz_version = plugin_version
if biz_version:
if not self.is_valid_version(version_str):
version_str = biz_version
else:
version_str_obj = version.Version(version_str)
biz_version_obj = version.Version(biz_version)
version_str = biz_version if version_str_obj > biz_version_obj else version_str
process_status_property = dict(
bk_host_id=bk_host_id,
name=plugin_name,
Expand Down Expand Up @@ -329,6 +347,14 @@ def _execute(self, data, parent_data, common_data: PluginCommonData):
batch_size=self.batch_size,
)

@staticmethod
def is_valid_version(version_str: str):
try:
version.Version(version_str)
return True # 如果没有异常,说明是有效的版本号
except version.InvalidVersion:
return False # 捕获到异常,说明不是有效的版本号

def inputs_format(self):
return self.inputs_format() + [
Service.InputItem(name="action", key="action", type="str", required=True),
Expand Down
18 changes: 11 additions & 7 deletions apps/backend/subscription/steps/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def max_ids_by_key(self, contained_os_cpu_items: List[Dict[str, Any]]) -> List[i
os_cpu__max_id_map[os_key] = item["id"]
return list(os_cpu__max_id_map.values())

def get_latest_package_ids(self, plugin_name: str, plugin_version: str):
def get_tag_package_ids(self, plugin_name: str, plugin_version: str):
# 先获取所有的 package
all_packages = models.Packages.objects.filter(project=plugin_name).values("id", "os", "cpu_arch", "version")
version_packages = {pkg["id"]: pkg for pkg in all_packages if pkg["version"] == plugin_version}
Expand All @@ -308,13 +308,17 @@ def format2policy_packages_new(
self, plugin_id: int, plugin_name: str, plugin_version: str, config_templates: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
latest_flag: str = "latest"
is_tag: bool = Tag.objects.filter(
stable_flag: str = "stable"
is_latest_tag: bool = Tag.objects.filter(
target_id=plugin_id, name=latest_flag, target_type=TargetType.PLUGIN.value
).exists()
is_stable_tag: bool = Tag.objects.filter(
target_id=plugin_id, name=stable_flag, target_type=TargetType.PLUGIN.value
).exists()

if plugin_version != latest_flag or is_tag:
if plugin_version != latest_flag or is_latest_tag or is_stable_tag:
# 如果 latest 是 tag,走取指定版本的逻辑
pkg_ids = self.get_latest_package_ids(plugin_name, plugin_version)
pkg_ids = self.get_tag_package_ids(plugin_name, plugin_version)
packages = models.Packages.objects.filter(id__in=pkg_ids)
else:
max_pkg_ids: List[int] = self.max_ids_by_key(
Expand All @@ -330,11 +334,11 @@ def format2policy_packages_new(
os_cpu__config_templates_map = defaultdict(list)
for template in config_templates:
is_main_template = template["is_main"]
if template["version"] != latest_flag or is_tag:
if template["version"] != latest_flag or is_latest_tag or is_stable_tag:
plugin_version_set = {plugin_version, "*"}
else:
latest_packages_version_set = set(packages.values_list("version", flat=True))
plugin_version_set = latest_packages_version_set | {"*"}
tag_packages_version_set = set(packages.values_list("version", flat=True))
plugin_version_set = tag_packages_version_set | {"*"}

max_config_tmpl_ids: typing.List[int] = self.max_ids_by_key(
list(
Expand Down
2 changes: 2 additions & 0 deletions apps/node_man/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ class KeyEnum(Enum):
AUTO_SELECT_INSTALL_CHANNEL_ONLY_DIRECT_AREA = "AUTO_SELECT_INSTALL_CHANNEL_ONLY_DIRECT_AREA"
# 安装通道ID与网段列表映射
INSTALL_CHANNEL_ID_NETWORK_SEGMENT = "INSTALL_CHANNEL_ID_NETWORK_SEGMENT"
# 业务指定插件版本
PLUGIN_VERSION_CONFIG = "PLUGIN_VERSION_CONFIG"

key = models.CharField(_("键"), max_length=255, db_index=True, primary_key=True)
v_json = JSONField(_("值"))
Expand Down

0 comments on commit 698668e

Please sign in to comment.