Skip to content

Commit 10726ff

Browse files
committed
fix:服务列表支持服务可见性&修复服务可见性优先级判断
1 parent 3e0f644 commit 10726ff

File tree

5 files changed

+89
-17
lines changed

5 files changed

+89
-17
lines changed

cache/api/types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ type (
267267
// GetRevisionWorker .
268268
GetRevisionWorker() ServiceRevisionWorker
269269
// GetVisibleServicesInOtherNamespace get same service in other namespace and it's visible
270-
GetVisibleServicesInOtherNamespace(name string, namespace string) []*model.Service
270+
// 如果 name == *,表示返回所有对 namespace 可见的服务
271+
// 如果 name 是具体服务,表示返回对 name + namespace 设置了可见的服务
272+
GetVisibleServicesInOtherNamespace(ctx context.Context, name string, namespace string) []*model.Service
271273
}
272274

273275
// ServiceRevisionWorker

cache/service/service.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,15 +637,15 @@ func (sc *serviceCache) updateCl5SidAndNames(service *model.Service) {
637637
}
638638

639639
// GetVisibleServicesInOtherNamespace 查询是否存在别的命名空间下存在名称相同且可见的服务
640-
func (sc *serviceCache) GetVisibleServicesInOtherNamespace(svcName, namespace string) []*model.Service {
640+
func (sc *serviceCache) GetVisibleServicesInOtherNamespace(ctx context.Context, svcName, namespace string) []*model.Service {
641641
ret := make(map[string]*model.Service)
642642
// 根据服务级别的可见性进行查询, 先查询精确匹配
643643
sc.exportServices.ReadRange(func(exportToNs string, services *utils.SyncMap[string, *model.Service]) {
644644
if exportToNs != namespace && exportToNs != types.AllMatched {
645645
return
646646
}
647647
services.ReadRange(func(_ string, svc *model.Service) {
648-
if svc.Name == svcName && svc.Namespace != namespace {
648+
if (svc.Name == svcName || utils.IsMatchAll(svcName)) && svc.Namespace != namespace {
649649
ret[svc.ID] = svc
650650
}
651651
})
@@ -658,11 +658,38 @@ func (sc *serviceCache) GetVisibleServicesInOtherNamespace(svcName, namespace st
658658
if !exactMatch && !allMatch {
659659
return
660660
}
661-
svc := sc.GetServiceByName(svcName, exportNs)
662-
if svc == nil {
663-
return
661+
if utils.IsMatchAll(svcName) {
662+
// 如果是全匹配,那就看下这个命名空间下的所有服务
663+
_, svcs := sc.ListServices(ctx, exportNs)
664+
for i := range svcs {
665+
if len(svcs[i].ExportTo) != 0 {
666+
// 需要在额外判断下 svc 自己可见性设置
667+
_, exactMatch := svcs[i].ExportTo[namespace]
668+
_, allMatch := svcs[i].ExportTo[types.AllMatched]
669+
if !exactMatch && !allMatch {
670+
continue
671+
}
672+
}
673+
674+
ret[svcs[i].ID] = svcs[i]
675+
}
676+
} else {
677+
svc := sc.GetServiceByName(svcName, exportNs)
678+
if svc == nil {
679+
return
680+
}
681+
// 可能 svc 有自己的可见性设置,此处优先级高于 namespace 的可见性设置
682+
if len(svc.ExportTo) != 0 {
683+
// 需要在额外判断下 svc 自己可见性设置
684+
_, exactMatch := svc.ExportTo[namespace]
685+
_, allMatch := svc.ExportTo[namespace]
686+
if !exactMatch && !allMatch {
687+
return
688+
}
689+
}
690+
691+
ret[svc.ID] = svc
664692
}
665-
ret[svc.ID] = svc
666693
})
667694

668695
visibleServices := make([]*model.Service, 0, len(ret))

cache/service/service_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ func Test_serviceCache_GetVisibleServicesInOtherNamespace(t *testing.T) {
650650
})
651651

652652
_, _, _ = svcCache.setServices(serviceList)
653-
visibles := svcCache.GetVisibleServicesInOtherNamespace("service-1", "ns-2")
653+
visibles := svcCache.GetVisibleServicesInOtherNamespace(context.Background(), "service-1", "ns-2")
654654
assert.Equal(t, 1, len(visibles))
655655
assert.Equal(t, "ns-1", visibles[0].Namespace)
656656
})
@@ -707,15 +707,15 @@ func Test_serviceCache_GetVisibleServicesInOtherNamespace(t *testing.T) {
707707
},
708708
})
709709

