@@ -23,6 +23,7 @@ import (
23
23
"sync"
24
24
25
25
"github.com/polarismesh/polaris/common/model"
26
+ "github.com/polarismesh/polaris/common/utils"
26
27
"github.com/polarismesh/polaris/store"
27
28
)
28
29
@@ -34,10 +35,12 @@ type ServiceArgs struct {
34
35
Metadata map [string ]string
35
36
// SvcIds 是否按照服务的ID进行等值查询
36
37
SvcIds map [string ]struct {}
37
- // FuzzyName 是否进行名字的模糊匹配
38
- FuzzyName bool
39
- // FuzzyBusiness 是否进行业务的模糊匹配
40
- FuzzyBusiness bool
38
+ // WildName 是否进行名字的模糊匹配
39
+ WildName bool
40
+ // WildBusiness 是否进行业务的模糊匹配
41
+ WildBusiness bool
42
+ // WildNamespace 是否进行命名空间的模糊匹配
43
+ WildNamespace bool
41
44
// Namespace 条件中的命名空间
42
45
Namespace string
43
46
// Name 条件中的服务名
@@ -68,7 +71,7 @@ func (sc *serviceCache) GetServicesByFilter(serviceFilters *ServiceArgs,
68
71
var services []* model.Service
69
72
70
73
// 如果具有名字条件,并且不是模糊查询,直接获取对应命名空间下面的服务,并检查是否匹配所有条件
71
- if serviceFilters .Name != "" && ! serviceFilters .FuzzyName {
74
+ if serviceFilters .Name != "" && ! serviceFilters .WildName && ! serviceFilters . WildNamespace {
72
75
amount , services , err = sc .getServicesFromCacheByName (serviceFilters , instanceFilters , offset , limit )
73
76
} else {
74
77
amount , services , err = sc .getServicesByIteratingCache (serviceFilters , instanceFilters , offset , limit )
@@ -90,7 +93,8 @@ func (sc *serviceCache) GetServicesByFilter(serviceFilters *ServiceArgs,
90
93
}
91
94
92
95
func hasInstanceFilter (instanceFilters * store.InstanceArgs ) bool {
93
- if instanceFilters == nil || (len (instanceFilters .Hosts ) == 0 && len (instanceFilters .Ports ) == 0 ) {
96
+ if instanceFilters == nil || (len (instanceFilters .Hosts ) == 0 && len (instanceFilters .Ports ) == 0 &&
97
+ len (instanceFilters .Meta ) == 0 ) {
94
98
return false
95
99
}
96
100
return true
@@ -116,6 +120,27 @@ func (sc *serviceCache) matchInstances(instances []*model.Instance, instanceFilt
116
120
matchedHost = true
117
121
}
118
122
123
+ matchedMeta := false
124
+ if len (instanceFilters .Meta ) > 0 {
125
+ for _ , instance := range instances {
126
+ instanceMetaMap := instance .Metadata ()
127
+ instanceMatched := true
128
+ for key , metaPattern := range instanceFilters .Meta {
129
+ if instanceMetaValue , ok := instanceMetaMap [key ]; ! ok ||
130
+ ! utils .IsWildMatch (instanceMetaValue , metaPattern ) {
131
+ instanceMatched = false
132
+ break
133
+ }
134
+ }
135
+ if instanceMatched {
136
+ matchedMeta = true
137
+ break
138
+ }
139
+ }
140
+ } else {
141
+ matchedMeta = true
142
+ }
143
+
119
144
var matchedPort bool
120
145
if len (instanceFilters .Ports ) > 0 {
121
146
var ports = make (map [uint32 ]bool , len (instanceFilters .Ports ))
@@ -131,7 +156,7 @@ func (sc *serviceCache) matchInstances(instances []*model.Instance, instanceFilt
131
156
} else {
132
157
matchedPort = true
133
158
}
134
- return matchedHost && matchedPort
159
+ return matchedHost && matchedPort && matchedMeta
135
160
}
136
161
137
162
// GetAllNamespaces 返回所有的命名空间
@@ -150,14 +175,14 @@ func (sc *serviceCache) getServicesFromCacheByName(svcArgs *ServiceArgs, instArg
150
175
var res []* model.Service
151
176
if svcArgs .Namespace != "" {
152
177
svc := sc .GetServiceByName (svcArgs .Name , svcArgs .Namespace )
153
- if svc != nil && ! svc .IsAlias () && matchService (svc , svcArgs .Filter , svcArgs .Metadata , false ) &&
178
+ if svc != nil && ! svc .IsAlias () && matchService (svc , svcArgs .Filter , svcArgs .Metadata , false , false ) &&
154
179
sc .matchInstance (svc , instArgs ) {
155
180
res = append (res , svc )
156
181
}
157
182
} else {
158
183
for _ , namespace := range sc .GetAllNamespaces () {
159
184
svc := sc .GetServiceByName (svcArgs .Name , namespace )
160
- if svc != nil && ! svc .IsAlias () && matchService (svc , svcArgs .Filter , svcArgs .Metadata , false ) &&
185
+ if svc != nil && ! svc .IsAlias () && matchService (svc , svcArgs .Filter , svcArgs .Metadata , false , false ) &&
161
186
sc .matchInstance (svc , instArgs ) {
162
187
res = append (res , svc )
163
188
}
@@ -196,26 +221,33 @@ func sortBeforeTrim(services []*model.Service, offset, limit uint32) (uint32, []
196
221
}
197
222
198
223
// matchService 根据查询条件比较一个服务是否符合条件
199
- func matchService (svc * model.Service , svcFilter map [string ]string , metaFilter map [string ]string , matchName bool ) bool {
200
- if ! matchServiceFilter (svc , svcFilter , matchName ) {
224
+ func matchService (svc * model.Service , svcFilter map [string ]string , metaFilter map [string ]string ,
225
+ isWildName , isWildNamespace bool ) bool {
226
+ if ! matchServiceFilter (svc , svcFilter , isWildName , isWildNamespace ) {
201
227
return false
202
228
}
203
229
return matchMetadata (svc , metaFilter )
204
230
}
205
231
206
232
// matchServiceFilter 查询一个服务是否满足服务相关字段的条件
207
- func matchServiceFilter (svc * model.Service , svcFilter map [string ]string , matchName bool ) bool {
233
+ func matchServiceFilter (svc * model.Service , svcFilter map [string ]string , isWildName , isWildNamespace bool ) bool {
208
234
var value string
209
235
var exist bool
210
- if matchName {
211
- // 走到这一步,一定是模糊匹配
236
+ if isWildName {
212
237
if value , exist = svcFilter ["name" ]; exist {
213
- searchVal := value [0 : len (value )- 1 ]
214
- if ! strings .Contains (strings .ToLower (svc .Name ), strings .ToLower (searchVal )) {
238
+ if ! utils .IsWildMatchIgnoreCase (svc .Name , value ) {
239
+ return false
240
+ }
241
+ }
242
+ }
243
+ if isWildNamespace {
244
+ if value , exist = svcFilter ["namespace" ]; exist {
245
+ if ! utils .IsWildMatchIgnoreCase (svc .Namespace , value ) {
215
246
return false
216
247
}
217
248
}
218
249
}
250
+
219
251
if value , exist = svcFilter ["business" ]; exist &&
220
252
! strings .Contains (strings .ToLower (svc .Business ), strings .ToLower (value )) {
221
253
return false
@@ -272,7 +304,7 @@ func (sc *serviceCache) getServicesByIteratingCache(
272
304
return
273
305
}
274
306
if ! svcArgs .EmptyCondition {
275
- if ! matchService (svc , svcArgs .Filter , svcArgs .Metadata , true ) {
307
+ if ! matchService (svc , svcArgs .Filter , svcArgs .Metadata , svcArgs . WildName , svcArgs . WildNamespace ) {
276
308
return
277
309
}
278
310
}
@@ -281,7 +313,7 @@ func (sc *serviceCache) getServicesByIteratingCache(
281
313
}
282
314
res = append (res , svc )
283
315
}
284
- if len (svcArgs .Namespace ) > 0 {
316
+ if len (svcArgs .Namespace ) > 0 && ! svcArgs . WildNamespace {
285
317
// 从命名空间来找
286
318
spaces , ok := sc .names .Load (svcArgs .Namespace )
287
319
if ! ok {
0 commit comments