mirror of
https://github.com/alibaba/higress.git
synced 2026-06-26 02:35:02 +08:00
support take effect on api level (#1150)
This commit is contained in:
@@ -30,6 +30,7 @@ const (
|
|||||||
Route Category = iota
|
Route Category = iota
|
||||||
Host
|
Host
|
||||||
Service
|
Service
|
||||||
|
RoutePrefix
|
||||||
)
|
)
|
||||||
|
|
||||||
type MatchType int
|
type MatchType int
|
||||||
@@ -45,6 +46,7 @@ const (
|
|||||||
MATCH_ROUTE_KEY = "_match_route_"
|
MATCH_ROUTE_KEY = "_match_route_"
|
||||||
MATCH_DOMAIN_KEY = "_match_domain_"
|
MATCH_DOMAIN_KEY = "_match_domain_"
|
||||||
MATCH_SERVICE_KEY = "_match_service_"
|
MATCH_SERVICE_KEY = "_match_service_"
|
||||||
|
MATCH_ROUTE_PREFIX_KEY = "_match_route_prefix_"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HostMatcher struct {
|
type HostMatcher struct {
|
||||||
@@ -56,6 +58,7 @@ type RuleConfig[PluginConfig any] struct {
|
|||||||
category Category
|
category Category
|
||||||
routes map[string]struct{}
|
routes map[string]struct{}
|
||||||
services map[string]struct{}
|
services map[string]struct{}
|
||||||
|
routePrefixs map[string]struct{}
|
||||||
hosts []HostMatcher
|
hosts []HostMatcher
|
||||||
config PluginConfig
|
config PluginConfig
|
||||||
}
|
}
|
||||||
@@ -92,6 +95,14 @@ func (m RuleMatcher[PluginConfig]) GetMatchConfig() (*PluginConfig, error) {
|
|||||||
return &rule.config, nil
|
return &rule.config, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// category == RoutePrefix
|
||||||
|
if rule.category == RoutePrefix {
|
||||||
|
for routePrefix := range rule.routePrefixs {
|
||||||
|
if strings.HasPrefix(string(routeName), routePrefix) {
|
||||||
|
return &rule.config, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// category == Cluster
|
// category == Cluster
|
||||||
if m.serviceMatch(rule, string(serviceName)) {
|
if m.serviceMatch(rule, string(serviceName)) {
|
||||||
return &rule.config, nil
|
return &rule.config, nil
|
||||||
@@ -152,18 +163,22 @@ func (m *RuleMatcher[PluginConfig]) ParseRuleConfig(config gjson.Result,
|
|||||||
rule.routes = m.parseRouteMatchConfig(ruleJson)
|
rule.routes = m.parseRouteMatchConfig(ruleJson)
|
||||||
rule.hosts = m.parseHostMatchConfig(ruleJson)
|
rule.hosts = m.parseHostMatchConfig(ruleJson)
|
||||||
rule.services = m.parseServiceMatchConfig(ruleJson)
|
rule.services = m.parseServiceMatchConfig(ruleJson)
|
||||||
|
rule.routePrefixs = m.parseRoutePrefixMatchConfig(ruleJson)
|
||||||
noRoute := len(rule.routes) == 0
|
noRoute := len(rule.routes) == 0
|
||||||
noHosts := len(rule.hosts) == 0
|
noHosts := len(rule.hosts) == 0
|
||||||
noService := len(rule.services) == 0
|
noService := len(rule.services) == 0
|
||||||
if boolToInt(noRoute)+boolToInt(noService)+boolToInt(noHosts) != 2 {
|
noRoutePrefix := len(rule.routePrefixs) == 0
|
||||||
return errors.New("there is only one of '_match_route_', '_match_domain_' and '_match_service_' can present in configuration.")
|
if boolToInt(noRoute)+boolToInt(noService)+boolToInt(noHosts)+boolToInt(noRoutePrefix) != 3 {
|
||||||
|
return errors.New("there is only one of '_match_route_', '_match_domain_', '_match_service_' and '_match_route_prefix_' can present in configuration.")
|
||||||
}
|
}
|
||||||
if !noRoute {
|
if !noRoute {
|
||||||
rule.category = Route
|
rule.category = Route
|
||||||
} else if !noHosts {
|
} else if !noHosts {
|
||||||
rule.category = Host
|
rule.category = Host
|
||||||
} else {
|
} else if !noService {
|
||||||
rule.category = Service
|
rule.category = Service
|
||||||
|
} else {
|
||||||
|
rule.category = RoutePrefix
|
||||||
}
|
}
|
||||||
m.ruleConfig = append(m.ruleConfig, rule)
|
m.ruleConfig = append(m.ruleConfig, rule)
|
||||||
}
|
}
|
||||||
@@ -182,6 +197,18 @@ func (m RuleMatcher[PluginConfig]) parseRouteMatchConfig(config gjson.Result) ma
|
|||||||
return routes
|
return routes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m RuleMatcher[PluginConfig]) parseRoutePrefixMatchConfig(config gjson.Result) map[string]struct{} {
|
||||||
|
keys := config.Get(MATCH_ROUTE_PREFIX_KEY).Array()
|
||||||
|
routePrefixs := make(map[string]struct{})
|
||||||
|
for _, item := range keys {
|
||||||
|
routePrefix := item.String()
|
||||||
|
if routePrefix != "" {
|
||||||
|
routePrefixs[routePrefix] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return routePrefixs
|
||||||
|
}
|
||||||
|
|
||||||
func (m RuleMatcher[PluginConfig]) parseServiceMatchConfig(config gjson.Result) map[string]struct{} {
|
func (m RuleMatcher[PluginConfig]) parseServiceMatchConfig(config gjson.Result) map[string]struct{} {
|
||||||
keys := config.Get(MATCH_SERVICE_KEY).Array()
|
keys := config.Get(MATCH_SERVICE_KEY).Array()
|
||||||
clusters := make(map[string]struct{})
|
clusters := make(map[string]struct{})
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ func TestParseRuleConfig(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "rules config",
|
name: "rules config",
|
||||||
config: `{"_rules_":[{"_match_domain_":["*.example.com","www.*","*","www.abc.com"],"name":"john", "age":18},{"_match_route_":["test1","test2"],"name":"ann", "age":16},{"_match_service_":["test1.dns","test2.static:8080"],"name":"ann", "age":16}]}`,
|
config: `{"_rules_":[{"_match_domain_":["*.example.com","www.*","*","www.abc.com"],"name":"john", "age":18},{"_match_route_":["test1","test2"],"name":"ann", "age":16},{"_match_service_":["test1.dns","test2.static:8080"],"name":"ann", "age":16},{"_match_route_prefix_":["api1","api2"],"name":"ann", "age":16}]}`,
|
||||||
expected: RuleMatcher[customConfig]{
|
expected: RuleMatcher[customConfig]{
|
||||||
ruleConfig: []RuleConfig[customConfig]{
|
ruleConfig: []RuleConfig[customConfig]{
|
||||||
{
|
{
|
||||||
@@ -254,6 +254,7 @@ func TestParseRuleConfig(t *testing.T) {
|
|||||||
},
|
},
|
||||||
routes: map[string]struct{}{},
|
routes: map[string]struct{}{},
|
||||||
services: map[string]struct{}{},
|
services: map[string]struct{}{},
|
||||||
|
routePrefixs: map[string]struct{}{},
|
||||||
config: customConfig{
|
config: customConfig{
|
||||||
name: "john",
|
name: "john",
|
||||||
age: 18,
|
age: 18,
|
||||||
@@ -266,6 +267,7 @@ func TestParseRuleConfig(t *testing.T) {
|
|||||||
"test2": {},
|
"test2": {},
|
||||||
},
|
},
|
||||||
services: map[string]struct{}{},
|
services: map[string]struct{}{},
|
||||||
|
routePrefixs: map[string]struct{}{},
|
||||||
config: customConfig{
|
config: customConfig{
|
||||||
name: "ann",
|
name: "ann",
|
||||||
age: 16,
|
age: 16,
|
||||||
@@ -273,11 +275,25 @@ func TestParseRuleConfig(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
category: Service,
|
category: Service,
|
||||||
|
routes: map[string]struct{}{},
|
||||||
services: map[string]struct{}{
|
services: map[string]struct{}{
|
||||||
"test1.dns": {},
|
"test1.dns": {},
|
||||||
"test2.static:8080": {},
|
"test2.static:8080": {},
|
||||||
},
|
},
|
||||||
|
routePrefixs: map[string]struct{}{},
|
||||||
|
config: customConfig{
|
||||||
|
name: "ann",
|
||||||
|
age: 16,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
category: RoutePrefix,
|
||||||
routes: map[string]struct{}{},
|
routes: map[string]struct{}{},
|
||||||
|
services: map[string]struct{}{},
|
||||||
|
routePrefixs: map[string]struct{}{
|
||||||
|
"api1": {},
|
||||||
|
"api2": {},
|
||||||
|
},
|
||||||
config: customConfig{
|
config: customConfig{
|
||||||
name: "ann",
|
name: "ann",
|
||||||
age: 16,
|
age: 16,
|
||||||
@@ -294,17 +310,17 @@ func TestParseRuleConfig(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "invalid rule",
|
name: "invalid rule",
|
||||||
config: `{"_rules_":[{"_match_domain_":["*"],"_match_route_":["test"]}]}`,
|
config: `{"_rules_":[{"_match_domain_":["*"],"_match_route_":["test"]}]}`,
|
||||||
errMsg: "there is only one of '_match_route_', '_match_domain_' and '_match_service_' can present in configuration.",
|
errMsg: "there is only one of '_match_route_', '_match_domain_', '_match_service_' and '_match_route_prefix_' can present in configuration.",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid rule",
|
name: "invalid rule",
|
||||||
config: `{"_rules_":[{"_match_domain_":["*"],"_match_service_":["test.dns"]}]}`,
|
config: `{"_rules_":[{"_match_domain_":["*"],"_match_service_":["test.dns"]}]}`,
|
||||||
errMsg: "there is only one of '_match_route_', '_match_domain_' and '_match_service_' can present in configuration.",
|
errMsg: "there is only one of '_match_route_', '_match_domain_', '_match_service_' and '_match_route_prefix_' can present in configuration.",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid rule",
|
name: "invalid rule",
|
||||||
config: `{"_rules_":[{"age":16}]}`,
|
config: `{"_rules_":[{"age":16}]}`,
|
||||||
errMsg: "there is only one of '_match_route_', '_match_domain_' and '_match_service_' can present in configuration.",
|
errMsg: "there is only one of '_match_route_', '_match_domain_', '_match_service_' and '_match_route_prefix_' can present in configuration.",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
@@ -379,6 +395,7 @@ func TestParseOverrideConfig(t *testing.T) {
|
|||||||
"r2": {},
|
"r2": {},
|
||||||
},
|
},
|
||||||
services: map[string]struct{}{},
|
services: map[string]struct{}{},
|
||||||
|
routePrefixs: map[string]struct{}{},
|
||||||
config: completeConfig{
|
config: completeConfig{
|
||||||
consumers: []string{"c1", "c2", "c3"},
|
consumers: []string{"c1", "c2", "c3"},
|
||||||
allow: []string{"c1", "c3"},
|
allow: []string{"c1", "c3"},
|
||||||
|
|||||||
Reference in New Issue
Block a user