710-
visibles := svcCache.GetVisibleServicesInOtherNamespace("service-1", "ns-2")
710+
visibles := svcCache.GetVisibleServicesInOtherNamespace(context.Background(), "service-1", "ns-2")
711711
assert.Equal(t, 1, len(visibles))
712712
assert.Equal(t, "ns-1", visibles[0].Namespace)
713713

714-
visibles = svcCache.GetVisibleServicesInOtherNamespace("service-1", "ns-3")
714+
visibles = svcCache.GetVisibleServicesInOtherNamespace(context.Background(), "service-1", "ns-3")
715715
assert.Equal(t, 1, len(visibles))
716716
assert.Equal(t, "ns-1", visibles[0].Namespace)
717717

718-
visibles = svcCache.GetVisibleServicesInOtherNamespace("service-1", "ns-4")
718+
visibles = svcCache.GetVisibleServicesInOtherNamespace(context.Background(), "service-1", "ns-4")
719719
assert.Equal(t, 0, len(visibles))
720720
})
721721

service/client_v1.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,23 @@ func (s *Server) GetServiceWithCache(ctx context.Context, req *apiservice.Servic
176176

177177
if req.GetNamespace().GetValue() != "" {
178178
revision, svcs = s.Cache().Service().ListServices(ctx, req.GetNamespace().GetValue())
179+
// 需要加上服务可见性处理
180+
visibleSvcs := s.caches.Service().GetVisibleServicesInOtherNamespace(ctx, utils.MatchAll, req.GetNamespace().GetValue())
181+
revisions := make([]string, 0, len(visibleSvcs)+1)
182+
revisions = append(revisions, revision)
183+
for i := range visibleSvcs {
184+
revisions = append(revisions, visibleSvcs[i].Revision)
185+
}
186+
if rever, err := cachetypes.CompositeComputeRevision(revisions); err != nil {
187+
// 如果计算失败,直接返回一个新的revision
188+
revision = utils.NewUUID()
189+
} else {
190+
revision = rever
191+
}
192+
svcs = append(svcs, visibleSvcs...)
193+
// 需要重新计算 revison
179194
} else {
195+
// 这里拉的是全部服务实例列表,如果客户端可以发起这个请求,应该是不需要
180196
revision, svcs = s.Cache().Service().ListAllServices(ctx)
181197
}
182198
if revision == "" {
@@ -212,14 +228,14 @@ func (s *Server) ServiceInstancesCache(ctx context.Context, filter *apiservice.D
212228
req *apiservice.Service) *apiservice.DiscoverResponse {
213229

214230
resp := createCommonDiscoverResponse(req, apiservice.DiscoverResponse_INSTANCE)
215-
serviceName := req.GetName().GetValue()
216-
namespaceName := req.GetNamespace().GetValue()
231+
svcName := req.GetName().GetValue()
232+
nsName := req.GetNamespace().GetValue()
217233

218234
// 数据源都来自Cache,这里拿到的service,已经是源服务
219-
aliasFor, visibleServices := s.findVisibleServices(serviceName, namespaceName, req)
235+
aliasFor, visibleServices := s.findVisibleServices(ctx, svcName, nsName, req)
220236
if len(visibleServices) == 0 {
221237
log.Infof("[Server][Service][Instance] not found name(%s) namespace(%s) service",
222-
serviceName, namespaceName)
238+
svcName, nsName)
223239
return api.NewDiscoverInstanceResponse(apimodel.Code_NotFoundResource, req)
224240
}
225241

@@ -273,14 +289,15 @@ func (s *Server) ServiceInstancesCache(ctx context.Context, filter *apiservice.D
273289
return resp
274290
}
275291

276-
func (s *Server) findVisibleServices(serviceName, namespaceName string, req *apiservice.Service) (*model.Service, []*model.Service) {
292+
func (s *Server) findVisibleServices(ctx context.Context, serviceName, namespaceName string,
293+
req *apiservice.Service) (*model.Service, []*model.Service) {
277294
visibleServices := make([]*model.Service, 0, 4)
278295
// 数据源都来自Cache,这里拿到的service,已经是源服务
279296
aliasFor := s.getServiceCache(serviceName, namespaceName)
280297
if aliasFor != nil {
281298
visibleServices = append(visibleServices, aliasFor)
282299
}
283-
ret := s.caches.Service().GetVisibleServicesInOtherNamespace(serviceName, namespaceName)
300+
ret := s.caches.Service().GetVisibleServicesInOtherNamespace(ctx, serviceName, namespaceName)
284301
if len(ret) > 0 {
285302
visibleServices = append(visibleServices, ret...)
286303
}

service/client_v1_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package service
19+
20+
import (
21+
"testing"
22+
)
23+
24+
func TestServer_GetServiceWithCache(t *testing.T) {
25+
// TODO
26+
}

0 commit comments

Comments
 (0)