Skip to content

Commit

Permalink
fix: backend slz (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu327 authored Sep 25, 2023
1 parent fa7bf4e commit 293a290
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@
from apigateway.common.fields import CurrentGatewayDefault
from apigateway.common.mixins.serializers import ExtensibleFieldMixin
from apigateway.common.plugin.header_rewrite import HeaderRewriteConvertor
from apigateway.common.release.publish import trigger_gateway_publish
from apigateway.core.constants import (
DEFAULT_BACKEND_NAME,
DEFAULT_LB_HOST_WEIGHT,
STAGE_NAME_PATTERN,
LoadBalanceTypeEnum,
PublishSourceEnum,
)
from apigateway.core.models import Backend, BackendConfig, MicroGateway, Stage

Expand Down Expand Up @@ -230,15 +228,13 @@ def _get_stage_backend_config(self, proxy_http_config):
scheme, _host = host["host"].rstrip("/").split("://")
hosts.append({"scheme": scheme, "host": _host, "weight": host["weight"]})

config = {
return {
"type": "node",
"timeout": proxy_http_config["timeout"],
"loadbalance": proxy_http_config["upstreams"]["loadbalance"],
"hosts": hosts,
}

return config

def update(self, instance, validated_data):
validated_data.pop("name", None)
# 仅能通过发布更新 status,不允许直接更新 status
Expand Down
30 changes: 0 additions & 30 deletions src/dashboard/apigateway/apigateway/apis/web/backend/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,4 @@
#
import re

from blue_krill.data_types.enum import EnumField, StructuredEnum

from apigateway.core.constants import BackendTypeEnum

BACKEND_NAME_PATTERN = re.compile(r"^[a-zA-Z][a-zA-Z0-9-]{0,19}$")


class BackendConfigTypeEnum(StructuredEnum):
NODE = EnumField("node", label="节点")
# NOTE: 当前不支持
SERVICE_DISCOVERY = EnumField("service_discovery", label="服务发现")


class LoadBalanceTypeEnum(StructuredEnum):
RR = EnumField("roundrobin", "RR")
WRR = EnumField("weighted-roundrobin", "Weighted-RR")


class BackendConfigSchemeEnum(StructuredEnum):
# for http type backend
HTTP = EnumField("http", label="HTTP")
HTTPS = EnumField("https", label="HTTPS")
# for grpc type backend
GRPC = EnumField("grpc", label="GRPC")
GRPCS = EnumField("grpcs", label="GRPCS")


BACKEND_CONFIG_SCHEME_MAP = {
BackendTypeEnum.HTTP.value: [BackendConfigSchemeEnum.HTTP.value, BackendConfigSchemeEnum.HTTPS.value],
BackendTypeEnum.GRPC.value: [BackendConfigSchemeEnum.GRPC.value, BackendConfigSchemeEnum.GRPCS.value],
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,13 @@
from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator

from apigateway.biz.constants import MAX_BACKEND_TIMEOUT_IN_SECOND
from apigateway.apis.web.serializers import BaseBackendConfigSLZ
from apigateway.apis.web.constants import BACKEND_CONFIG_SCHEME_MAP
from apigateway.common.fields import CurrentGatewayDefault
from apigateway.core.constants import DEFAULT_BACKEND_NAME, HOST_WITHOUT_SCHEME_PATTERN, BackendTypeEnum
from apigateway.core.constants import DEFAULT_BACKEND_NAME, BackendTypeEnum
from apigateway.core.models import Backend, BackendConfig, Stage

from .constants import (
BACKEND_CONFIG_SCHEME_MAP,
BACKEND_NAME_PATTERN,
BackendConfigSchemeEnum,
BackendConfigTypeEnum,
LoadBalanceTypeEnum,
)


class HostSLZ(serializers.Serializer):
scheme = serializers.ChoiceField(choices=BackendConfigSchemeEnum.get_choices())
host = serializers.RegexField(HOST_WITHOUT_SCHEME_PATTERN)
weight = serializers.IntegerField(min_value=1, required=False)

class Meta:
ref_name = "apis.web.backend.HostSLZ"


class BaseBackendConfigSLZ(serializers.Serializer):
type = serializers.ChoiceField(
choices=BackendConfigTypeEnum.get_choices(), default=BackendConfigTypeEnum.NODE.value
)
timeout = serializers.IntegerField(max_value=MAX_BACKEND_TIMEOUT_IN_SECOND, min_value=1)
loadbalance = serializers.ChoiceField(choices=LoadBalanceTypeEnum.get_choices())
hosts = serializers.ListField(child=HostSLZ(), allow_empty=False)
from .constants import BACKEND_NAME_PATTERN


class BackendConfigSLZ(BaseBackendConfigSLZ):
Expand Down
28 changes: 28 additions & 0 deletions src/dashboard/apigateway/apigateway/apis/web/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#
from blue_krill.data_types.enum import EnumField, StructuredEnum

from apigateway.core.constants import BackendTypeEnum


class UserAuthTypeEnum(StructuredEnum):
IEOD = EnumField("ieod")
Expand All @@ -31,3 +33,29 @@ class ExportTypeEnum(StructuredEnum):
FILTERED = EnumField("filtered")
# 已选资源
SELECTED = EnumField("selected")


class BackendConfigTypeEnum(StructuredEnum):
NODE = EnumField("node", label="节点")
# NOTE: 当前不支持
SERVICE_DISCOVERY = EnumField("service_discovery", label="服务发现")


class LoadBalanceTypeEnum(StructuredEnum):
RR = EnumField("roundrobin", "RR")
WRR = EnumField("weighted-roundrobin", "Weighted-RR")


class BackendConfigSchemeEnum(StructuredEnum):
# for http type backend
HTTP = EnumField("http", label="HTTP")
HTTPS = EnumField("https", label="HTTPS")
# for grpc type backend
GRPC = EnumField("grpc", label="GRPC")
GRPCS = EnumField("grpcs", label="GRPCS")


BACKEND_CONFIG_SCHEME_MAP = {
BackendTypeEnum.HTTP.value: [BackendConfigSchemeEnum.HTTP.value, BackendConfigSchemeEnum.HTTPS.value],
BackendTypeEnum.GRPC.value: [BackendConfigSchemeEnum.GRPC.value, BackendConfigSchemeEnum.GRPCS.value],
}
42 changes: 42 additions & 0 deletions src/dashboard/apigateway/apigateway/apis/web/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
#
# TencentBlueKing is pleased to support the open source community by making
# 蓝鲸智云 - API 网关(BlueKing - APIGateway) available.
# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
# Licensed under the MIT License (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://opensource.org/licenses/MIT
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific language governing permissions and
# limitations under the License.
#
# 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 rest_framework import serializers

from apigateway.biz.constants import MAX_BACKEND_TIMEOUT_IN_SECOND
from apigateway.core.constants import HOST_WITHOUT_SCHEME_PATTERN

from .constants import BackendConfigSchemeEnum, BackendConfigTypeEnum, LoadBalanceTypeEnum


class HostSLZ(serializers.Serializer):
scheme = serializers.ChoiceField(choices=BackendConfigSchemeEnum.get_choices())
host = serializers.RegexField(HOST_WITHOUT_SCHEME_PATTERN)
weight = serializers.IntegerField(min_value=1, required=False)

class Meta:
ref_name = "apis.web.HostSLZ"


class BaseBackendConfigSLZ(serializers.Serializer):
type = serializers.ChoiceField(
choices=BackendConfigTypeEnum.get_choices(), default=BackendConfigTypeEnum.NODE.value
)
timeout = serializers.IntegerField(max_value=MAX_BACKEND_TIMEOUT_IN_SECOND, min_value=1)
loadbalance = serializers.ChoiceField(choices=LoadBalanceTypeEnum.get_choices())
hosts = serializers.ListField(child=HostSLZ(), allow_empty=False)
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from rest_framework.validators import UniqueTogetherValidator
from tencent_apigateway_common.i18n.field import SerializerTranslatedField

from apigateway.apis.web.backend.constants import BACKEND_CONFIG_SCHEME_MAP
from apigateway.apis.web.backend.serializers import BaseBackendConfigSLZ
from apigateway.apis.web.constants import BACKEND_CONFIG_SCHEME_MAP
from apigateway.apis.web.serializers import BaseBackendConfigSLZ
from apigateway.biz.validators import MaxCountPerGatewayValidator
from apigateway.common.fields import CurrentGatewayDefault
from apigateway.core.constants import STAGE_NAME_PATTERN, ReleaseStatusEnum, StageStatusEnum
Expand Down

0 comments on commit 293a290

Please sign in to comment.