Skip to content

Commit

Permalink
perf: helm install (#1386)
Browse files Browse the repository at this point in the history
* feat: helm only one click install

* feat: helm only one click install

* feat: helm only one click install

* feat: helm only one click install

* feat: helm only one click install

* fix: pass CI check

* fix: pass CI check
  • Loading branch information
DokiDoki1103 authored Aug 23, 2023
1 parent dc96b87 commit c7ccf40
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
26 changes: 16 additions & 10 deletions console/repositories/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@ def create_helm_repo(self, **params):
return HelmRepoInfo.objects.create(**params)

def delete_helm_repo(self, repo_name):
perms = HelmRepoInfo.objects.filter(repo_name=repo_name)
if not perms:
data = HelmRepoInfo.objects.filter(repo_name=repo_name)
if not data:
return None
return perms.delete()
return data.delete()

def get_helm_repo_by_name(self, repo_name):
perms = HelmRepoInfo.objects.filter(repo_name=repo_name)
if not perms:
data = HelmRepoInfo.objects.filter(repo_name=repo_name)
if not data:
return None
return perms[0].to_dict()
return data[0].to_dict()

def get_helm_repo_by_url(self, url):
data = HelmRepoInfo.objects.filter(repo_url=url)
if not data:
return None
return data[0].to_dict()

def update_helm_repo(self, repo_name, repo_url):
perms = HelmRepoInfo.objects.filter(repo_name=repo_name)
if not perms:
data = HelmRepoInfo.objects.filter(repo_name=repo_name)
if not data:
return None
perms[0].repo_url = repo_url
perms[0].save()
data[0].repo_url = repo_url
data[0].save()


helm_repo = HelmRepo()
24 changes: 24 additions & 0 deletions console/services/helm_app_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,30 @@ def get_helm_chart_information(self, region, tenant_name, repo_url, chart_name):
_, body = region_api.get_chart_information(region, tenant_name, repo_chart)
return body["bean"]

def parse_cmd_add_repo(self, command):

repo_add_pattern = r'^helm\s+repo\s+add\s+(?P<repo_name>\S+)\s+(?P<repo_url>\S+)(?:\s+--username\s+' \
r'(?P<username>\S+))?(?:\s+--password\s+(?P<password>\S+))?$'
repo_add_match = re.match(repo_add_pattern, command)
if not repo_add_match:
raise AbortRequest("helm repo is exist", "命令解析错误,请重新输入", status_code=400, error_code=400)
repo_name = repo_add_match.group('repo_name')
repo_url = repo_add_match.group('repo_url')
username = repo_add_match.group('username') if repo_add_match.group('username') else ""
password = repo_add_match.group('password') if repo_add_match.group('password') else ""
repo = helm_repo.get_helm_repo_by_name(repo_name)
if not repo:
logger.info("create helm repo {}".format(repo_name))
self.add_helm_repo(repo_name, repo_url, username, password)
return repo_name, repo_url, username, password, True
else:
# 有一种情况,仓库名被占用了,但是url不同。
repo = helm_repo.get_helm_repo_by_url(repo_url)
if repo:
return repo_name, repo_url, username, password, False
else:
raise AbortRequest("helm repo is exist", "仓库名称已被占用,请更改仓库名称", status_code=409, error_code=409)

def parse_helm_command(self, command, region_name, tenant):
result = dict()
repo_add_pattern = r'^helm\s+repo\s+add\s+(?P<repo_name>\S+)\s+(?P<repo_url>\S+)(?:\s+--username\s+' \
Expand Down
5 changes: 4 additions & 1 deletion console/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
TenantGroupOperationView, TenantGroupView, ApplicationInstallView, ApplicationPodView, ApplicationHelmAppComponentView,
ApplicationParseServicesView, ApplicationReleasesView, ApplicationIngressesView, TenantAppUpgradableNumView,
AppGovernanceModeCheckView, ApplicationVolumesView, AppGovernanceModeCRView, TenantGroupHandleView)
from console.views.helm_app import HelmAppView, HelmRepo, HelmCenterApp, HelmChart, CommandInstallHelm
from console.views.helm_app import HelmAppView, HelmRepo, HelmCenterApp, HelmChart, CommandInstallHelm, HelmList, \
HelmRepoAdd
from console.views.jwt_token_view import JWTTokenView
from console.views.k8s_attribute import ComponentK8sAttributeView, ComponentK8sAttributeListView
from console.views.k8s_resource import AppK8sResourceListView, AppK8ResourceView
Expand Down Expand Up @@ -254,6 +255,8 @@
url(r'^teams/(?P<team_name>[\w\-]+)/helm_app$', HelmAppView.as_view()),
url(r'^teams/(?P<team_name>[\w\-]+)/chart/version$', HelmChart.as_view()),
url(r'^teams/(?P<team_name>[\w\-]+)/helm_command$', CommandInstallHelm.as_view()),
url(r'^teams/(?P<team_name>[\w\-]+)/helm_list$', HelmList.as_view()),
url(r'^teams/(?P<team_name>[\w\-]+)/helm_cmd_add$', HelmRepoAdd.as_view()),
url(r'^teams/(?P<team_name>[\w\-]+)/helm_center_app$', HelmCenterApp.as_view()),

# 获取生成令牌
Expand Down
27 changes: 26 additions & 1 deletion console/views/helm_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from console.repositories.share_repo import share_repo
from console.services.helm_app_yaml import helm_app_service
from console.views.base import RegionTenantHeaderView, JWTAuthApiView
from www.models.main import HelmRepoInfo
from www.utils.crypt import make_uuid3
from www.utils.return_message import general_message
from rest_framework.response import Response
Expand Down Expand Up @@ -134,6 +135,30 @@ def post(self, request, *args, **kwargs):
return Response(result, status=status.HTTP_200_OK)


class HelmList(RegionTenantHeaderView):
def get(self, request, *args, **kwargs):
"""
查询本地已经有的helm商店
"""
data = HelmRepoInfo.objects.all().values()
result = general_message(200, "success", "查询成功", list=data)
return Response(result, status=status.HTTP_200_OK)


class HelmRepoAdd(RegionTenantHeaderView):
def post(self, request, *args, **kwargs):
"""
根据cmd 命令行 增加helm仓库
"""
command = request.data.get("command")

repo_name, repo_url, username, password, success = helm_app_service.parse_cmd_add_repo(command)
data = {"status": success, "repo_name": repo_name, "repo_url": repo_url, "username": username, "password": password}
result = general_message(200, "success", "添加成功", bean=data)

return Response(result, status=status.HTTP_200_OK)


class HelmRepo(JWTAuthApiView):
def post(self, request, *args, **kwargs):
"""
Expand All @@ -144,7 +169,7 @@ def post(self, request, *args, **kwargs):
username = request.data.get("username", "")
password = request.data.get("password", "")
if helm_repo.get_helm_repo_by_name(repo_name):
result = general_message(200, "success", "添加成功", "")
result = general_message(200, "success", "仓库已存在", "")
return Response(result, status=status.HTTP_200_OK)
helm_app_service.add_helm_repo(repo_name, repo_url, username, password)
result = general_message(200, "success", "添加成功", "")
Expand Down

0 comments on commit c7ccf40

Please sign in to comment.