Skip to content

Commit

Permalink
codecov:1.18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Jan 8, 2024
1 parent 19ba2cf commit fd48261
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 27 deletions.
3 changes: 3 additions & 0 deletions apiserver/grpcserver/discover/v1/client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 8 additions & 8 deletions apiserver/xdsserverv3/resource/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
},
},
},
Expand All @@ -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(),
},
},
},
Expand Down
21 changes: 11 additions & 10 deletions common/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 29 additions & 6 deletions common/model/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"time"

commontime "github.com/polarismesh/polaris/common/time"

Check failure on line 24 in common/model/contract.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.21.5)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/polarismesh/polaris) --custom-order (gci)
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"

Check failure on line 25 in common/model/contract.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.21.5)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/polarismesh/polaris) --custom-order (gci)
)

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions service/api_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
116 changes: 116 additions & 0 deletions service/client_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Check warning on line 511 in service/client_v1.go

View check run for this annotation

Codecov / codecov/patch

service/client_v1.go#L499-L511

Added lines #L499 - L511 were not covered by tests

// 服务名和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(),
}
}

Check warning on line 526 in service/client_v1.go

View check run for this annotation

Codecov / codecov/patch

service/client_v1.go#L514-L526

Added lines #L514 - L526 were not covered by tests

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
}

Check warning on line 539 in service/client_v1.go

View check run for this annotation

Codecov / codecov/patch

service/client_v1.go#L528-L539

Added lines #L528 - L539 were not covered by tests

// 获取熔断规则数据,并对比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
}

Check warning on line 546 in service/client_v1.go

View check run for this annotation

Codecov / codecov/patch

service/client_v1.go#L542-L546

Added lines #L542 - L546 were not covered by tests

resp.Service.Revision = wrapperspb.String(out.Revision)
resp.ServiceContract = out.ToSpec()
return resp

Check warning on line 550 in service/client_v1.go

View check run for this annotation

Codecov / codecov/patch

service/client_v1.go#L548-L550

Added lines #L548 - L550 were not covered by tests
}

func createCommonDiscoverResponse(req *apiservice.Service,
dT apiservice.DiscoverResponse_DiscoverResponseType) *apiservice.DiscoverResponse {
return &apiservice.DiscoverResponse{
Expand Down Expand Up @@ -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
}

Check warning on line 658 in service/client_v1.go

View check run for this annotation

Codecov / codecov/patch

service/client_v1.go#L639-L658

Added lines #L639 - L658 were not covered by tests

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

Check warning on line 695 in service/client_v1.go

View check run for this annotation

Codecov / codecov/patch

service/client_v1.go#L660-L695

Added lines #L660 - L695 were not covered by tests
}
21 changes: 21 additions & 0 deletions service/interceptor/auth/client_v1_authability.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 221 in service/interceptor/auth/client_v1_authability.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.21.5)

line is 142 characters (lll)
authCtx := svr.collectServiceAuthContext(
ctx, []*apiservice.Service{&apiservice.Service{

Check failure on line 223 in service/interceptor/auth/client_v1_authability.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.21.5)

File is not `gofmt`-ed with `-s` (gofmt)
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)
}

0 comments on commit fd48261

Please sign in to comment.