From c98d9eafc6456dcdba02f3bf96048068ddc904e6 Mon Sep 17 00:00:00 2001 From: wklken Date: Mon, 2 Dec 2024 17:38:25 +0800 Subject: [PATCH] feat(components): use gateway of bkmonito/bklog, disable esb when ENABLE_MULTI_TENANT_MODE --- .../apigateway/apigateway/apis/open/urls.py | 51 ++++++++++--------- .../apigateway/apis/web/docs/urls.py | 21 +++++--- .../apigateway/components/bk_log.py | 21 ++++---- .../components/bkapi_client/bkmonitorv3.py | 8 +++ .../components/bkapi_client/log_search.py | 8 +++ .../apigateway/components/prometheus.py | 14 ++--- .../apigateway/apigateway/conf/celery_conf.py | 10 ++-- .../apigateway/apigateway/conf/default.py | 27 ++++++---- src/dashboard/apigateway/apigateway/urls.py | 9 +++- 9 files changed, 103 insertions(+), 66 deletions(-) diff --git a/src/dashboard/apigateway/apigateway/apis/open/urls.py b/src/dashboard/apigateway/apigateway/apis/open/urls.py index d05088dd9..24205f278 100644 --- a/src/dashboard/apigateway/apigateway/apis/open/urls.py +++ b/src/dashboard/apigateway/apigateway/apis/open/urls.py @@ -16,6 +16,7 @@ # We undertake not to change the open source license (MIT license) applicable # to the current version of the project delivered to anyone in the future. # +from django.conf import settings from django.urls import include, path from apigateway.apis.open.esb.permission import views as esb_permission_views @@ -87,27 +88,29 @@ path("", include("apigateway.apis.open.monitor.urls")), ] -urlpatterns += [ - path("esb/systems/", include("apigateway.apis.open.esb.system.urls")), - path("esb/systems//permissions/", include("apigateway.apis.open.esb.permission.urls")), - path( - "esb/systems/permissions/renew/", - esb_permission_views.AppPermissionRenewAPIView.as_view({"post": "renew"}), - name="openapi.esb.permission.renew", - ), - path( - "esb/systems/permissions/app-permissions/", - esb_permission_views.AppPermissionViewSet.as_view({"get": "list"}), - name="openapi.esb.permission.app-permissions", - ), - path( - "esb/systems/permissions/apply-records/", - esb_permission_views.AppPermissionApplyRecordViewSet.as_view({"get": "list"}), - name="openapi.esb.permission.app-records", - ), - path( - "esb/systems/permissions/apply-records//", - esb_permission_views.AppPermissionApplyRecordViewSet.as_view({"get": "retrieve"}), - name="openapi.esb.permission.app-record-detail", - ), -] +# 非多租户模式才会有 esb 相关的接口 +if not settings.ENABLE_MULTI_TENANT_MODE: + urlpatterns += [ + path("esb/systems/", include("apigateway.apis.open.esb.system.urls")), + path("esb/systems//permissions/", include("apigateway.apis.open.esb.permission.urls")), + path( + "esb/systems/permissions/renew/", + esb_permission_views.AppPermissionRenewAPIView.as_view({"post": "renew"}), + name="openapi.esb.permission.renew", + ), + path( + "esb/systems/permissions/app-permissions/", + esb_permission_views.AppPermissionViewSet.as_view({"get": "list"}), + name="openapi.esb.permission.app-permissions", + ), + path( + "esb/systems/permissions/apply-records/", + esb_permission_views.AppPermissionApplyRecordViewSet.as_view({"get": "list"}), + name="openapi.esb.permission.app-records", + ), + path( + "esb/systems/permissions/apply-records//", + esb_permission_views.AppPermissionApplyRecordViewSet.as_view({"get": "retrieve"}), + name="openapi.esb.permission.app-record-detail", + ), + ] diff --git a/src/dashboard/apigateway/apigateway/apis/web/docs/urls.py b/src/dashboard/apigateway/apigateway/apis/web/docs/urls.py index 20e1e9473..b7af56d2f 100644 --- a/src/dashboard/apigateway/apigateway/apis/web/docs/urls.py +++ b/src/dashboard/apigateway/apigateway/apis/web/docs/urls.py @@ -15,6 +15,7 @@ # We undertake not to change the open source license (MIT license) applicable # to the current version of the project delivered to anyone in the future. # +from django.conf import settings from django.urls import include, path urlpatterns = [ @@ -26,11 +27,17 @@ path("gateways//resources/", include("apigateway.apis.web.docs.gateway.resource.urls")), path("gateways//stages/", include("apigateway.apis.web.docs.gateway.stage.urls")), path("gateways//sdks/", include("apigateway.apis.web.docs.gateway.gateway_sdk.urls")), - # esb - path("esb/boards//systems/", include("apigateway.apis.web.docs.esb.system.urls")), - path( - "esb/boards//systems//components/", - include("apigateway.apis.web.docs.esb.component.urls"), - ), - path("esb/boards//sdks/", include("apigateway.apis.web.docs.esb.sdk.urls")), ] + + +# 非多租户模式才会有 esb 相关的接口 +if not settings.ENABLE_MULTI_TENANT_MODE: + urlpatterns += [ + # esb + path("esb/boards//systems/", include("apigateway.apis.web.docs.esb.system.urls")), + path( + "esb/boards//systems//components/", + include("apigateway.apis.web.docs.esb.component.urls"), + ), + path("esb/boards//sdks/", include("apigateway.apis.web.docs.esb.sdk.urls")), + ] diff --git a/src/dashboard/apigateway/apigateway/components/bk_log.py b/src/dashboard/apigateway/apigateway/components/bk_log.py index 5d9621eba..4758507b7 100644 --- a/src/dashboard/apigateway/apigateway/components/bk_log.py +++ b/src/dashboard/apigateway/apigateway/components/bk_log.py @@ -23,8 +23,7 @@ from bkapi_client_core.apigateway.django_helper import get_client_by_username as get_client_by_username_for_apigateway from django.conf import settings -from apigateway.components.bkapi_client.log_search import Client as LogSearchClient -from apigateway.components.esb_components import get_client_by_username as get_client_by_username_for_esb +from apigateway.components.bkapi_client.log_search import new_client_cls from apigateway.components.handler import RequestAPIHandler from apigateway.components.utils import inject_accept_language @@ -46,15 +45,15 @@ def esquery_dsl(self, index: str, body: Any) -> Dict[str, Any]: return self._request_handler.parse_api_result(api_result, response, {"result": True}, itemgetter("data")) def _get_api_client(self) -> OperationGroup: - # use gateway: log-search - if settings.USE_BKAPI_BK_LOG: - apigw_client = get_client_by_username_for_apigateway(LogSearchClient, username="admin") - apigw_client.session.register_hook("request", inject_accept_language) - return apigw_client.api - - esb_client = get_client_by_username_for_esb("admin") - esb_client.session.register_hook("request", inject_accept_language) - return esb_client.bk_log + # use gateway: log-search(te) / bk-log-search(ee) + gateway_name = "bk-log-search" + if settings.EDITION == "te": + gateway_name = "log-search" + + client_cls = new_client_cls(gateway_name) + apigw_client = get_client_by_username_for_apigateway(client_cls, username="admin") + apigw_client.session.register_hook("request", inject_accept_language) + return apigw_client.api bk_log_component = BKLogComponent() diff --git a/src/dashboard/apigateway/apigateway/components/bkapi_client/bkmonitorv3.py b/src/dashboard/apigateway/apigateway/components/bkapi_client/bkmonitorv3.py index 42e6a4730..94864754c 100644 --- a/src/dashboard/apigateway/apigateway/components/bkapi_client/bkmonitorv3.py +++ b/src/dashboard/apigateway/apigateway/components/bkapi_client/bkmonitorv3.py @@ -34,3 +34,11 @@ class Client(APIGatewayClient): _api_name = "bkmonitorv3" api = bind_property(Group, name="api") + + +def new_client_cls(api_name: str): + class Client(APIGatewayClient): + _api_name = api_name + api = bind_property(Group, name="api") + + return Client diff --git a/src/dashboard/apigateway/apigateway/components/bkapi_client/log_search.py b/src/dashboard/apigateway/apigateway/components/bkapi_client/log_search.py index 45eb6cc92..4c9f62fdd 100644 --- a/src/dashboard/apigateway/apigateway/components/bkapi_client/log_search.py +++ b/src/dashboard/apigateway/apigateway/components/bkapi_client/log_search.py @@ -34,3 +34,11 @@ class Client(APIGatewayClient): _api_name = "log-search" api = bind_property(Group, name="api") + + +def new_client_cls(api_name: str): + class Client(APIGatewayClient): + _api_name = api_name + api = bind_property(Group, name="api") + + return Client diff --git a/src/dashboard/apigateway/apigateway/components/prometheus.py b/src/dashboard/apigateway/apigateway/components/prometheus.py index 000d97e47..6055f8b6a 100644 --- a/src/dashboard/apigateway/apigateway/components/prometheus.py +++ b/src/dashboard/apigateway/apigateway/components/prometheus.py @@ -21,10 +21,8 @@ from bkapi_client_core.apigateway import OperationGroup from bkapi_client_core.apigateway.django_helper import get_client_by_username as get_client_by_username_for_apigateway -from django.conf import settings -from apigateway.components.bkapi_client.bkmonitorv3 import Client as BkMonitorV3Client -from apigateway.components.esb_components import get_client_by_username as get_client_by_username_for_esb +from apigateway.components.bkapi_client.bkmonitorv3 import new_client_cls from apigateway.components.handler import RequestAPIHandler @@ -85,13 +83,9 @@ def _promql_query( def _get_api_client(self) -> OperationGroup: # use gateway: bkmonitorv3 - if settings.USE_BKAPI_BKMONITORV3: - apigw_client = get_client_by_username_for_apigateway(BkMonitorV3Client, username="admin") - return apigw_client.api - - # use esb api - esb_client = get_client_by_username_for_esb("admin") - return esb_client.monitor_v3 + client_cls = new_client_cls("bkmonitorv3") + apigw_client = get_client_by_username_for_apigateway(client_cls, username="admin") + return apigw_client.api prometheus_component = PrometheusComponent() diff --git a/src/dashboard/apigateway/apigateway/conf/celery_conf.py b/src/dashboard/apigateway/apigateway/conf/celery_conf.py index 01e2f049e..dd09f0d88 100644 --- a/src/dashboard/apigateway/apigateway/conf/celery_conf.py +++ b/src/dashboard/apigateway/apigateway/conf/celery_conf.py @@ -16,6 +16,8 @@ # We undertake not to change the open source license (MIT license) applicable # to the current version of the project delivered to anyone in the future. # +import os + from celery.schedules import crontab # celery configuration @@ -28,13 +30,15 @@ CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler" -CELERY_IMPORTS = ( +CELERY_IMPORTS = [ "apigateway.apps.monitor.tasks", "apigateway.apps.metrics.tasks", "apigateway.apps.permission.tasks", - "apigateway.apps.esb.component.tasks", "apigateway.controller.tasks", -) +] + +if os.getenv("ENABLE_MULTI_TENANT_MODE", "False").lower() not in ("true", "on", "ok", "y", "yes", "1"): + CELERY_IMPORTS.append("apigateway.apps.esb.component.tasks") CELERY_BEAT_SCHEDULE = { # "add-every-minute": { diff --git a/src/dashboard/apigateway/apigateway/conf/default.py b/src/dashboard/apigateway/apigateway/conf/default.py index 816b14eac..61b54a86b 100644 --- a/src/dashboard/apigateway/apigateway/conf/default.py +++ b/src/dashboard/apigateway/apigateway/conf/default.py @@ -68,6 +68,9 @@ # 是否为本地开发环境 IS_LOCAL = env.bool("DASHBOARD_IS_LOCAL", default=False) +# te 还是 ee +EDITION = env.str("EDITION", "ee") + # 是否开启多租户模式 ENABLE_MULTI_TENANT_MODE = env.bool("ENABLE_MULTI_TENANT_MODE", default=False) @@ -103,9 +106,7 @@ "bkpaas_auth", "apigateway.account", "apigateway.apps.feature", - "apigateway.apps.esb", "apigateway.apps.docs", - "apigateway.apps.esb.bkcore", "apigateway.apps.api_debug", "apigw_manager.apigw", "apigateway.controller", @@ -114,6 +115,13 @@ "bk_notice_sdk", ] +# 非多租户模式才会有 esb 相关的模型 +if not ENABLE_MULTI_TENANT_MODE: + INSTALLED_APPS += [ + "apigateway.apps.esb", + "apigateway.apps.esb.bkcore", + ] + MIDDLEWARE = [ # 这个必须在最前 "django_prometheus.middleware.PrometheusBeforeMiddleware", @@ -262,7 +270,11 @@ "isolation_level": env.str("BK_APIGW_DATABASE_ISOLATION_LEVEL", "READ COMMITTED"), }, }, - "bkcore": { +} + +# 非多租户模式才会有 esb 相关的模型 +if not ENABLE_MULTI_TENANT_MODE: + DATABASES["bkcore"] = { "ENGINE": env.str("BK_ESB_DATABASE_ENGINE", "django.db.backends.mysql"), "NAME": env.str("BK_ESB_DATABASE_NAME", "bk_esb"), "USER": env.str("BK_ESB_DATABASE_USER", BK_APP_CODE), @@ -272,8 +284,7 @@ "OPTIONS": { "isolation_level": env.str("BK_ESB_DATABASE_ISOLATION_LEVEL", "READ COMMITTED"), }, - }, -} + } # ============================================================================== # redis 配置 @@ -636,10 +647,6 @@ "es_index": env.str("BK_ESB_API_LOG_ES_INDEX", "2_bklog_bkapigateway_esb_container*"), } -# 使用 bkmonitorv3 网关 API,还是 monitor_v3 组件 API -USE_BKAPI_BKMONITORV3 = env.bool("USE_BKAPI_BKMONITORV3", False) -# 是否使用 bklog 网关 API -USE_BKAPI_BK_LOG = env.bool("USE_BKAPI_BK_LOG", False) # ============================================================================== # prometheus 配置 @@ -806,6 +813,8 @@ # ---------------------------------------------------------------------------- # 是否展示蓝鲸通知中心组件 "ENABLE_BK_NOTICE": ENABLE_BK_NOTICE, + # 是否开启多租户模式 + "ENABLE_MULTI_TENANT_MODE": ENABLE_MULTI_TENANT_MODE, } # 用户功能开关,将与 DEFAULT_FEATURE_FLAG 合并 diff --git a/src/dashboard/apigateway/apigateway/urls.py b/src/dashboard/apigateway/apigateway/urls.py index 21a555640..18870a082 100644 --- a/src/dashboard/apigateway/apigateway/urls.py +++ b/src/dashboard/apigateway/apigateway/urls.py @@ -31,6 +31,7 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ +from django.conf import settings from django.contrib import admin from django.urls import include, path, re_path from django.views.i18n import set_language @@ -47,8 +48,6 @@ path("backend/admin42/", admin.site.urls), # /userinfo path("backend/accounts/", include("apigateway.account.urls")), - # esb - path("backend/esb/", include("apigateway.apps.esb.urls")), # open api path("backend/api/v1/", include("apigateway.apis.open.urls")), # open api v2 @@ -85,6 +84,12 @@ path("backend/notice/", include(("bk_notice_sdk.urls", "notice"), namespace="notice")), ] +# 非多租户模式才会有 esb 相关的接口 +if not settings.ENABLE_MULTI_TENANT_MODE: + urlpatterns += [ + # esb + path("backend/esb/", include("apigateway.apps.esb.urls")), + ] # backend/docs/ urlpatterns += [