Skip to content

Commit

Permalink
enhance: remove "cnative-default-cluster" concept and related code (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
piglei authored May 27, 2024
1 parent 46c434c commit b90d94a
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 127 deletions.
4 changes: 2 additions & 2 deletions apiserver/paasng/conf.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@
# IS_ALLOW_CREATE_SMART_APP_BY_DEFAULT: true
## 是否默认允许创建云原生应用
# IS_ALLOW_CREATE_CLOUD_NATIVE_APP_BY_DEFAULT: false
## 云原生应用的默认集群名称
# CLOUD_NATIVE_APP_DEFAULT_CLUSTER: ""
## 使用“应用迁移”功能,迁移到云原生应用时所使用的目标集群名称,不配置时使用 region 默认集群
# MGRLEGACY_CLOUD_NATIVE_TARGET_CLUSTER: ""
## 是否允许创建蓝鲸插件应用
# IS_ALLOW_CREATE_BK_PLUGIN_APP: false
## 新建的 lesscode 应用是否为云原生应用
Expand Down
18 changes: 1 addition & 17 deletions apiserver/paasng/paas_wl/infras/cluster/shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
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 typing import TYPE_CHECKING, List, Optional

from paas_wl.bk_app.applications.constants import WlAppType
from paas_wl.bk_app.applications.models import WlApp
from paas_wl.infras.cluster.constants import ClusterFeatureFlag
from paas_wl.infras.cluster.models import Cluster
from paasng.platform.applications.models import ModuleEnvironment
from paasng.utils.configs import get_region_aware

if TYPE_CHECKING:
from paasng.platform.applications.models import Application
Expand All @@ -45,10 +44,6 @@ def get_default_cluster(self) -> Cluster:
return qs[0]
raise Cluster.DoesNotExist(f"Default cluster not found for {self.region}")

def get_cnative_app_default_cluster(self) -> Cluster:
default_cluster_name = get_region_aware("CLOUD_NATIVE_APP_DEFAULT_CLUSTER", self.region)
return self.get_cluster_by_name(default_cluster_name)

def get_cluster_by_name(self, cluster_name: str) -> Cluster:
return Cluster.objects.get(region=self.region, name=cluster_name)

Expand All @@ -75,11 +70,6 @@ def get_cluster_name(self) -> str:

if wl_app.latest_config.cluster:
return wl_app.latest_config.cluster

# 云原生应用需要用自己的默认集群
if wl_app.type == WlAppType.CLOUD_NATIVE:
return self._get_cnative_app_default_cluster(region).name

return RegionClusterService(region).get_default_cluster().name

def bind_cluster(self, cluster_name: Optional[str]):
Expand All @@ -92,17 +82,11 @@ def bind_cluster(self, cluster_name: Optional[str]):

if cluster_name:
cluster = Cluster.objects.get(name=cluster_name)
elif wl_app.type == WlAppType.CLOUD_NATIVE:
# 云原生应用需要用自己的默认集群
cluster = self._get_cnative_app_default_cluster(region)
else:
cluster = RegionClusterService(region).get_default_cluster()

_bind_cluster_to_wl_app(wl_app, cluster)

def _get_cnative_app_default_cluster(self, region):
return RegionClusterService(region).get_cnative_app_default_cluster()


def _bind_cluster_to_wl_app(wl_app: WlApp, cluster: Cluster):
"""bind cluster to wl_app by modifying config.cluster"""
Expand Down
9 changes: 6 additions & 3 deletions apiserver/paasng/paas_wl/infras/cluster/utils.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 paas_wl.bk_app.applications.models import WlApp
from paas_wl.infras.cluster.models import Cluster

Expand Down Expand Up @@ -50,6 +51,8 @@ def get_cluster_by_app(app: WlApp) -> Cluster:


def get_dev_sandbox_cluster(app: WlApp) -> Cluster:
# 目前云原生集群的 k8s 版本 >= 1.20.x, 因此默认选择云原生集群作为开发容器集群, 以减少 ingress 等资源版本的兼容问题
# TODO Cluster 增加新字段(配置项)来标记功能所使用的集群(以及配置整个功能开关等)
return RegionClusterService(app.region).get_cnative_app_default_cluster()
# 当前直接使用 region 下的默认集群,注意:
# - 功能要求 k8s 版本 >= 1.20.x, 版本过低可能会导致 ingress 等资源出现版本兼容问题
#
# TODO:Cluster 增加新字段(配置项)来标记功能所使用的集群(以及配置整个功能开关等)
return RegionClusterService(app.region).get_default_cluster()
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
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.permissions import IsAuthenticated

from paas_wl.infras.cluster.shim import RegionClusterService
from paasng.core.region.models import get_all_regions
from paasng.infras.accounts.permissions.constants import SiteAction
from paasng.infras.accounts.permissions.global_site import site_perm_class
from paasng.plat_admin.admin42.utils.mixins import GenericTemplateView
from paasng.utils.configs import get_region_aware


class OperatorManageView(GenericTemplateView):
Expand All @@ -36,13 +37,8 @@ def get_context_data(self, **kwargs):
if "view" not in kwargs:
kwargs["view"] = self

cnative_default_cluster = {}
for region in get_all_regions():
try:
cluster_name = get_region_aware("CLOUD_NATIVE_APP_DEFAULT_CLUSTER", region)
except KeyError:
cluster_name = "--"
cnative_default_cluster[region] = cluster_name

kwargs["cnative_default_cluster"] = cnative_default_cluster
# "Cnative default" cluster == THE default cluster
kwargs["cnative_default_cluster"] = {
region: RegionClusterService(region).get_default_cluster().name for region in get_all_regions()
}
return kwargs
21 changes: 3 additions & 18 deletions apiserver/paasng/paasng/platform/bkapp_model/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +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.
"""
from typing import Dict, List, Optional

from django.utils.translation import gettext_lazy as _
from typing import Dict, List, Optional

from paas_wl.bk_app.applications.api import (
create_app_ignore_duplicated,
Expand All @@ -34,8 +33,6 @@
from paasng.platform.engine.models import EngineApp
from paasng.platform.modules.manager import ModuleInitializer, make_engine_app_name
from paasng.platform.modules.models import Module
from paasng.utils.configs import get_region_aware
from paasng.utils.error_codes import error_codes

# Model-Resource
default_environments: List[str] = [AppEnvName.STAG.value, AppEnvName.PROD.value]
Expand All @@ -60,8 +57,9 @@ def initialize_simple(
:param target_port: Custom target port
"""
if not cluster_name:
cluster_name = get_default_cluster_name(module.region)
cluster_name = RegionClusterService(module.region).get_default_cluster().name

assert cluster_name
model_res = create_cnative_app_model_resource(module, image, api_version, command, args, target_port)
create_engine_apps(module.application, module, environments=default_environments, cluster_name=cluster_name)
return model_res
Expand Down Expand Up @@ -103,16 +101,3 @@ def get_or_create_engine_app(owner: str, region: str, engine_app_name: str) -> E
owner=owner,
region=region,
)


