Skip to content

Commit

Permalink
Merge branch 'main' into v5.17.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangSetSail authored Feb 28, 2024
2 parents e87a3fc + 4a66a03 commit b00296d
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 14 deletions.
6 changes: 6 additions & 0 deletions console/services/app_config/app_relation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def get_service_dependencies(self, tenant, service):
services = service_repo.get_services_by_service_ids(dep_ids)
return services

def get_service_dependencies_reverse(self, service):
dep_ids = dep_relation_repo.get_service_reverse_dependencies(service.tenant_id, service.service_id).values_list(
"service_id", flat=True)
services = service_repo.get_services_by_service_ids(dep_ids)
return services

def get_undependencies(self, tenant, service):

# 打开对内端口才能被依赖
Expand Down
6 changes: 4 additions & 2 deletions console/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from console.views import app_upgrade
from console.views.app_autoscaler import (AppAutoscalerView, AppScalingRecords, ListAppAutoscalerView)
from console.views.app_config.app_dependency import (AppDependencyManageView, AppDependencyView, AppNotDependencyView,
AppDependencyReverseView)
AppDependencyReverseView, AppDependencyViewList)
from console.views.app_config.app_domain import (
AppServiceDomainQueryView, AppServiceTcpDomainQueryView, DomainQueryView, DomainView, GatewayCustomConfigurationView,
GetPortView, GetSeniorUrlView, HttpStrategyView, SecondLevelDomainView, ServiceDomainView, ServiceTcpDomainQueryView,
Expand Down Expand Up @@ -500,6 +500,8 @@
# 组件依赖
url(r'^teams/(?P<tenantName>[\w\-]+)/apps/(?P<serviceAlias>[\w\-]+)/dependency$', AppDependencyView.as_view(),
perms.AppDependencyView),
url(r'^teams/(?P<tenantName>[\w\-]+)/apps/(?P<serviceAlias>[\w\-]+)/dependency-list$', AppDependencyViewList.as_view(),
perms.AppDependencyView),
url(r'^teams/(?P<tenantName>[\w\-]+)/apps/(?P<serviceAlias>[\w\-]+)/dependency-reverse$',
AppDependencyReverseView.as_view(), perms.AppDependencyView),
url(r'^teams/(?P<tenantName>[\w\-]+)/apps/(?P<serviceAlias>[\w\-]+)/dependency/(?P<dep_service_id>[\w\-]+)$',
Expand Down Expand Up @@ -969,7 +971,7 @@
# 修改镜像源
url(r'^teams/(?P<tenantName>[\w\-]+)/apps/(?P<serviceAlias>[\w\-]+)/image', ImageAppView.as_view(), perms.ImageAppView),
# 查询构建源
url(r'^teams/(?P<tenantName>[\w\-]+)/apps/(?P<serviceAlias>[\w\-]+)/buildsource', BuildSourceinfo.as_view(),
url(r'^teams/(?P<tenantName>[\w\-]+)/apps/(?P<serviceAlias>[\w\-]+)/buildsource$', BuildSourceinfo.as_view(),
perms.BuildSourceinfo),
# job、cronjob策略配置
url(r'^teams/(?P<tenantName>[\w\-]+)/apps/(?P<serviceAlias>[\w\-]+)/job_strategy$', JobStrategy.as_view()),
Expand Down
48 changes: 45 additions & 3 deletions console/views/app_config/app_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ def get(self, request, *args, **kwargs):
un_dep_list.append(dep_service_info)
elif condition == "service_name" and search_key.lower() in un_dep.service_cname.lower():
un_dep_list.append(dep_service_info)
else:
result = general_message(400, "error", "condition参数错误")
return Response(result, status=400)

if search_key is not None and not condition:
if search_key.lower() in service_group_map[
un_dep.service_id]["group_name"].lower() or search_key.lower() in un_dep.service_cname.lower():
Expand Down Expand Up @@ -132,6 +130,50 @@ def post(self, request, *args, **kwargs):
return Response(result, status=result["code"])


class AppDependencyViewList(AppBaseView):
@never_cache
def get(self, request, *args, **kwargs):
page_num = int(request.GET.get("page", 1))
if page_num < 1:
page_num = 1
page_size = int(request.GET.get("page_size", 25))
dependencies = dependency_service.get_service_dependencies_reverse(self.service)
service_ids = [s.service_id for s in dependencies]
service_group_map = group_service.get_services_group_name(service_ids)
dep_list = []
for dep in dependencies:
tenant_service_ports = port_service.get_service_ports(dep)
ports_list = []
if tenant_service_ports:
for port in tenant_service_ports:
ports_list.append(port.container_port)
dep_service_info = {
"service_cname": dep.service_cname,
"service_id": dep.service_id,
"service_type": dep.service_type,
"service_alias": dep.service_alias,
"group_name": service_group_map[dep.service_id]["group_name"],
"group_id": service_group_map[dep.service_id]["group_id"],
"ports_list": ports_list
}
dep_list.append(dep_service_info)
start = (page_num - 1) * page_size
end = page_num * page_size
if start >= len(dep_list):
start = len(dep_list) - 1
end = len(dep_list) - 1
rt_list = dep_list[start:end]

service_ports = port_service.get_service_ports(self.service)
port_list = []
if service_ports:
for port in service_ports:
port_list.append(port.container_port)
bean = {"service_id": self.service.service_id, "port_list": port_list, 'total': len(dep_list)}
result = general_message(200, "success", "查询成功", list=rt_list, total=len(dep_list), bean=bean)
return Response(result, status=result["code"])


class AppDependencyView(AppBaseView):
@never_cache
def get(self, request, *args, **kwargs):
Expand Down
18 changes: 18 additions & 0 deletions openapi/serializer/app_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,21 @@ class DeployAppSerializer(serializers.Serializer):
action = serializers.CharField(max_length=255, required=True, help_text="操作类型,如:deploy, update")
group_key = serializers.CharField(max_length=128, required=False, help_text="生成模型的key")
group_version = serializers.CharField(max_length=128, required=False, help_text="生成模型的version")


class ChangeDeploySourceSerializer(serializers.Serializer):
image = serializers.CharField(max_length=512, required=True, help_text="镜像地址")


class ServiceVolumeSerializer(serializers.Serializer):
volume_name = serializers.CharField(max_length=512, required=True, help_text="名称")
volume_path = serializers.CharField(max_length=512, required=True, help_text="挂载路径")
volume_capacity = serializers.IntegerField(required=True, help_text="存储配额(GB)")
volume_type = serializers.CharField(max_length=512, required=True, help_text="储存类型")


class HelmChartSerializer(serializers.Serializer):
repo_name = serializers.CharField(max_length=512, required=True, help_text="helm 仓库名称")
chart_name = serializers.CharField(max_length=512, required=True, help_text="chart 包名称")
version = serializers.CharField(max_length=512, required=True, help_text="安装版本")
overrides = serializers.CharField(max_length=512, required=False, help_text="配置参数")
6 changes: 4 additions & 2 deletions openapi/sub_urls/app_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
AppServiceTelescopicHorizontalView, AppServiceTelescopicVerticalView, ComponentBuildView,
ComponentEnvsUView, CreateThirdComponentView, ListAppServicesView, TeamAppsCloseView,
TeamAppsMonitorQueryRangeView, TeamAppsMonitorQueryView, ComponentPortsShowView,
ComponentPortsChangeView)
ComponentPortsChangeView, ChangeDeploySourceView, ServiceVolumeView)
from openapi.views.apps.market import AppInstallView, AppUpgradeView
from openapi.views.gateway.gateway import (ListAppGatewayHTTPRuleView, ListAppGatewayRuleView, UpdateAppGatewayHTTPRuleView,
UpdateAppGatewayRuleView)
Expand Down Expand Up @@ -36,8 +36,10 @@
url(r'^(?P<app_id>[\d\-]+)/services/(?P<service_id>[\w\-]+)/telescopic/horizontal$',
AppServiceTelescopicHorizontalView.as_view(), perms.AppServiceTelescopicHorizontalView),
url(r'^(?P<app_id>[\d\-]+)/services/(?P<service_id>[\w\-]+)/envs$', ComponentEnvsUView.as_view()),
url(r'^(?P<app_id>[\d\-]+)/services/(?P<service_id>[\w\-]+)/ports/(?P<port>[\w\-]+)$$', ComponentPortsChangeView.as_view()),
url(r'^(?P<app_id>[\d\-]+)/services/(?P<service_id>[\w\-]+)/ports/(?P<port>[\w\-]+)$', ComponentPortsChangeView.as_view()),
url(r'^(?P<app_id>[\d\-]+)/services/(?P<service_id>[\w\-]+)/ports$', ComponentPortsShowView.as_view()),
url(r'^(?P<app_id>[\d\-]+)/services/(?P<service_id>[\w\-]+)/build$', ComponentBuildView.as_view()),
url(r'^(?P<app_id>[\d\-]+)/services/(?P<service_id>[\w\-]+)/docker-image-change$', ChangeDeploySourceView.as_view()),
url(r'^(?P<app_id>[\d\-]+)/services/(?P<service_id>[\w\-]+)/volumes$', ServiceVolumeView.as_view()),
url(r'^(?P<app_id>[\d\-]+)/third-components$', CreateThirdComponentView.as_view()),
]
4 changes: 3 additions & 1 deletion openapi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from openapi.auth.permissions import OpenAPIPermissions
from openapi.views.admin_view import AdminInfoView, ListAdminsView
from openapi.views.apps.apps import ListAppsView, AppModelImportEvent, AppTarballDirView, \
AppImportView, AppDeployView, AppChartInfo, DeleteApp, AppsPortView
AppImportView, AppDeployView, AppChartInfo, DeleteApp, AppsPortView, HelmChart
from openapi.views.enterprise_view import EnterpriseConfigView
from openapi.views.gateway.gateway import ListEnterpriseAppGatewayHTTPRuleView
from openapi.views.region_view import ListRegionInfo, RegionInfo, ReplaceRegionIP
Expand Down Expand Up @@ -86,6 +86,8 @@
# 删除应用及所有资源
url(r'^v1/teams/(?P<team_id>[\w\-]+)/regions/(?P<region_name>[\w\-]+)/app/(?P<app_id>[\w\-]+)/delete$',
DeleteApp.as_view()),
url(r'^v1/teams/(?P<team_id>[\w\-]+)/regions/(?P<region_name>[\w\-]+)/app/(?P<app_id>[\w\-]+)/helm_chart$',
HelmChart.as_view()),
]
if os.environ.get("OPENAPI_V2") == "true":
urlpatterns += [url(r'^v2', include('openapi.v2.urls'))]
156 changes: 154 additions & 2 deletions openapi/views/apps/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import os
import pickle
import re
import time

