From a16e4b4496affbaf2858a8623fa9140a8498ce58 Mon Sep 17 00:00:00 2001 From: jamesgetx Date: Wed, 4 Dec 2024 17:57:32 +0800 Subject: [PATCH] fix: apply error ingress when cnative app switching default module N --- .../paas_wl/bk_app/cnative/specs/handlers.py | 26 ++++++++++++++++++- .../workloads/networking/entrance/handlers.py | 6 ++++- .../publish/entrance/test_triggers.py | 12 +++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/apiserver/paasng/paas_wl/bk_app/cnative/specs/handlers.py b/apiserver/paasng/paas_wl/bk_app/cnative/specs/handlers.py index caf37452da..648904b7b2 100644 --- a/apiserver/paasng/paas_wl/bk_app/cnative/specs/handlers.py +++ b/apiserver/paasng/paas_wl/bk_app/cnative/specs/handlers.py @@ -14,14 +14,38 @@ # 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 django.dispatch import receiver -from paas_wl.bk_app.cnative.specs.resource import deploy_networking +from paas_wl.bk_app.cnative.specs.resource import deploy_networking, get_mres_from_cluster, sync_networking from paas_wl.workloads.networking.ingress.signals import cnative_custom_domain_updated +from paasng.platform.applications.constants import ApplicationType from paasng.platform.applications.models import ModuleEnvironment +from paasng.platform.applications.signals import application_default_module_switch + +logger = logging.getLogger(__name__) @receiver(cnative_custom_domain_updated) def on_custom_domain_updated(sender, env: ModuleEnvironment, **kwargs): """Trigger a new networking deploy.""" deploy_networking(env) + + +@receiver(application_default_module_switch) +def sync_default_entrances_for_cnative_module_switching(sender, application, new_module, old_module, **kwargs): + """sync module's default domains and subpaths after switching default module for cloud native app""" + if application.type != ApplicationType.CLOUD_NATIVE: + return + + for module in [old_module, new_module]: + for env in module.envs.all(): + try: + logger.info(f"Refreshing default entrances for {application.code}/{env.environment}/{module.name}...") + if res := get_mres_from_cluster(env): + sync_networking(env, res) + except Exception: + logger.exception( + f"Error syncing default entrances for {application.code}/{env.environment}/{module.name}" + ) diff --git a/apiserver/paasng/paas_wl/workloads/networking/entrance/handlers.py b/apiserver/paasng/paas_wl/workloads/networking/entrance/handlers.py index 15f0f44e7b..d0cfd14a3d 100644 --- a/apiserver/paasng/paas_wl/workloads/networking/entrance/handlers.py +++ b/apiserver/paasng/paas_wl/workloads/networking/entrance/handlers.py @@ -21,6 +21,7 @@ from paas_wl.core.env import env_is_running from paas_wl.workloads.networking.ingress.shim import sync_subdomains, sync_subpaths +from paasng.platform.applications.constants import ApplicationType from paasng.platform.applications.signals import application_default_module_switch from paasng.platform.modules.models import Module @@ -29,7 +30,10 @@ @receiver(application_default_module_switch) def sync_default_entrances_for_module_switching(sender, application, new_module, old_module, **kwargs): - """sync module's default domains and subpaths after switching default module""" + """sync module's default domains and subpaths after switching default module for non cloud native app""" + if application.type == ApplicationType.CLOUD_NATIVE: + return + for module in [old_module, new_module]: try: logger.info(f"Refreshing domains and subpaths for {application.code}/{module.name}...") diff --git a/apiserver/paasng/tests/paasng/accessories/publish/entrance/test_triggers.py b/apiserver/paasng/tests/paasng/accessories/publish/entrance/test_triggers.py index 8422610cdf..2e313b2553 100644 --- a/apiserver/paasng/tests/paasng/accessories/publish/entrance/test_triggers.py +++ b/apiserver/paasng/tests/paasng/accessories/publish/entrance/test_triggers.py @@ -19,6 +19,8 @@ import pytest +from paas_wl.bk_app.cnative.specs.crd.bk_app import BkAppResource +from paas_wl.bk_app.cnative.specs.handlers import sync_default_entrances_for_cnative_module_switching from paas_wl.workloads.networking.entrance.handlers import sync_default_entrances_for_module_switching pytestmark = pytest.mark.django_db @@ -30,3 +32,13 @@ def test_sync_default_entrances_for_module_switching(mocker_subpath, mocker_doma sync_default_entrances_for_module_switching(None, bk_app, bk_module, bk_module) assert mocker_domain.called, "should refresh domains" assert mocker_subpath.called, "should refresh subpaths" + + +@mock.patch("paas_wl.bk_app.cnative.specs.handlers.sync_networking") +def test_sync_default_entrances_for_cnative_module_switching(sync_networking, bk_cnative_app, bk_module): + with mock.patch( + "paas_wl.bk_app.cnative.specs.handlers.get_mres_from_cluster", + return_value=BkAppResource(metadata={"name": "foo"}, spec={}), + ): + sync_default_entrances_for_cnative_module_switching(None, bk_cnative_app, bk_module, bk_module) + assert sync_networking.called, "should sync networking"