def get_default_cluster_name(region: str) -> str:
"""Get default cluster name from settings, and valid if we have this cluster."""
try:
default_cluster_name = get_region_aware("CLOUD_NATIVE_APP_DEFAULT_CLUSTER", region)
except Exception as e:
raise error_codes.CANNOT_CREATE_APP.f(_("暂无可用集群, 请联系管理员")) from e

try:
return RegionClusterService(region).get_cluster_by_name(default_cluster_name).name
except Exception:
raise error_codes.CANNOT_CREATE_APP.f(_(f"集群 {default_cluster_name} 未就绪, 请联系管理员"))
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
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 typing import List, Optional

from paas_wl.infras.cluster.shim import EnvClusterService, RegionClusterService
from paas_wl.infras.cluster.shim import EnvClusterService
from paasng.platform.applications.constants import ApplicationType
from paasng.platform.mgrlegacy.entities import ClusterLegacyData, DefaultAppLegacyData
from paasng.platform.mgrlegacy.exceptions import PreCheckMigrationFailed
from paasng.platform.mgrlegacy.utils import get_cnative_target_cluster

from .base import CNativeBaseMigrator

Expand Down Expand Up @@ -49,9 +51,9 @@ def _can_migrate_or_raise(self):
raise PreCheckMigrationFailed(f"app({self.app.code}) type does not set to cloud_native")

def _migrate(self):
cnative_cluster = RegionClusterService(self.app.region).get_cnative_app_default_cluster()
cnative_cluster_name = get_cnative_target_cluster(self.app.region).name
for env in self.app.get_app_envs():
EnvClusterService(env).bind_cluster(cnative_cluster.name)
EnvClusterService(env).bind_cluster(cnative_cluster_name)

def _rollback(self):
clusters: List[ClusterLegacyData] = self.migration_process.legacy_data.clusters
Expand Down
20 changes: 20 additions & 0 deletions apiserver/paasng/paasng/platform/mgrlegacy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@
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 logging
import typing

from django.utils.functional import cached_property
from rest_framework.exceptions import PermissionDenied

