From 41dcface16301db81377f5d56f7234fbc5505cc1 Mon Sep 17 00:00:00 2001 From: gqp <446105468@qq.com> Date: Mon, 9 Dec 2024 17:06:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8F=90=E4=BE=9B=20Agent=20=E5=8C=85?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=90=8E=E5=8F=B0=E5=9F=BA=E7=A1=80=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=20(closed=20#1683)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../steps/agent_adapter/adapter.py | 24 ++++++++++++++++--- apps/node_man/constants.py | 2 ++ apps/node_man/handlers/gse_package.py | 2 +- apps/node_man/views/package_manage.py | 14 +++++++++-- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/apps/backend/subscription/steps/agent_adapter/adapter.py b/apps/backend/subscription/steps/agent_adapter/adapter.py index dfdafe305..4e6b6919d 100644 --- a/apps/backend/subscription/steps/agent_adapter/adapter.py +++ b/apps/backend/subscription/steps/agent_adapter/adapter.py @@ -38,6 +38,8 @@ class AgentVersionSerializer(serializers.Serializer): os_cpu_arch = serializers.CharField(label="系统CPU架构", required=False) bk_host_id = serializers.IntegerField(label="主机ID", required=False) + bk_cloud_id = serializers.IntegerField(label="云区域id", required=False) + inner_ip = serializers.IPAddressField(label="内网ip", required=False) version = serializers.CharField(label="Agent Version") @@ -189,6 +191,14 @@ def get_config( def bk_host_id_version_map(self) -> typing.Dict[int, str]: return {versiom_map["bk_host_id"]: versiom_map["version"] for versiom_map in self.config["version_map_list"]} + @property + @cache.class_member_cache() + def bk_cloud_and_inner_ip__version_map(self) -> typing.Dict[str, str]: + return { + f"{version_map['bk_cloud_id']}:{version_map['inner_ip']}": version_map["version"] + for version_map in self.config["version_map_list"] + } + def get_host_setup_info(self, host: models.Host) -> base.AgentSetupInfo: """ 获取 Agent 设置信息 @@ -218,9 +228,11 @@ def get_host_setup_info(self, host: models.Host) -> base.AgentSetupInfo: f"agent_name:{self.agent_name}:type:{constants.AgentVersionType.BY_SYSTEM_ARCH.value}:" f"os:{host.os_type.lower()}:version:{agent_version}" ) - else: + elif self.config["choice_version_type"] == constants.AgentVersionType.BY_HOST.value: # 按主机维度 agent_version: str = self.bk_host_id_version_map[host.bk_host_id] + else: + agent_version = self.bk_cloud_and_inner_ip__version_map[f"{host.bk_cloud_id}:{host.inner_ip}"] target_version_cache_key: str = f"agent_desc_id:{self.agent_desc.id}:agent_version:{agent_version}" target_version: str = self._target_version_cache.get(target_version_cache_key) @@ -231,7 +243,10 @@ def get_host_setup_info(self, host: models.Host) -> base.AgentSetupInfo: ) self._target_version_cache[target_version_cache_key] = target_version - if self.config["choice_version_type"] != constants.AgentVersionType.BY_HOST.value: + if self.config["choice_version_type"] not in [ + constants.AgentVersionType.BY_HOST.value, + constants.AgentVersionType.BY_CLOUD_ID_AND_INNER_IP.value, + ]: agent_setup_info: typing.Optional[base.AgentSetupInfo] = self._setup_info_cache.get(setup_info_cache_key) if agent_setup_info: return agent_setup_info @@ -242,7 +257,10 @@ def get_host_setup_info(self, host: models.Host) -> base.AgentSetupInfo: name=self.config.get("name"), version=target_version, ) - if self.config["choice_version_type"] != constants.AgentVersionType.BY_HOST.value: + if self.config["choice_version_type"] not in [ + constants.AgentVersionType.BY_HOST.value, + constants.AgentVersionType.BY_CLOUD_ID_AND_INNER_IP.value, + ]: self._setup_info_cache[setup_info_cache_key] = agent_setup_info return agent_setup_info diff --git a/apps/node_man/constants.py b/apps/node_man/constants.py index 9ad98f95f..bc7809d3e 100644 --- a/apps/node_man/constants.py +++ b/apps/node_man/constants.py @@ -1234,6 +1234,7 @@ class AgentVersionType(EnhanceEnum): UNIFIED = "unified" BY_HOST = "by_host" BY_SYSTEM_ARCH = "by_system_arch" + BY_CLOUD_ID_AND_INNER_IP = "by_cloud_id_and_inner_ip" @classmethod def _get_member__alias_map(cls) -> Dict[Enum, str]: @@ -1241,6 +1242,7 @@ def _get_member__alias_map(cls) -> Dict[Enum, str]: cls.UNIFIED: _("统一的版本"), cls.BY_HOST: _("按主机的"), cls.BY_SYSTEM_ARCH: _("按系统架构"), + cls.BY_CLOUD_ID_AND_INNER_IP: _("按云区域id + 内网ip"), } diff --git a/apps/node_man/handlers/gse_package.py b/apps/node_man/handlers/gse_package.py index 8a4cd51f1..2f74d8dbb 100644 --- a/apps/node_man/handlers/gse_package.py +++ b/apps/node_man/handlers/gse_package.py @@ -155,7 +155,7 @@ def filter_tags(cls, queryset: QuerySet, project: str, tag_names: List[str] = No combined_tag_names_conditions: Q = Q() for tag_name in tag_names or []: - combined_tag_names_conditions |= Q(name__contains=tag_name) + combined_tag_names_conditions |= Q(name=tag_name) filter_conditions: Q = Q(target_id=project__id_map.get(project)) & combined_tag_names_conditions diff --git a/apps/node_man/views/package_manage.py b/apps/node_man/views/package_manage.py index bd70240f6..759b97d90 100644 --- a/apps/node_man/views/package_manage.py +++ b/apps/node_man/views/package_manage.py @@ -89,7 +89,7 @@ class PackageManageFilterClass(FilterSet): os_cpu_arch = django_filters.BaseInFilter(field_name="os_cpu_arch", method="filter_os_cpu_arch") tag_names = django_filters.BaseInFilter(lookup_expr="in", method="filter_tag_names") created_by = django_filters.BaseInFilter(field_name="created_by", lookup_expr="in") - is_ready = django_filters.BooleanFilter(field_name="is_ready") + is_ready = django_filters.CharFilter(field_name="is_ready", method="filter_is_ready") version = django_filters.BaseInFilter(field_name="version", lookup_expr="in") created_time = django_filters.DateTimeFromToRangeFilter() condition = django_filters.Filter(method="filter_condition") @@ -99,7 +99,8 @@ def filter_tag_names(self, queryset, name, tag_names): raise ValidationError(_("筛选tag_names时必须传入project")) return gse_package_handler.filter_tags(queryset, self.request.data["project"], tag_names=tag_names) - def filter_os_cpu_arch(self, queryset, name, os_cpu_archs): + @staticmethod + def filter_os_cpu_arch(queryset, name, os_cpu_archs): package_query = Q() for os_cpu_arch in os_cpu_archs: try: @@ -137,6 +138,15 @@ def filter_condition(self, queryset, name, query_list): return queryset.filter(model_field_query | tag_query) + @staticmethod + def filter_is_ready(queryset, name, is_ready): + if is_ready in (True, "True", "true", "1"): + return queryset.filter(is_ready=True) + elif is_ready in (False, "False", "false", "0"): + return queryset.filter(is_ready=False) + else: + return queryset.none() + class Meta: model = GsePackages fields = ["tag_names", "project", "created_by", "is_ready", "version", "os", "cpu_arch"]