Skip to content

Commit

Permalink
feat: update micro gateway values (#7)
Browse files Browse the repository at this point in the history
* feat: update micro gateway values
* test: fix micro gateway values generotor
  • Loading branch information
zhu327 authored Jun 2, 2023
1 parent bb6098d commit 60b9167
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 70 deletions.
6 changes: 3 additions & 3 deletions src/dashboard/apigateway/apigateway/conf/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@
}

# 微网关 chart 信息
BCS_MICRO_GATEWAY_CHART_NAME = "bk-micro-gateway-operator"
BCS_MICRO_GATEWAY_CHART_VERSION = "0.0.1"
BCS_MICRO_GATEWAY_CHART_NAME = "bk-micro-gateway"
BCS_MICRO_GATEWAY_CHART_VERSION = "v1.0.0-alpha.2"
BCS_MICRO_GATEWAY_IMAGE_REGISTRY = ""
BCS_MICRO_GATEWAY_SENTRY_DSN = env.str("BCS_MICRO_GATEWAY_SENTRY_DSN", "")

Expand Down Expand Up @@ -650,7 +650,7 @@
}

APISIX_CONFIG = {
"plugin_attr": {
"pluginAttrs": {
"log-rotate": {
# 每间隔多长时间切分一次日志,秒为单位
"interval": 60 * 60,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from django.conf import settings

from apigateway.controller.crds.config_controller import create_config_controller
from apigateway.core.micro_gateway_config import MicroGatewayHTTPInfo, MicroGatewayJWTAuth
from apigateway.core.micro_gateway_config import MicroGatewayHTTPInfo
from apigateway.core.models import MicroGateway, Stage


Expand All @@ -37,7 +37,9 @@ class MicroGatewayValuesGenerator:
def __attrs_post_init__(self):
self.micro_gateway_config = self.micro_gateway.config
self.stage = self.micro_gateway.stage_set.first()
self.http_info = MicroGatewayHTTPInfo.from_micro_gateway_config(self.micro_gateway_config)
self.http_info = MicroGatewayHTTPInfo.from_micro_gateway_config(
self.micro_gateway_config
) # NOTE 配置一个url, 用于状态上报
self.values = self.micro_gateway_config.get("values", {})

def _merge_dict(self, to: Dict[str, Any], from_: Dict[str, Any]):
Expand All @@ -62,52 +64,56 @@ def generate_values(self) -> Dict[str, Any]:
values = {
"global": {
"imageRegistry": settings.BCS_MICRO_GATEWAY_IMAGE_REGISTRY,
"serviceMonitor": {
"enabled": True,
},
},
"serviceMonitor": {
"enabled": True,
},
"replicaCount": 2,
"sentry": {
"enabled": settings.BCS_MICRO_GATEWAY_SENTRY_DSN != "",
"dsn": settings.BCS_MICRO_GATEWAY_SENTRY_DSN,
"operator": {
"sentryDsn": settings.BCS_MICRO_GATEWAY_SENTRY_DSN,
},
"service": {
"type": "NodePort",
},
"gomicro-discovery-operator": {
"enabled": False,
},
"apisixConfig": self._get_apisix_config(),
"apisix": self._get_apisix_config(),
}
self._merge_dict(values, deepcopy(self.values))

jwt_auth_info = MicroGatewayJWTAuth.from_micro_gateway_config(self.micro_gateway.config)
self._merge_dict(
values,
{
"gatewayConfigEnabled": True,
"gatewayConfig": {
"instance_id": self.micro_gateway.instance_id,
"controller": {
"endpoints": [settings.BK_API_URL_TMPL.format(api_name="bk-apigateway")],
"base_path": settings.EDGE_CONTROLLER_API_BASE_PATH,
"jwt_auth": {
"secret": jwt_auth_info.secret_key,
},
},
},
"gatewayStageEnabled": False,
},
)
# TODO 暂时不支持配置, 等状态上报的方案确定
# jwt_auth_info = MicroGatewayJWTAuth.from_micro_gateway_config(self.micro_gateway.config)
# self._merge_dict(
# values,
# {
# "gatewayConfigEnabled": True,
# "gatewayConfig": {
# "instance_id": self.micro_gateway.instance_id,
# "controller": {
# "endpoints": [settings.BK_API_URL_TMPL.format(api_name="bk-apigateway")],
# "base_path": settings.EDGE_CONTROLLER_API_BASE_PATH,
# "jwt_auth": {
# "secret": jwt_auth_info.secret_key,
# },
# },
# },
# "gatewayStageEnabled": False,
# },
# )

return values

def _get_apisix_config(self) -> Dict[str, Any]:
controller = create_config_controller(self.micro_gateway)
config = {
"bk_gateway": {
"instance_id": self.micro_gateway.instance_id,
"controller": controller.dict(),
# TODO 后续需要支持bk_gateway的全套配置
# "bk_gateway": {
# "instance_id": self.micro_gateway.instance_id,
# "controller": controller.dict(),
# }
"bkGateway": {
"instance": {
"id": self.micro_gateway.instance_id,
"secret": controller.jwt_auth.secret_key,
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def setup_settings(self, settings, faker):

def test_generate_values(self, mocker, micro_gateway, settings):
settings.APISIX_CONFIG = {
"plugin_attr": {
"pluginAttrs": {
"log-rotate": {
# 每间隔多长时间切分一次日志,秒为单位
"interval": 60 * 60,
Expand All @@ -67,35 +67,19 @@ def test_generate_values(self, mocker, micro_gateway, settings):
assert values == {
"global": {
"imageRegistry": settings.BCS_MICRO_GATEWAY_IMAGE_REGISTRY,
"serviceMonitor": {
"enabled": True,
},
},
"sentry": {
"enabled": settings.BCS_MICRO_GATEWAY_SENTRY_DSN != "",
"dsn": settings.BCS_MICRO_GATEWAY_SENTRY_DSN,
"serviceMonitor": {
"enabled": True,
},
"operator": {
"sentryDsn": settings.BCS_MICRO_GATEWAY_SENTRY_DSN,
},
"replicaCount": 2,
"gatewayStageEnabled": False,
"service": {
"type": "NodePort",
},
"gatewayConfigEnabled": True,
"gatewayConfig": {
"instance_id": micro_gateway.instance_id,
"controller": {
"endpoints": [settings.BK_API_URL_TMPL.format(api_name="bk-apigateway")],
"base_path": settings.EDGE_CONTROLLER_API_BASE_PATH,
"jwt_auth": {
"secret": "jwt_secret_key",
},
},
},
"gomicro-discovery-operator": {
"enabled": False,
},
"apisixConfig": {
"plugin_attr": {
"apisix": {
"pluginAttrs": {
"log-rotate": {
"interval": 60 * 60,
"max_kept": 24 * 7,
Expand All @@ -106,12 +90,10 @@ def test_generate_values(self, mocker, micro_gateway, settings):
},
},
},
"bk_gateway": {
"instance_id": micro_gateway.instance_id,
"controller": {
"endpoints": [settings.BK_API_URL_TMPL.format(api_name="bk-apigateway")],
"base_path": settings.EDGE_CONTROLLER_API_BASE_PATH,
"jwt_auth": {"secret": "jwt_secret_key"},
"bkGateway": {
"instance": {
"id": micro_gateway.instance_id,
"secret": "jwt_secret_key",
},
},
},
Expand All @@ -123,9 +105,10 @@ def test_generate_values_with_extra_config(self, micro_gateway):
"values": {
"extraEnvVarsSecret": "env-vars-secret",
"replicaCount": 1,
"gatewayConfigEnabled": True,
"gatewayConfig": {
"instance_id": "not-allowed",
"bkGateway": {
"instance": {
"id": "not-allowed",
}
},
},
}
Expand All @@ -135,4 +118,4 @@ def test_generate_values_with_extra_config(self, micro_gateway):

assert values["replicaCount"] == 1
assert values["extraEnvVarsSecret"] == "env-vars-secret"
assert values["gatewayConfig"]["instance_id"] == micro_gateway.instance_id
assert values["bkGateway"]["instance"]["id"] == micro_gateway.instance_id

0 comments on commit 60b9167

Please sign in to comment.