from paas_wl.infras.cluster.models import Cluster
from paas_wl.infras.cluster.shim import RegionClusterService
from paasng.accessories.publish.sync_market.managers import AppDeveloperManger
from paasng.core.region.models import get_region
from paasng.platform.applications.constants import AppLanguage
from paasng.platform.applications.models import Application
from paasng.platform.mgrlegacy.constants import LegacyAppTag, MigrationStatus
from paasng.platform.mgrlegacy.models import MigrationProcess
from paasng.utils.configs import get_region_aware

try:
from paasng.platform.mgrlegacy.legacy_proxy_te import LegacyAppProxy
Expand Down Expand Up @@ -216,3 +220,19 @@ def get_prod_exposed_link(self):
name = "bkapp-%s-%s" % (self.legacy_app.code, "prod")
context = dict(region=region_name, name=name)
return tmpl.format(**context)


def get_cnative_target_cluster(region: str) -> Cluster:
"""Get the target cluster when migrating an application to the cloud-native type.
:param region: The region name.
:return: The cluster, return the default cluster if the target cluster is not
configured in the given region.
"""
try:
target_cluster_name = get_region_aware("MGRLEGACY_CLOUD_NATIVE_TARGET_CLUSTER", region)
except KeyError:
logger.warning("Get mgrlegacy target cluster failed, return default instead, region: %s.", region)
return RegionClusterService(region).get_default_cluster()

return RegionClusterService(region).get_cluster_by_name(target_cluster_name)
8 changes: 4 additions & 4 deletions apiserver/paasng/paasng/platform/mgrlegacy/views.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.
"""

import datetime
import logging
from typing import Dict, List, Optional
Expand All @@ -36,7 +37,6 @@
from paas_wl.bk_app.processes.constants import ProcessUpdateType
from paas_wl.bk_app.processes.serializers import UpdateProcessSLZ
from paas_wl.bk_app.processes.shim import ProcessManager
from paas_wl.infras.cluster.shim import RegionClusterService
from paas_wl.infras.cluster.utils import get_cluster_by_app
from paas_wl.infras.resources.generation.mapper import get_mapper_proc_config_latest
from paas_wl.workloads.networking.egress.models import RCStateAppBinding, RegionClusterState
Expand Down Expand Up @@ -81,7 +81,7 @@
rollback_cnative_to_default,
rollback_migration_process,
)
from paasng.platform.mgrlegacy.utils import LegacyAppManager, check_operation_perms
from paasng.platform.mgrlegacy.utils import LegacyAppManager, check_operation_perms, get_cnative_target_cluster
from paasng.platform.modules.models import Module
from paasng.utils.error_codes import error_codes

Expand Down Expand Up @@ -350,7 +350,7 @@ def _can_migrate_or_raise(app):
if (last_process := CNativeMigrationProcess.objects.filter(app=app).last()) and last_process.is_active():
raise error_codes.APP_MIGRATION_FAILED.f("该应用正在变更中, 无法迁移")

cnative_cluster_name = RegionClusterService(app.region).get_cnative_app_default_cluster().name
cnative_cluster_name = get_cnative_target_cluster(app.region).name
for m in app.modules.all():
for env in m.envs.all():
cluster_name = env.wl_app.config_set.latest().cluster
Expand Down Expand Up @@ -467,7 +467,7 @@ def get(self, request, code):
app_rcs_bindings.append(binding)

# 目前仅有一个云原生集群
cnative_cluster = RegionClusterService(app.region).get_cnative_app_default_cluster()
cnative_cluster = get_cnative_target_cluster(app.region)
root_domains = {
# 当前普通应用的子域名
"legacy": list(set(app_root_domains)),
Expand Down
4 changes: 2 additions & 2 deletions apiserver/paasng/paasng/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,8 @@ def _build_file_handler(log_path: Path, filename: str, format: str) -> Dict:
# 是否默认允许创建云原生应用
IS_ALLOW_CREATE_CLOUD_NATIVE_APP_BY_DEFAULT = settings.get("IS_ALLOW_CREATE_CLOUD_NATIVE_APP_BY_DEFAULT", False)

# 云原生应用的默认集群名称
CLOUD_NATIVE_APP_DEFAULT_CLUSTER = settings.get("CLOUD_NATIVE_APP_DEFAULT_CLUSTER", "")
# 使用“应用迁移”功能,迁移到云原生应用时所使用的目标集群名称,不配置时使用 region 默认集群
MGRLEGACY_CLOUD_NATIVE_TARGET_CLUSTER = settings.get("MGRLEGACY_CLOUD_NATIVE_TARGET_CLUSTER", "")

# 新建的 lesscode 应用是否为云原生应用
LESSCODE_APP_USE_CLOUD_NATIVE_TYPE = settings.get("LESSCODE_APP_USE_CLOUD_NATIVE_TYPE", True)
Expand Down
1 change: 1 addition & 0 deletions apiserver/paasng/tests/api/conftest.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 unittest import mock

import pytest
Expand Down
3 changes: 1 addition & 2 deletions apiserver/paasng/tests/api/test_applications.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.
"""