from django.db import transaction
Expand Down Expand Up @@ -39,13 +40,15 @@
from django.forms.models import model_to_dict
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema

from console.views.app_config.app_volume import ensure_volume_mode
from openapi.serializer.app_serializer import (
AppBaseInfoSerializer, AppInfoSerializer, AppPostInfoSerializer, AppServiceEventsSerializer,
AppServiceTelescopicHorizontalSerializer, AppServiceTelescopicVerticalSerializer, ComponentBuildReqSerializers,
ComponentEnvsSerializers, ComponentEventSerializers, ComponentMonitorSerializers, CreateThirdComponentResponseSerializer,
CreateThirdComponentSerializer, ListServiceEventsResponse, ServiceBaseInfoSerializer, ServiceGroupOperationsSerializer,
TeamAppsCloseSerializers, DeployAppSerializer, ServicePortSerializer, ComponentPortReqSerializers,
ComponentUpdatePortReqSerializers)
ComponentUpdatePortReqSerializers, ChangeDeploySourceSerializer, ServiceVolumeSerializer, HelmChartSerializer)
from openapi.serializer.base_serializer import (FailSerializer, SuccessSerializer)
from openapi.services.app_service import app_service
from openapi.services.component_action import component_action_service
Expand All @@ -54,7 +57,7 @@
from rest_framework import status
from rest_framework.response import Response
from www.apiclient.regionapi import RegionInvokeApi
from www.utils.crypt import make_uuid
from www.utils.crypt import make_uuid, make_uuid3
from www.utils.return_message import general_message, error_message

