Skip to content

Commit

Permalink
feat(components): use gateway of bkmonito/bklog, disable esb when ENA…
Browse files Browse the repository at this point in the history
…BLE_MULTI_TENANT_MODE
  • Loading branch information
wklken committed Dec 2, 2024
1 parent e905231 commit c98d9ea
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 66 deletions.
51 changes: 27 additions & 24 deletions src/dashboard/apigateway/apigateway/apis/open/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/<int:system_id>/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/<int:record_id>/",
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/<int:system_id>/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/<int:record_id>/",
esb_permission_views.AppPermissionApplyRecordViewSet.as_view({"get": "retrieve"}),
name="openapi.esb.permission.app-record-detail",
),
]
21 changes: 14 additions & 7 deletions src/dashboard/apigateway/apigateway/apis/web/docs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -26,11 +27,17 @@
path("gateways/<slug:gateway_name>/resources/", include("apigateway.apis.web.docs.gateway.resource.urls")),
path("gateways/<slug:gateway_name>/stages/", include("apigateway.apis.web.docs.gateway.stage.urls")),
path("gateways/<slug:gateway_name>/sdks/", include("apigateway.apis.web.docs.gateway.gateway_sdk.urls")),
# esb
path("esb/boards/<slug:board>/systems/", include("apigateway.apis.web.docs.esb.system.urls")),
path(
"esb/boards/<slug:board>/systems/<slug:system_name>/components/",
include("apigateway.apis.web.docs.esb.component.urls"),
),
path("esb/boards/<slug:board>/sdks/", include("apigateway.apis.web.docs.esb.sdk.urls")),
]


# 非多租户模式才会有 esb 相关的接口
if not settings.ENABLE_MULTI_TENANT_MODE:
urlpatterns += [
# esb
path("esb/boards/<slug:board>/systems/", include("apigateway.apis.web.docs.esb.system.urls")),
path(
"esb/boards/<slug:board>/systems/<slug:system_name>/components/",
include("apigateway.apis.web.docs.esb.component.urls"),
),
path("esb/boards/<slug:board>/sdks/", include("apigateway.apis.web.docs.esb.sdk.urls")),
]
21 changes: 10 additions & 11 deletions src/dashboard/apigateway/apigateway/components/bk_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 4 additions & 10 deletions src/dashboard/apigateway/apigateway/components/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()
10 changes: 7 additions & 3 deletions src/dashboard/apigateway/apigateway/conf/celery_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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": {
Expand Down
27 changes: 18 additions & 9 deletions src/dashboard/apigateway/apigateway/conf/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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),
Expand All @@ -272,8 +284,7 @@
"OPTIONS": {
"isolation_level": env.str("BK_ESB_DATABASE_ISOLATION_LEVEL", "READ COMMITTED"),
},
},
}
}

# ==============================================================================
# redis 配置
Expand Down Expand Up @@ -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 配置
Expand Down Expand Up @@ -806,6 +813,8 @@
# ----------------------------------------------------------------------------
# 是否展示蓝鲸通知中心组件
"ENABLE_BK_NOTICE": ENABLE_BK_NOTICE,
# 是否开启多租户模式
"ENABLE_MULTI_TENANT_MODE": ENABLE_MULTI_TENANT_MODE,
}

# 用户功能开关,将与 DEFAULT_FEATURE_FLAG 合并
Expand Down
9 changes: 7 additions & 2 deletions src/dashboard/apigateway/apigateway/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 += [
Expand Down

0 comments on commit c98d9ea

Please sign in to comment.