import logging
from unittest import mock

Expand All @@ -41,7 +42,6 @@
from paasng.platform.sourcectl.connector import IntegratedSvnAppRepoConnector, SourceSyncResult
from paasng.utils.basic import get_username_by_bkpaas_user_id
from paasng.utils.error_codes import error_codes
from tests.conftest import CLUSTER_NAME_FOR_TESTING
from tests.utils.auth import create_user
from tests.utils.helpers import configure_regions, generate_random_string

Expand Down Expand Up @@ -490,7 +490,6 @@ class TestCreateCloudNativeApp:

@pytest.fixture(autouse=True)
def _setup(self, mock_wl_services_in_creation, mock_initialize_vcs_with_template, _init_tmpls, bk_user, settings):
settings.CLOUD_NATIVE_APP_DEFAULT_CLUSTER = CLUSTER_NAME_FOR_TESTING
AccountFeatureFlag.objects.set_feature(bk_user, AFF.ALLOW_CREATE_CLOUD_NATIVE_APP, True)

def test_create_with_image(self, api_client):
Expand Down
5 changes: 3 additions & 2 deletions apiserver/paasng/tests/api/test_cnative_migration.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 unittest import mock

import pytest
Expand Down Expand Up @@ -252,7 +253,7 @@ def _set_default_cluster(self, settings, bk_app):
cluster_name = get_random_string(6)
G(Cluster, name=cluster_name, region=bk_app.region)
G(Cluster, name=CLUSTER_NAME_FOR_TESTING, region=bk_app.region)
settings.CLOUD_NATIVE_APP_DEFAULT_CLUSTER = cluster_name
settings.MGRLEGACY_CLOUD_NATIVE_TARGET_CLUSTER = cluster_name

@pytest.fixture(autouse=True)
def _set_rcs_binding(self, _set_default_cluster, settings, bk_app, _with_wl_apps, bk_stag_env):
Expand All @@ -271,7 +272,7 @@ def _set_rcs_binding(self, _set_default_cluster, settings, bk_app, _with_wl_apps

RegionClusterState.objects.create(
region=wl_app.region,
cluster_name=settings.CLOUD_NATIVE_APP_DEFAULT_CLUSTER,
cluster_name=settings.MGRLEGACY_CLOUD_NATIVE_TARGET_CLUSTER,
nodes_data=[
{
"metadata": {"name": "node-b"},
Expand Down
3 changes: 1 addition & 2 deletions apiserver/paasng/tests/api/test_modules.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.
"""

import logging
from unittest import mock

Expand All @@ -31,7 +32,6 @@
from paasng.platform.modules.models import BuildConfig
from paasng.platform.modules.models.module import Module
from paasng.platform.sourcectl.connector import IntegratedSvnAppRepoConnector, SourceSyncResult
from tests.conftest import CLUSTER_NAME_FOR_TESTING
from tests.utils.helpers import generate_random_string, initialize_module

pytestmark = pytest.mark.django_db(databases=["default", "workloads"])
Expand Down Expand Up @@ -115,7 +115,6 @@ def test_create_nondefault_origin(
class TestCreateCloudNativeModule:
@pytest.fixture(autouse=True)
def _setup(self, mock_wl_services_in_creation, mock_initialize_vcs_with_template, _init_tmpls, bk_user, settings):
settings.CLOUD_NATIVE_APP_DEFAULT_CLUSTER = CLUSTER_NAME_FOR_TESTING
AccountFeatureFlag.objects.set_feature(bk_user, AFF.ALLOW_CREATE_CLOUD_NATIVE_APP, True)

def test_create_with_image(self, bk_cnative_app, api_client):
Expand Down
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.
"""

import pytest

from paas_wl.bk_app.dev_sandbox.controller import _DevWlAppCreator
Expand All @@ -25,11 +26,6 @@
from tests.conftest import CLUSTER_NAME_FOR_TESTING


@pytest.fixture(autouse=True)
def _set_default_cluster(settings):
settings.CLOUD_NATIVE_APP_DEFAULT_CLUSTER = CLUSTER_NAME_FOR_TESTING


@pytest.fixture()
def default_dev_sandbox_cluster():
return Cluster.objects.get(name=CLUSTER_NAME_FOR_TESTING)
Expand Down
Loading

0 comments on commit b90d94a

Please sign in to comment.