region_api = RegionInvokeApi()
Expand Down Expand Up @@ -791,6 +794,88 @@ def post(self, request, *args, **kwargs):
return Response(result, status=status.HTTP_200_OK)


class ServiceVolumeView(TeamAppServiceAPIView):
@swagger_auto_schema(
operation_description="挂载组件的储存",
manual_parameters=[
openapi.Parameter("team_id", openapi.IN_PATH, description="团队ID、名称", type=openapi.TYPE_STRING),
openapi.Parameter("region_name", openapi.IN_PATH, description="数据中心名称", type=openapi.TYPE_STRING),
openapi.Parameter("app_id", openapi.IN_PATH, description="应用组id", type=openapi.TYPE_INTEGER),
openapi.Parameter("service_id", openapi.IN_PATH, description="组件ID", type=openapi.TYPE_STRING)
],
request_body=ServiceVolumeSerializer,
tags=['openapi-apps'],
)
def post(self, request, *args, **kwargs):

ServiceVolumeSerializerRequest = ServiceVolumeSerializer(data=request.data)
ServiceVolumeSerializerRequest.is_valid(raise_exception=True)

req = ServiceVolumeSerializerRequest.data
r = re.compile('(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$')
if not r.match(req.get("volume_name")):
raise AbortRequest(msg="volume name illegal", msg_show="持久化名称只支持数字字母下划线")

