Skip to content

Commit

Permalink
fix: apply error ingress when cnative app switching default module (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgetx authored Dec 5, 2024
1 parent 268d52e commit 1943fb7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
26 changes: 25 additions & 1 deletion apiserver/paasng/paas_wl/bk_app/cnative/specs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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}...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

0 comments on commit 1943fb7

Please sign in to comment.