From 8610ed01d589a30256e51a0ffca8a8edb61eb7c2 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sun, 25 Sep 2022 16:35:01 +0800 Subject: [PATCH 01/10] docs:add error code desc --- release/conf/i18n/en.toml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/release/conf/i18n/en.toml b/release/conf/i18n/en.toml index 5236d4dcc..b0fa36774 100644 --- a/release/conf/i18n/en.toml +++ b/release/conf/i18n/en.toml @@ -156,11 +156,19 @@ 401002 = "auth token empty" #EmptyAutToken 401003 = "token already disabled" #TokenDisabled 401004 = "token not existed" #TokenNotExisted +<<<<<<< HEAD 403001 = "token verify exception" #AuthTokenVerifyException 403002 = "operation role exception" #OperationRoleException 404001 = "not found the host cmdb" #CMDBNotFindHost 409000 = "data is conflict, please try again" #DataConflict 429001 = "instance has too many requests" #InstanceTooManyRequests +======= +403001 = "server limit the ip access" #IPRateLimit +403002 = "server limit the api access" #APIRateLimit +404001 = "not found the host cmdb" #CMDBNotFindHost +409000 = "data is conflict, please try again" #DataConflict +429001 = "your instance has too many requests" #InstanceTooManyRequests +>>>>>>> c07080f0... docs:add error code desc 500000 = "execute exception" #ExecuteException 500001 = "store layer exception" #StoreLayerException 500002 = "cmdb plugin exception" #CMDBPluginException @@ -169,3 +177,8 @@ 500006 = "parse circuit breaker failed" #ParseCircuitBreakerException 500007 = "heartbeat execute exception" #HeartbeatException 500008 = "instance async regist timeout" #InstanceRegisTimeout +<<<<<<< HEAD +======= +500100 = "token verify exception" #AuthTokenVerifyException +500101 = "operation role exception" #OperationRoleException +>>>>>>> c07080f0... docs:add error code desc From fa2117d3ce56966c1b07a47810ad38c730fa18ff Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Wed, 19 Oct 2022 16:00:33 +0800 Subject: [PATCH 02/10] =?UTF-8?q?fix:=E8=B0=83=E6=95=B4license-checker?= =?UTF-8?q?=E7=9A=84=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/golangci-lint.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 394cb4df3..4fa69ac1e 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -15,10 +15,6 @@ name: golangci-lint on: - push: - branches: - - main - - release* pull_request: branches: - main From a6b75ad87dafafce801ab49507e5603d69c48caf Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Wed, 19 Oct 2022 17:08:41 +0800 Subject: [PATCH 03/10] =?UTF-8?q?fix:=E8=B0=83=E6=95=B4license-checker?= =?UTF-8?q?=E7=9A=84=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/golangci-lint.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 4fa69ac1e..394cb4df3 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -15,6 +15,10 @@ name: golangci-lint on: + push: + branches: + - main + - release* pull_request: branches: - main From ea1918e923f4cd8080ed02b40b2cdd80e093ee9f Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Tue, 27 Jun 2023 11:58:15 +0800 Subject: [PATCH 04/10] =?UTF-8?q?hotfix:=E4=BF=AE=E5=A4=8D=E9=89=B4?= =?UTF-8?q?=E6=9D=83interceptor=E9=81=97=E6=BC=8F=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=9D=A5=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- release/conf/i18n/en.toml | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/release/conf/i18n/en.toml b/release/conf/i18n/en.toml index b0fa36774..5236d4dcc 100644 --- a/release/conf/i18n/en.toml +++ b/release/conf/i18n/en.toml @@ -156,19 +156,11 @@ 401002 = "auth token empty" #EmptyAutToken 401003 = "token already disabled" #TokenDisabled 401004 = "token not existed" #TokenNotExisted -<<<<<<< HEAD 403001 = "token verify exception" #AuthTokenVerifyException 403002 = "operation role exception" #OperationRoleException 404001 = "not found the host cmdb" #CMDBNotFindHost 409000 = "data is conflict, please try again" #DataConflict 429001 = "instance has too many requests" #InstanceTooManyRequests -======= -403001 = "server limit the ip access" #IPRateLimit -403002 = "server limit the api access" #APIRateLimit -404001 = "not found the host cmdb" #CMDBNotFindHost -409000 = "data is conflict, please try again" #DataConflict -429001 = "your instance has too many requests" #InstanceTooManyRequests ->>>>>>> c07080f0... docs:add error code desc 500000 = "execute exception" #ExecuteException 500001 = "store layer exception" #StoreLayerException 500002 = "cmdb plugin exception" #CMDBPluginException @@ -177,8 +169,3 @@ 500006 = "parse circuit breaker failed" #ParseCircuitBreakerException 500007 = "heartbeat execute exception" #HeartbeatException 500008 = "instance async regist timeout" #InstanceRegisTimeout -<<<<<<< HEAD -======= -500100 = "token verify exception" #AuthTokenVerifyException -500101 = "operation role exception" #OperationRoleException ->>>>>>> c07080f0... docs:add error code desc From 0f2b17306e6b8a4edfef11c54887bf4dedf4edc5 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Fri, 5 Jan 2024 15:31:07 +0800 Subject: [PATCH 05/10] codecov:1.18.0 --- config/client_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/config/client_test.go b/config/client_test.go index 43c77792c..e428fae80 100644 --- a/config/client_test.go +++ b/config/client_test.go @@ -24,17 +24,16 @@ import ( "testing" "time" - apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage" - apimodel "github.com/polarismesh/specification/source/go/api/v1/model" - "github.com/stretchr/testify/assert" - "google.golang.org/protobuf/types/known/wrapperspb" - api "github.com/polarismesh/polaris/common/api/v1" commonlog "github.com/polarismesh/polaris/common/log" "github.com/polarismesh/polaris/common/model" "github.com/polarismesh/polaris/common/utils" "github.com/polarismesh/polaris/config" testsuit "github.com/polarismesh/polaris/test/suit" + apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage" + apimodel "github.com/polarismesh/specification/source/go/api/v1/model" + "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/types/known/wrapperspb" ) // TestClientSetupAndFileNotExisted 测试客户端启动时(version=0),并且配置不存在的情况下拉取配置 From 25c3eebd96d80d11b3bd2298c771de23eaa213b9 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sat, 6 Jan 2024 11:06:42 +0800 Subject: [PATCH 06/10] codecov:1.18.0 --- config/client_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/config/client_test.go b/config/client_test.go index e428fae80..43c77792c 100644 --- a/config/client_test.go +++ b/config/client_test.go @@ -24,16 +24,17 @@ import ( "testing" "time" + apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage" + apimodel "github.com/polarismesh/specification/source/go/api/v1/model" + "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/types/known/wrapperspb" + api "github.com/polarismesh/polaris/common/api/v1" commonlog "github.com/polarismesh/polaris/common/log" "github.com/polarismesh/polaris/common/model" "github.com/polarismesh/polaris/common/utils" "github.com/polarismesh/polaris/config" testsuit "github.com/polarismesh/polaris/test/suit" - apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage" - apimodel "github.com/polarismesh/specification/source/go/api/v1/model" - "github.com/stretchr/testify/assert" - "google.golang.org/protobuf/types/known/wrapperspb" ) // TestClientSetupAndFileNotExisted 测试客户端启动时(version=0),并且配置不存在的情况下拉取配置 From 19ba2cf89ef5b9dbebaa0e324dc8e7a8dc52f0c8 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Mon, 8 Jan 2024 10:33:25 +0800 Subject: [PATCH 07/10] codecov:1.18.0 --- config/server.go | 37 +++++++++++++++++++++++++------------ config/server_test.go | 11 +++++------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/config/server.go b/config/server.go index 3aaa72ef9..9e9413e94 100644 --- a/config/server.go +++ b/config/server.go @@ -89,21 +89,34 @@ type Server struct { // Initialize 初始化配置中心模块 func Initialize(ctx context.Context, config Config, s store.Store, cacheMgr cachetypes.CacheManager, namespaceOperator namespace.NamespaceOperateServer) error { - if !config.Open { - originServer.initialized = true + if originServer.initialized { return nil } + proxySvr, originSvr, err := doInitialize(ctx, config, s, cacheMgr, namespaceOperator) + if err != nil { + return err + } + originServer = originSvr + server = proxySvr + return nil +} - if originServer.initialized { - return nil +func doInitialize(ctx context.Context, config Config, s store.Store, cacheMgr cachetypes.CacheManager, + namespaceOperator namespace.NamespaceOperateServer) (ConfigCenterServer, *Server, error) { + if !config.Open { + originServer.initialized = true + return nil, nil, nil } + var proxySvr ConfigCenterServer + originSvr := &Server{} + if err := cacheMgr.OpenResourceCache(configCacheEntries...); err != nil { - return err + return nil, nil, err } err := originServer.initialize(ctx, config, s, namespaceOperator, cacheMgr) if err != nil { - return err + return nil, nil, err } // 需要返回包装代理的 DiscoverServer @@ -111,18 +124,18 @@ func Initialize(ctx context.Context, config Config, s store.Store, cacheMgr cach for i := range order { factory, exist := serverProxyFactories[order[i]] if !exist { - return fmt.Errorf("name(%s) not exist in serverProxyFactories", order[i]) + return nil, nil, fmt.Errorf("name(%s) not exist in serverProxyFactories", order[i]) } - proxySvr, err := factory(originServer, server) + tmpSvr, err := factory(originServer, server) if err != nil { - return err + return nil, nil, err } - server = proxySvr + proxySvr = tmpSvr } - originServer.initialized = true - return nil + originSvr.initialized = true + return proxySvr, originSvr, nil } func (s *Server) initialize(ctx context.Context, config Config, ss store.Store, diff --git a/config/server_test.go b/config/server_test.go index f9d29fdf1..ff0061c30 100644 --- a/config/server_test.go +++ b/config/server_test.go @@ -37,10 +37,6 @@ func Test_Initialize(t *testing.T) { t.Cleanup(func() { ctrl.Finish() - originServer.watchCenter.Close() - originServer.initialized = false - originServer = nil - server = nil }) cacheMgr.EXPECT().OpenResourceCache(gomock.Any()).Return(nil).AnyTimes() @@ -48,9 +44,12 @@ func Test_Initialize(t *testing.T) { cacheMgr.EXPECT().Gray().Return(nil).AnyTimes() cacheMgr.EXPECT().ConfigGroup().Return(nil).AnyTimes() - err := Initialize(context.Background(), Config{ + proxySvr, originSvr, err := doInitialize(context.Background(), Config{ Open: true, }, mockStore, cacheMgr, nil) assert.NoError(t, err) - assert.NotNil(t, originServer) + assert.NotNil(t, originSvr) + assert.NotNil(t, proxySvr) + + originSvr.watchCenter.Close() } From fd482615bed23a01f310724decc4ebe0bf28f54b Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Mon, 8 Jan 2024 18:28:40 +0800 Subject: [PATCH 08/10] codecov:1.18.0 --- .../grpcserver/discover/v1/client_access.go | 3 + apiserver/xdsserverv3/resource/help.go | 16 +-- common/metrics/types.go | 21 ++-- common/model/contract.go | 35 +++++- go.mod | 2 +- go.sum | 4 +- service/api_v1.go | 2 + service/client_v1.go | 116 ++++++++++++++++++ .../interceptor/auth/client_v1_authability.go | 21 ++++ 9 files changed, 193 insertions(+), 27 deletions(-) diff --git a/apiserver/grpcserver/discover/v1/client_access.go b/apiserver/grpcserver/discover/v1/client_access.go index 14ee828a1..30b967c73 100644 --- a/apiserver/grpcserver/discover/v1/client_access.go +++ b/apiserver/grpcserver/discover/v1/client_access.go @@ -163,6 +163,9 @@ func (g *DiscoverServer) Discover(server apiservice.PolarisGRPC_DiscoverServer) case apiservice.DiscoverRequest_FAULT_DETECTOR: action = metrics.ActionDiscoverFaultDetect out = g.namingServer.GetFaultDetectWithCache(ctx, in.Service) + case apiservice.DiscoverRequest_SERVICE_CONTRACT: + action = metrics.ActionDiscoverServiceContract + out = g.namingServer.GetServiceContractWithCache(ctx, in.ServiceContract) default: out = api.NewDiscoverRoutingResponse(apimodel.Code_InvalidDiscoverResource, in.Service) } diff --git a/apiserver/xdsserverv3/resource/help.go b/apiserver/xdsserverv3/resource/help.go index ac862116e..699ab05fb 100644 --- a/apiserver/xdsserverv3/resource/help.go +++ b/apiserver/xdsserverv3/resource/help.go @@ -333,7 +333,7 @@ func BuildRateLimitDescriptors(rule *traffic_manage.Rule) ([]*route.RateLimit_Ac Value: arg.GetValue().GetValue().GetValue(), }) case apitraffic.MatchArgument_QUERY: - queryParameterValueMatch := BuildRateLimitActionQueryParameterValueMatch(descriptorKey, arg.Value) + queryParameterValueMatch := BuildRateLimitActionQueryParameterValueMatch(descriptorKey, arg) actions = append(actions, &route.RateLimit_Action{ ActionSpecifier: &route.RateLimit_Action_QueryParameterValueMatch_{ QueryParameterValueMatch: queryParameterValueMatch, @@ -425,22 +425,22 @@ func BuildRateLimitDescriptors(rule *traffic_manage.Rule) ([]*route.RateLimit_Ac } func BuildRateLimitActionQueryParameterValueMatch(key string, - value *apimodel.MatchString) *route.RateLimit_Action_QueryParameterValueMatch { + arg *apitraffic.MatchArgument) *route.RateLimit_Action_QueryParameterValueMatch { queryParameterValueMatch := &route.RateLimit_Action_QueryParameterValueMatch{ DescriptorKey: key, - DescriptorValue: value.GetValue().GetValue(), + DescriptorValue: arg.GetValue().GetValue().GetValue(), ExpectMatch: wrapperspb.Bool(true), QueryParameters: []*route.QueryParameterMatcher{}, } - switch value.GetType() { + switch arg.GetValue().GetType() { case apimodel.MatchString_EXACT: queryParameterValueMatch.QueryParameters = []*route.QueryParameterMatcher{ { - Name: key, + Name: arg.GetKey(), QueryParameterMatchSpecifier: &route.QueryParameterMatcher_StringMatch{ StringMatch: &v32.StringMatcher{ MatchPattern: &v32.StringMatcher_Exact{ - Exact: value.GetValue().GetValue(), + Exact: arg.GetValue().GetValue().GetValue(), }, }, }, @@ -449,13 +449,13 @@ func BuildRateLimitActionQueryParameterValueMatch(key string, case apimodel.MatchString_REGEX: queryParameterValueMatch.QueryParameters = []*route.QueryParameterMatcher{ { - Name: key, + Name: arg.GetKey(), QueryParameterMatchSpecifier: &route.QueryParameterMatcher_StringMatch{ StringMatch: &v32.StringMatcher{ MatchPattern: &v32.StringMatcher_SafeRegex{ SafeRegex: &v32.RegexMatcher{ EngineType: &v32.RegexMatcher_GoogleRe2{}, - Regex: value.GetValue().GetValue(), + Regex: arg.GetValue().GetValue().GetValue(), }, }, }, diff --git a/common/metrics/types.go b/common/metrics/types.go index 484ceb6b1..17a6d929e 100644 --- a/common/metrics/types.go +++ b/common/metrics/types.go @@ -116,16 +116,17 @@ func ResourceOfConfigFile(group, name string) string { } const ( - ActionGetConfigFile = "GET_CONFIG_FILE" - ActionListConfigFiles = "LIST_CONFIG_FILES" - ActionListConfigGroups = "LIST_CONFIG_GROUPS" - ActionPublishConfigFile = "PUBLISH_CONFIG_FILE" - ActionDiscoverInstance = "DISCOVER_INSTANCE" - ActionDiscoverServices = "DISCOVER_SERVICES" - ActionDiscoverRouterRule = "DISCOVER_ROUTER_RULE" - ActionDiscoverRateLimit = "DISCOVER_RATE_LIMIT" - ActionDiscoverCircuitBreaker = "DISCOVER_CIRCUIT_BREAKER" - ActionDiscoverFaultDetect = "DISCOVER_FAULT_DETECT" + ActionGetConfigFile = "GET_CONFIG_FILE" + ActionListConfigFiles = "LIST_CONFIG_FILES" + ActionListConfigGroups = "LIST_CONFIG_GROUPS" + ActionPublishConfigFile = "PUBLISH_CONFIG_FILE" + ActionDiscoverInstance = "DISCOVER_INSTANCE" + ActionDiscoverServices = "DISCOVER_SERVICES" + ActionDiscoverRouterRule = "DISCOVER_ROUTER_RULE" + ActionDiscoverRateLimit = "DISCOVER_RATE_LIMIT" + ActionDiscoverCircuitBreaker = "DISCOVER_CIRCUIT_BREAKER" + ActionDiscoverFaultDetect = "DISCOVER_FAULT_DETECT" + ActionDiscoverServiceContract = "DISCOVER_SERVICE_CONTRACT" ) type ClientDiscoverMetric struct { diff --git a/common/model/contract.go b/common/model/contract.go index a5e1f9b46..8d72a7061 100644 --- a/common/model/contract.go +++ b/common/model/contract.go @@ -21,6 +21,7 @@ import ( "fmt" "time" + commontime "github.com/polarismesh/polaris/common/time" apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" ) @@ -89,12 +90,34 @@ func (e *EnrichServiceContract) Format() { } } -type ServiceContractView struct { - *ServiceContract - // 接口描述信息 - Interfaces []*InterfaceDescriptor - ClientInterfaces map[string]*InterfaceDescriptor - ManualInterfaces map[string]*InterfaceDescriptor +func (e *EnrichServiceContract) ToSpec() *apiservice.ServiceContract { + interfaces := make([]*apiservice.InterfaceDescriptor, 0, len(e.Interfaces)) + for i := range e.Interfaces { + item := e.Interfaces[i] + interfaces = append(interfaces, &apiservice.InterfaceDescriptor{ + Id: item.ID, + Path: item.Path, + Method: item.Method, + Source: item.Source, + Content: item.Content, + Revision: item.Revision, + Ctime: commontime.Time2String(item.CreateTime), + Mtime: commontime.Time2String(item.ModifyTime), + }) + } + return &apiservice.ServiceContract{ + Id: e.ID, + Name: e.Name, + Namespace: e.Namespace, + Service: e.Service, + Protocol: e.Protocol, + Version: e.Version, + Revision: e.Revision, + Content: e.Content, + Ctime: commontime.Time2String(e.CreateTime), + Mtime: commontime.Time2String(e.ModifyTime), + Interfaces: interfaces, + } } func (s *ServiceContract) GetResourceName() string { diff --git a/go.mod b/go.mod index 973b5f1f9..5c23dd35e 100644 --- a/go.mod +++ b/go.mod @@ -82,7 +82,7 @@ require ( require ( github.com/DATA-DOG/go-sqlmock v1.5.0 - github.com/polarismesh/specification v1.4.2-alpha.6 + github.com/polarismesh/specification v1.4.2-alpha.7 ) require ( diff --git a/go.sum b/go.sum index ef222bb38..bf8386ec3 100644 --- a/go.sum +++ b/go.sum @@ -323,8 +323,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219 h1:XnFyNUWnciM6zgXaz6tm+Egs35rhoD0KGMmKh4gCdi0= github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219/go.mod h1:4WhwBysTom9Eoy0hQ4W69I0FmO+T0EpjEW9/5sgHoUk= -github.com/polarismesh/specification v1.4.2-alpha.6 h1:EUhATwFjb4lGIrI/UEoaVdyLWpjZrOuDTQn4S12QE10= -github.com/polarismesh/specification v1.4.2-alpha.6/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= +github.com/polarismesh/specification v1.4.2-alpha.7 h1:4BhlGD/xJ/092cuu/T5BgwAMwlPFg8vIMfBNMRyEtak= +github.com/polarismesh/specification v1.4.2-alpha.7/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= diff --git a/service/api_v1.go b/service/api_v1.go index 9c6029415..388631c8e 100644 --- a/service/api_v1.go +++ b/service/api_v1.go @@ -180,6 +180,8 @@ type ClientServer interface { GetCircuitBreakerWithCache(ctx context.Context, req *apiservice.Service) *apiservice.DiscoverResponse // GetFaultDetectWithCache User Client Get FaultDetect Rule Information GetFaultDetectWithCache(ctx context.Context, req *apiservice.Service) *apiservice.DiscoverResponse + // GetServiceContractWithCache User Client Get ServiceContract Rule Information + GetServiceContractWithCache(ctx context.Context, req *apiservice.ServiceContract) *apiservice.DiscoverResponse // UpdateInstance update one instance by client UpdateInstance(ctx context.Context, req *apiservice.Instance) *apiservice.Response // ReportServiceContract client report service_contract diff --git a/service/client_v1.go b/service/client_v1.go index 3311abcf7..32ea4fc32 100644 --- a/service/client_v1.go +++ b/service/client_v1.go @@ -27,6 +27,7 @@ import ( apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage" "go.uber.org/zap" + "google.golang.org/protobuf/types/known/wrapperspb" cachetypes "github.com/polarismesh/polaris/cache/api" api "github.com/polarismesh/polaris/common/api/v1" @@ -493,6 +494,62 @@ func (s *Server) GetCircuitBreakerWithCache(ctx context.Context, req *apiservice return resp } +// GetServiceContractWithCache User Client Get ServiceContract Rule Information +func (s *Server) GetServiceContractWithCache(ctx context.Context, + req *apiservice.ServiceContract) *apiservice.DiscoverResponse { + resp := &apiservice.DiscoverResponse{ + Code: &wrappers.UInt32Value{Value: uint32(apimodel.Code_ExecuteSuccess)}, + Info: &wrappers.StringValue{Value: api.Code2Info(uint32(apimodel.Code_ExecuteSuccess))}, + Type: apiservice.DiscoverResponse_SERVICE_CONTRACT, + Service: &apiservice.Service{ + Name: wrapperspb.String(req.GetService()), + Namespace: wrapperspb.String(req.GetNamespace()), + }, + } + if !s.serviceContractCheckDiscoverRequest(req, resp) { + return resp + } + + // 服务名和request保持一致 + resp.Service = &apiservice.Service{ + Name: wrapperspb.String(req.GetService()), + Namespace: wrapperspb.String(req.GetNamespace()), + } + + // 获取源服务 + aliasFor := s.getServiceCache(req.GetService(), req.GetNamespace()) + if aliasFor == nil { + aliasFor = &model.Service{ + Namespace: req.GetNamespace(), + Name: req.GetService(), + } + } + + out := s.caches.ServiceContract().Get(&model.ServiceContract{ + Namespace: aliasFor.Namespace, + Service: aliasFor.Name, + Version: req.Version, + Name: req.Name, + Protocol: req.Protocol, + }) + if out == nil { + resp.Code = wrapperspb.UInt32(uint32(apimodel.Code_NotFoundResource)) + resp.Info = wrapperspb.String(api.Code2Info(uint32(apimodel.Code_NotFoundResource))) + return resp + } + + // 获取熔断规则数据,并对比revision + if len(req.GetRevision()) > 0 && req.GetRevision() == out.Revision { + resp.Code = wrapperspb.UInt32(uint32(apimodel.Code_DataNoChange)) + resp.Info = wrapperspb.String(api.Code2Info(uint32(apimodel.Code_DataNoChange))) + return resp + } + + resp.Service.Revision = wrapperspb.String(out.Revision) + resp.ServiceContract = out.ToSpec() + return resp +} + func createCommonDiscoverResponse(req *apiservice.Service, dT apiservice.DiscoverResponse_DiscoverResponseType) *apiservice.DiscoverResponse { return &apiservice.DiscoverResponse{ @@ -578,3 +635,62 @@ func (s *Server) commonCheckDiscoverRequest(req *apiservice.Service, resp *apise return true } + +func (s *Server) serviceContractCheckDiscoverRequest(req *apiservice.ServiceContract, resp *apiservice.DiscoverResponse) bool { + svc := &apiservice.Service{ + Name: wrapperspb.String(req.GetService()), + Namespace: wrapperspb.String(req.GetNamespace()), + } + + if s.caches == nil { + resp.Code = utils.NewUInt32Value(uint32(apimodel.Code_ClientAPINotOpen)) + resp.Info = utils.NewStringValue(api.Code2Info(resp.GetCode().GetValue())) + resp.Service = svc + resp.ServiceContract = req + return false + } + if req == nil { + resp.Code = utils.NewUInt32Value(uint32(apimodel.Code_EmptyRequest)) + resp.Info = utils.NewStringValue(api.Code2Info(resp.GetCode().GetValue())) + resp.Service = svc + resp.ServiceContract = req + return false + } + + if req.GetName() == "" { + resp.Code = utils.NewUInt32Value(uint32(apimodel.Code_InvalidParameter)) + resp.Info = utils.NewStringValue(api.Code2Info(resp.GetCode().GetValue())) + resp.Service = svc + resp.ServiceContract = req + return false + } + if req.GetNamespace() == "" { + resp.Code = utils.NewUInt32Value(uint32(apimodel.Code_InvalidNamespaceName)) + resp.Info = utils.NewStringValue(api.Code2Info(resp.GetCode().GetValue())) + resp.Service = svc + resp.ServiceContract = req + return false + } + if req.GetService() == "" { + resp.Code = utils.NewUInt32Value(uint32(apimodel.Code_InvalidServiceName)) + resp.Info = utils.NewStringValue(api.Code2Info(resp.GetCode().GetValue())) + resp.Service = svc + resp.ServiceContract = req + return false + } + if req.GetProtocol() == "" { + resp.Code = utils.NewUInt32Value(uint32(apimodel.Code_InvalidParameter)) + resp.Info = utils.NewStringValue(api.Code2Info(resp.GetCode().GetValue())) + resp.Service = svc + resp.ServiceContract = req + return false + } + if req.GetVersion() == "" { + resp.Code = utils.NewUInt32Value(uint32(apimodel.Code_InvalidParameter)) + resp.Info = utils.NewStringValue(api.Code2Info(resp.GetCode().GetValue())) + resp.Service = svc + resp.ServiceContract = req + return false + } + return true +} diff --git a/service/interceptor/auth/client_v1_authability.go b/service/interceptor/auth/client_v1_authability.go index 9d0ceeea2..248fd7e56 100644 --- a/service/interceptor/auth/client_v1_authability.go +++ b/service/interceptor/auth/client_v1_authability.go @@ -216,3 +216,24 @@ func (svr *ServerAuthAbility) UpdateInstance(ctx context.Context, req *apiservic return svr.targetServer.UpdateInstance(ctx, req) } + +// GetServiceContractWithCache User Client Get ServiceContract Rule Information +func (svr *ServerAuthAbility) GetServiceContractWithCache(ctx context.Context, req *apiservice.ServiceContract) *apiservice.DiscoverResponse { + authCtx := svr.collectServiceAuthContext( + ctx, []*apiservice.Service{&apiservice.Service{ + Namespace: wrapperspb.String(req.Namespace), + Name: wrapperspb.String(req.Service), + }}, model.Read, "GetServiceContractWithCache") + + _, err := svr.strategyMgn.GetAuthChecker().CheckClientPermission(authCtx) + if err != nil { + resp := api.NewDiscoverResponse(convertToErrCode(err)) + resp.Info = utils.NewStringValue(err.Error()) + return resp + } + + ctx = authCtx.GetRequestContext() + ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) + + return svr.targetServer.GetServiceContractWithCache(ctx, req) +} From 70775884333dbf37353968c1408219fd242daffa Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Mon, 8 Jan 2024 22:41:20 +0800 Subject: [PATCH 09/10] codecov:1.18.0 --- common/model/contract.go | 3 ++- config/client_test.go | 2 +- config/server_test.go | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/common/model/contract.go b/common/model/contract.go index 8d72a7061..30ccb6c8a 100644 --- a/common/model/contract.go +++ b/common/model/contract.go @@ -21,8 +21,9 @@ import ( "fmt" "time" - commontime "github.com/polarismesh/polaris/common/time" apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" + + commontime "github.com/polarismesh/polaris/common/time" ) type ServiceContract struct { diff --git a/config/client_test.go b/config/client_test.go index 43c77792c..ba650115f 100644 --- a/config/client_test.go +++ b/config/client_test.go @@ -694,7 +694,7 @@ func TestServer_GetConfigGroupsWithCache(t *testing.T) { // 同一个 revision 查询 rsp = testSuit.ConfigServer().GetConfigGroupsWithCache(testSuit.DefaultCtx, &apiconfig.ClientConfigFileInfo{ Namespace: wrapperspb.String("ns-0"), - Md5: wrapperspb.String(rsp.GetRevision()), + Md5: wrapperspb.String(rsp.GetRevision()), }) assert.Equal(t, uint32(apimodel.Code_DataNoChange), rsp.Code, rsp.Info) diff --git a/config/server_test.go b/config/server_test.go index ff0061c30..66d3a06e2 100644 --- a/config/server_test.go +++ b/config/server_test.go @@ -22,10 +22,11 @@ import ( "testing" "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + mockcache "github.com/polarismesh/polaris/cache/mock" "github.com/polarismesh/polaris/common/eventhub" mockstore "github.com/polarismesh/polaris/store/mock" - "github.com/stretchr/testify/assert" ) func Test_Initialize(t *testing.T) { From 06d9733c8fa68b85dacf60b12179615ff1fbd540 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Mon, 8 Jan 2024 22:50:02 +0800 Subject: [PATCH 10/10] codecov:1.18.0 --- service/interceptor/auth/client_v1_authability.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/service/interceptor/auth/client_v1_authability.go b/service/interceptor/auth/client_v1_authability.go index 248fd7e56..b967993db 100644 --- a/service/interceptor/auth/client_v1_authability.go +++ b/service/interceptor/auth/client_v1_authability.go @@ -218,12 +218,12 @@ func (svr *ServerAuthAbility) UpdateInstance(ctx context.Context, req *apiservic } // GetServiceContractWithCache User Client Get ServiceContract Rule Information -func (svr *ServerAuthAbility) GetServiceContractWithCache(ctx context.Context, req *apiservice.ServiceContract) *apiservice.DiscoverResponse { - authCtx := svr.collectServiceAuthContext( - ctx, []*apiservice.Service{&apiservice.Service{ - Namespace: wrapperspb.String(req.Namespace), - Name: wrapperspb.String(req.Service), - }}, model.Read, "GetServiceContractWithCache") +func (svr *ServerAuthAbility) GetServiceContractWithCache(ctx context.Context, + req *apiservice.ServiceContract) *apiservice.DiscoverResponse { + authCtx := svr.collectServiceAuthContext(ctx, []*apiservice.Service{{ + Namespace: wrapperspb.String(req.Namespace), + Name: wrapperspb.String(req.Service), + }}, model.Read, "GetServiceContractWithCache") _, err := svr.strategyMgn.GetAuthChecker().CheckClientPermission(authCtx) if err != nil {