file_content = request.data.get("file_content", None)
provider_name = request.data.get("volume_provider_name", '')
access_mode = request.data.get("access_mode", '')
share_policy = request.data.get('share_policy', '')
backup_policy = request.data.get('back_policy', '')
reclaim_policy = request.data.get('reclaim_policy', '')
allow_expansion = request.data.get('allow_expansion', False)
mode = request.data.get("mode")
if mode is not None:
mode = ensure_volume_mode(mode)

settings = {}
settings['volume_capacity'] = req.get("volume_capacity")
settings['provider_name'] = provider_name
settings['access_mode'] = access_mode
settings['share_policy'] = share_policy
settings['backup_policy'] = backup_policy
settings['reclaim_policy'] = reclaim_policy
settings['allow_expansion'] = allow_expansion

data = volume_service.add_service_volume(
self.team,
self.service,
req.get("volume_path"),
req.get("volume_type"),
req.get("volume_name"),
file_content,
settings,
self.user.nick_name,
mode=mode)
result = general_message(200, "success", "持久化路径添加成功", bean=data.to_dict())

return Response(result, status=result["code"])


class ChangeDeploySourceView(TeamAppServiceAPIView):
@swagger_auto_schema(
operation_description="更改docker构建方式的镜像地址",
manual_parameters=[
openapi.Parameter("team_id", openapi.IN_PATH, description="团队ID、名称", type=openapi.TYPE_STRING),
openapi.Parameter("region_name", openapi.IN_PATH, description="数据中心名称", type=openapi.TYPE_STRING),
openapi.Parameter("app_id", openapi.IN_PATH, description="应用组id", type=openapi.TYPE_INTEGER),
openapi.Parameter("service_id", openapi.IN_PATH, description="组件ID", type=openapi.TYPE_STRING)
],
request_body=ChangeDeploySourceSerializer,
tags=['openapi-apps'],
)
def put(self, request, *args, **kwargs):
image = request.data.get("image", None)
if image:
version = image.split(':')[-1]
if not version:
version = "latest"
image = image + ":" + version
self.service.image = image
self.service.version = version
self.service.save()
return Response(general_message(200, "success", "更改镜像地址成功"), status=200)


