From 55c8c69db7e94f0ddb932f3a3346182b274fef6e Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Mon, 30 Oct 2023 21:35:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=94=AF=E6=8C=81=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E7=9A=84=E8=B7=AF=E7=94=B1=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/servicerouter/rulebase/base.go | 31 +++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/plugin/servicerouter/rulebase/base.go b/plugin/servicerouter/rulebase/base.go index 6ebbb321..ea25f2ee 100644 --- a/plugin/servicerouter/rulebase/base.go +++ b/plugin/servicerouter/rulebase/base.go @@ -158,7 +158,7 @@ func (g *RuleBasedInstancesFilter) matchSourceMetadata(ruleMeta map[string]*apim if ruleMetaValue.GetValue().GetValue() == matchAll { continue } - rawMetaValue, exist := g.getRuleMetaValueStr(routeInfo, ruleMetaKey, ruleMetaValue) + rawMetaValue, exist := g.getRuleMetaValueForSource(routeInfo, ruleMetaKey, ruleMetaValue) if !exist { return false, "", nil } @@ -354,7 +354,7 @@ func (g *RuleBasedInstancesFilter) matchDstMetadata(routeInfo *servicerouter.Rou cls = model.NewCluster(svcCache, inCluster) var metaChanged bool for ruleMetaKey, ruleMetaValue := range ruleMeta { - ruleMetaValueStr, exist := g.getRuleMetaValueStr(routeInfo, ruleMetaKey, ruleMetaValue) + ruleMetaValueStr, exist := g.getRuleMetaValueForDest(routeInfo, ruleMetaKey, ruleMetaValue) if !exist { // 首先如果元数据的value无法获取,直接匹配失败 return nil, false, "", nil @@ -421,9 +421,21 @@ func (g *RuleBasedInstancesFilter) matchDstMetadata(routeInfo *servicerouter.Rou return cls, true, "", nil } +// getRuleMetaValueForSource 针对 Source 方向的标签 value 匹配获取 +func (g *RuleBasedInstancesFilter) getRuleMetaValueForSource(routeInfo *servicerouter.RouteInfo, ruleMetaKey string, + ruleMetaValue *apimodel.MatchString) (string, bool) { + return g.getRuleMetaValueStr(routeInfo, ruleMetaKey, ruleMetaValue, false) +} + +// getRuleMetaValueForDest 针对 Destination 方向的标签 value 匹配获取 +func (g *RuleBasedInstancesFilter) getRuleMetaValueForDest(routeInfo *servicerouter.RouteInfo, ruleMetaKey string, + ruleMetaValue *apimodel.MatchString) (string, bool) { + return g.getRuleMetaValueStr(routeInfo, ruleMetaKey, ruleMetaValue, true) +} + // 获取具体用于匹配的元数据的value func (g *RuleBasedInstancesFilter) getRuleMetaValueStr(routeInfo *servicerouter.RouteInfo, ruleMetaKey string, - ruleMetaValue *apimodel.MatchString) (string, bool) { + ruleMetaValue *apimodel.MatchString, forDest bool) (string, bool) { var srcMeta map[string]string if routeInfo.SourceService != nil { srcMeta = routeInfo.SourceService.GetMetadata() @@ -435,10 +447,17 @@ func (g *RuleBasedInstancesFilter) getRuleMetaValueStr(routeInfo *servicerouter. processedRuleMetaValue = ruleMetaValue.GetValue().GetValue() exist = true case apimodel.MatchString_PARAMETER: - if len(srcMeta) == 0 { - exist = false + if forDest { + if len(srcMeta) == 0 { + exist = false + } else { + // 对于参数场景,实例标签的 value 来自 source metadata 中的 value + processedRuleMetaValue, exist = srcMeta[ruleMetaValue.GetValue().GetValue()] + } } else { - processedRuleMetaValue, exist = srcMeta[ruleMetaKey] + // 如果是参数类型,并且当前是针对 Source 方向的标签匹配,默认直接放通 + exist = true + processedRuleMetaValue = matchAll } case apimodel.MatchString_VARIABLE: processedRuleMetaValue, exist = g.getVariable(ruleMetaValue.GetValue().GetValue())