Skip to content

Commit

Permalink
feat: 提供 Agent 包管理后台基础接口 (closed #1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
ping15 committed Dec 11, 2024
1 parent 9736f99 commit 41dcfac
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
24 changes: 21 additions & 3 deletions apps/backend/subscription/steps/agent_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down Expand Up @@ -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 设置信息
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions apps/node_man/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,13 +1234,15 @@ 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]:
return {
cls.UNIFIED: _("统一的版本"),
cls.BY_HOST: _("按主机的"),
cls.BY_SYSTEM_ARCH: _("按系统架构"),
cls.BY_CLOUD_ID_AND_INNER_IP: _("按云区域id + 内网ip"),
}


Expand Down
2 changes: 1 addition & 1 deletion apps/node_man/handlers/gse_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 12 additions & 2 deletions apps/node_man/views/package_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit 41dcfac

Please sign in to comment.