class ComponentBuildView(TeamAppServiceAPIView):
@swagger_auto_schema(
operation_description="构建组件,用于CI/CD工作流调用",
Expand Down Expand Up @@ -1205,3 +1290,70 @@ def delete(self, request, app_id, *args, **kwargs):
group_service.delete_app(self.team, self.region_name, app)
result = general_message(200, "success", "删除成功")
return Response(result, status=result["code"])


class HelmChart(TeamAPIView):
@swagger_auto_schema(
operation_description="安装helm应用",
manual_parameters=[
openapi.Parameter("team_id", openapi.IN_PATH, description="团队ID、名称", type=openapi.TYPE_STRING),
openapi.Parameter("app_id", openapi.IN_PATH, description="应用id", type=openapi.TYPE_INTEGER),
openapi.Parameter("region_name", openapi.IN_PATH, description="集群名称", type=openapi.TYPE_STRING),
],
request_body=HelmChartSerializer,
tags=['openapi-apps'],
)
def get(self, request, app_id, *args, **kwargs):
chart_info = HelmChartSerializer(data=request.GET)
chart_info.is_valid(raise_exception=True)
name = chart_info.data.get("chart_name")
repo_name = chart_info.data.get("repo_name")
chart_name = chart_info.data.get("chart_name")
version = chart_info.data.get("version")
overrides = []
print(name, chart_name, repo_name, version)
_, data = helm_app_service.check_helm_app(name, repo_name, chart_name, version, overrides, self.region_name,
self.team.tenant_name, self.team)
result = general_message(200, "success", "获取成功", bean=data)
return Response(result, status=result["code"])

def post(self, request, app_id, *args, **kwargs):
chart_info = HelmChartSerializer(data=request.data)
chart_info.is_valid(raise_exception=True)
repo_name = chart_info.data.get("repo_name")
overrides = chart_info.data.get("overrides", "")
version = chart_info.data.get("version")
chart_name = chart_info.data.get("chart_name")
name = chart_name
app_model_id = make_uuid3(repo_name + "/" + chart_name)
helm_center_app = rainbond_app_repo.get_rainbond_app_qs_by_key(self.enterprise.enterprise_id, app_model_id)
data = {"exist": True, "app_model_id": app_id}
if not helm_center_app:
center_app = {
"app_id": app_id,
"app_name": chart_name,
"create_team": "",
"source": "helm:" + repo_name,
"scope": "enterprise",
"pic": "",
"describe": "",
"enterprise_id": self.enterprise.enterprise_id,
"details": "",
}
helm_app_service.create_helm_center_app(center_app, self.region_name)
data["exist"] = False
data["app_model_id"] = app_model_id
overrides_list = overrides.split(",")
cvdata = helm_app_service.yaml_conversion(name, repo_name, chart_name, version, overrides_list, self.region_name,
self.team.tenant_name, self.team, self.enterprise.enterprise_id,
self.region.region_id)
helm_center_app = rainbond_app_repo.get_rainbond_app_qs_by_key(self.enterprise.enterprise_id, app_model_id)
chart = repo_name + "/" + chart_name
helm_app_service.generate_template(cvdata, helm_center_app, version, self.team, chart, self.region_name,
self.enterprise.enterprise_id, self.user.user_id, overrides_list, app_id)

market_app_service.install_app(self.team, self.region, self.user, app_id, app_model_id, version, "localApplication",
False, True, False)

result = general_message(200, "success", "成功")
return Response(result, status=result["code"])
7 changes: 3 additions & 4 deletions www/apiclient/regionapibaseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,11 @@ def _unpack(self, dict_body):
return dict()

def get_default_timeout_conifg(self):
connect, red = 2.0, 5.0
try:
connect = float(os.environ.get("REGION_CONNECTION_TIMEOUT", 2.0))
red = float(os.environ.get("REGION_RED_TIMEOUT", 5.0))
connect = float(os.environ.get("REGION_CONNECTION_TIMEOUT", 5.0))
red = float(os.environ.get("REGION_RED_TIMEOUT", 30.0))
except Exception:
connect, red = 2.0, 5.0
connect, red = 5.0, 30.0
return connect, red

def _request(self, url, method, headers=None, body=None, *args, **kwargs):
Expand Down

0 comments on commit b00296d

Please sign in to comment.