From 46236116d35d2b6805b4f6c7d9e3d8547ec5f119 Mon Sep 17 00:00:00 2001 From: chengjoey <30427474+chengjoey@users.noreply.github.com> Date: Thu, 12 Sep 2024 09:22:47 +0800 Subject: [PATCH] fix failed to delete `route` due to nil zoneInfo make panic (#6436) --- .../hepa/services/api_policy/impl/impl.go | 4 ++- .../hepa/services/endpoint_api/impl/impl.go | 36 +++++++++++-------- .../hepa/services/openapi_rule/impl/impl.go | 8 +++-- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/internal/tools/orchestrator/hepa/services/api_policy/impl/impl.go b/internal/tools/orchestrator/hepa/services/api_policy/impl/impl.go index 1f82e46af5d..bef14f4e1ed 100644 --- a/internal/tools/orchestrator/hepa/services/api_policy/impl/impl.go +++ b/internal/tools/orchestrator/hepa/services/api_policy/impl/impl.go @@ -896,7 +896,9 @@ func (impl GatewayApiPolicyServiceImpl) SetPackageDefaultPolicyConfig(category, if err != nil { return "", err } - zones = append(zones, *zone) + if zone != nil { + zones = append(zones, *zone) + } } } policyEngine, err := apipolicy.GetPolicyEngine(category) diff --git a/internal/tools/orchestrator/hepa/services/endpoint_api/impl/impl.go b/internal/tools/orchestrator/hepa/services/endpoint_api/impl/impl.go index f62a496a6e2..66a71db5111 100644 --- a/internal/tools/orchestrator/hepa/services/endpoint_api/impl/impl.go +++ b/internal/tools/orchestrator/hepa/services/endpoint_api/impl/impl.go @@ -1009,7 +1009,7 @@ func (impl GatewayOpenapiServiceImpl) UpdatePackage(orgId, id string, dto *gw.Pa return } //老类型兼容 - if z.Type == db.ZONE_TYPE_PACKAGE { + if z != nil && z.Type == db.ZONE_TYPE_PACKAGE { _, err = (*impl.zoneBiz).UpdateZoneRoute(dao.ZoneId, zone.ZoneRoute{ zone.RouteConfig{ Hosts: domains, @@ -2116,9 +2116,11 @@ func (impl GatewayOpenapiServiceImpl) TouchPackageApiZone(info endpoint_api.Pack if err != nil { return "", err } - externalSvc, err = impl.createOrUpdateService(k8sAdapter, info, *z) - if err != nil { - return "", err + if z != nil { + externalSvc, err = impl.createOrUpdateService(k8sAdapter, info, *z) + if err != nil { + return "", err + } } } } @@ -2133,9 +2135,11 @@ func (impl GatewayOpenapiServiceImpl) TouchPackageApiZone(info endpoint_api.Pack if err != nil { return "", err } - err = (*impl.policyBiz).RefreshZoneIngress(*z, *az, runtimeService.ProjectNamespace, useKong) - if err != nil { - return "", err + if z != nil { + err = (*impl.policyBiz).RefreshZoneIngress(*z, *az, runtimeService.ProjectNamespace, useKong) + if err != nil { + return "", err + } } } return info.ZoneId, nil @@ -2900,14 +2904,16 @@ func (impl GatewayOpenapiServiceImpl) deleteMSEApi(dao *orm.GatewayPackageApi, p if err != nil { return err } - var k8sAdapter k8s.K8SAdapter - k8sAdapter, err = k8s.NewAdapter(pack.DiceClusterName) - if err != nil { - return err - } - err = k8sAdapter.DeleteService(ingressNamespace, strings.ToLower(zone.Name)) - if err != nil { - return err + if zone != nil { + var k8sAdapter k8s.K8SAdapter + k8sAdapter, err = k8s.NewAdapter(pack.DiceClusterName) + if err != nil { + return err + } + err = k8sAdapter.DeleteService(ingressNamespace, strings.ToLower(zone.Name)) + if err != nil { + return err + } } } return nil diff --git a/internal/tools/orchestrator/hepa/services/openapi_rule/impl/impl.go b/internal/tools/orchestrator/hepa/services/openapi_rule/impl/impl.go index 621a945cc3a..58bb6faf4ca 100644 --- a/internal/tools/orchestrator/hepa/services/openapi_rule/impl/impl.go +++ b/internal/tools/orchestrator/hepa/services/openapi_rule/impl/impl.go @@ -263,7 +263,7 @@ func (impl GatewayOpenapiRuleServiceImpl) setPackageApiKongPolicies(basePriority if err != nil { return false, err } - if len(zoneInfo.KongPolicies) > 0 { + if zoneInfo != nil && len(zoneInfo.KongPolicies) > 0 { // clear err = (*impl.zoneBiz).SetZoneKongPoliciesWithoutDomainPolicy(packageApi.ZoneId, nil, helper) if err != nil { @@ -812,11 +812,13 @@ func (impl GatewayOpenapiRuleServiceImpl) pluginReq(gatewayProvider string, dto } if gatewayProvider == mseCommon.MseProviderName && api.ZoneId != "" { - zone, err := (*impl.zoneBiz).GetZone(api.ZoneId) + z, err := (*impl.zoneBiz).GetZone(api.ZoneId) if err != nil { return nil, err } - reqDto.ZoneName = strings.ToLower(zone.Name) + if z != nil { + reqDto.ZoneName = strings.ToLower(z.Name) + } } } return reqDto, nil