Skip to content

Commit

Permalink
fix:修复service_contract没有放开客户端接入 (#1312)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Dec 27, 2023
1 parent 1e10f5e commit f4f35c6
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 6 deletions.
1 change: 1 addition & 0 deletions apiserver/grpcserver/config/client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (g *ConfigGRPCServer) Discover(svr apiconfig.PolarisConfigGRPC_DiscoverServ
req := in.GetConfigFile()
req.Md5 = wrapperspb.String(in.GetRevision())
out = g.configServer.GetConfigGroupsWithCache(ctx, req)
out.Type = apiconfig.ConfigDiscoverResponse_CONFIG_FILE_GROUPS
default:
out = api.NewConfigDiscoverResponse(apimodel.Code_InvalidDiscoverResource)
}
Expand Down
2 changes: 1 addition & 1 deletion apiserver/grpcserver/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (g *ConfigGRPCServer) allowAccess(method string) bool {
// GetClientOpenMethod .
func GetClientOpenMethod(protocol string) (map[string]bool, error) {
openMethods := []string{"GetConfigFile", "CreateConfigFile",
"UpdateConfigFile", "PublishConfigFile", "WatchConfigFiles", "GetConfigFileMetadataList"}
"UpdateConfigFile", "PublishConfigFile", "WatchConfigFiles", "GetConfigFileMetadataList", "Discover"}

openMethod := make(map[string]bool)

Expand Down
1 change: 1 addition & 0 deletions apiserver/grpcserver/discover/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (g *GRPCServer) Run(errCh chan error) {
// 注册 v1 版本的 spec discover server
apiservice.RegisterPolarisGRPCServer(server, g.v1server)
apiservice.RegisterPolarisHeartbeatGRPCServer(server, g.v1server)
apiservice.RegisterPolarisServiceContractGRPCServer(server, g.v1server)
openMethod, getErr := utils.GetClientOpenMethod(config.Include, g.GetProtocol())
if getErr != nil {
return getErr
Expand Down
9 changes: 9 additions & 0 deletions apiserver/grpcserver/discover/v1/client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ func (g *DiscoverServer) Discover(server apiservice.PolarisGRPC_DiscoverServer)
}
}

func (g *DiscoverServer) ReportServiceContract(ctx context.Context, in *apiservice.ServiceContract) (*apiservice.Response, error) {
// 需要记录操作来源,提高效率,只针对特殊接口添加operator
rCtx := utils.ConvertGRPCContext(ctx)
rCtx = context.WithValue(rCtx, utils.StringContext("operator"), ParseGrpcOperator(ctx))

out := g.namingServer.ReportServiceContract(rCtx, in)
return out, nil
}

// ParseGrpcOperator 构造请求源
func ParseGrpcOperator(ctx context.Context) string {
// 获取请求源
Expand Down
4 changes: 2 additions & 2 deletions apiserver/xdsserverv3/resource/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ const (
// SidecarRegisterService xds metadata key when node what register service from envoy healthcheck
// value example: [{"name":"","ports":{"TCP":[8080],"DUBBO":[28080]},"health_check_path":"","health_check_port":8080,"health_check_ttl":5}]
SidecarRegisterService = "sidecar.polarismesh.cn/registerServices"
// SidecarOpenOnDemandFeature .
SidecarOpenOnDemandFeature = "sidecar.polarismesh.cn/openOnDemand"
// SidecarTLSModeTag .
SidecarTLSModeTag = "sidecar.polarismesh.cn/tlsMode"
// SidecarOpenOnDemandFeature .
SidecarOpenOnDemandFeature = "sidecar.polarismesh.cn/openOnDemand"
// SidecarConnectServerEndpoint report xds server the envoy xds on-demand cds server endpoint info
SidecarODCDSServerEndpoint = "sidecar.polarismesh.cn/odcdsServerEndpoint"
)
Expand Down
2 changes: 2 additions & 0 deletions cache/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ type (
Query(filter map[string]string, offset, limit uint32) ([]*model.EnrichServiceContract, uint32, error)
// ListVersions .
ListVersions(service, namespace string) []*model.EnrichServiceContract
// Get .
Get(req *model.ServiceContract) *model.EnrichServiceContract
}
)

Expand Down
29 changes: 29 additions & 0 deletions cache/mock/cache_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions cache/service/service_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type serviceContractCache struct {

lastMtimeLogged int64

// data namespace/service/name/protocol/version -> *model.EnrichServiceContract
data *utils.SyncMap[string, *model.EnrichServiceContract]
// contracts 服务契约缓存,namespace -> service -> []*model.EnrichServiceContract
contracts *utils.SyncMap[string, *utils.SyncMap[string, *utils.SyncMap[string, *model.EnrichServiceContract]]]
singleGroup *singleflight.Group
Expand All @@ -54,6 +56,7 @@ type serviceContractCache struct {
// Initialize
func (sc *serviceContractCache) Initialize(c map[string]interface{}) error {
sc.singleGroup = &singleflight.Group{}
sc.data = utils.NewSyncMap[string, *model.EnrichServiceContract]()
sc.contracts = utils.NewSyncMap[string, *utils.SyncMap[string, *utils.SyncMap[string, *model.EnrichServiceContract]]]()
return nil
}
Expand Down Expand Up @@ -113,11 +116,13 @@ func (sc *serviceContractCache) setContracts(values []*model.EnrichServiceContra
serviceVal, _ := namespaceVal.Load(service)
if !item.Valid {
del++
sc.data.Delete(item.GetCacheKey())
serviceVal.Delete(item.ID)
continue
}

upsert++
sc.data.Store(item.GetCacheKey(), item)
serviceVal.Store(item.ID, item)
}
return map[string]time.Time{
Expand All @@ -127,6 +132,7 @@ func (sc *serviceContractCache) setContracts(values []*model.EnrichServiceContra

// Clear
func (sc *serviceContractCache) Clear() error {
sc.data = utils.NewSyncMap[string, *model.EnrichServiceContract]()
sc.contracts = utils.NewSyncMap[string, *utils.SyncMap[string, *utils.SyncMap[string, *model.EnrichServiceContract]]]()
return nil
}
Expand All @@ -148,6 +154,11 @@ func (sc *serviceContractCache) forceQueryUpdate() error {
return err
}

func (sc *serviceContractCache) Get(req *model.ServiceContract) *model.EnrichServiceContract {
ret, _ := sc.data.Load(req.GetCacheKey())
return ret
}

// Query .
func (sc *serviceContractCache) Query(filter map[string]string, offset, limit uint32) ([]*model.EnrichServiceContract, uint32, error) {
if err := sc.forceQueryUpdate(); err != nil {
Expand Down
6 changes: 5 additions & 1 deletion common/model/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ type ServiceContractView struct {
ManualInterfaces map[string]*InterfaceDescriptor
}

func (s *ServiceContract) GetKey() string {
func (s *ServiceContract) GetResourceName() string {
return fmt.Sprintf("%s/%s/%s/%s", s.Service, s.Name, s.Protocol, s.Version)
}

func (s *ServiceContract) GetCacheKey() string {
return fmt.Sprintf("%s/%s/%s/%s/%s", s.Namespace, s.Service, s.Name, s.Protocol, s.Version)
}

type InterfaceDescriptor struct {
// ID
ID string
Expand Down
4 changes: 4 additions & 0 deletions release/standalone/linux/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ xdsv3_port=$(getProperties polaris_xdsv3_port)
service_grpc_port=$(getProperties polaris_service_grpc_port)
config_grpc_port=$(getProperties polaris_config_grpc_port)
api_http_port=$(getProperties polaris_open_api_port)
nacos_port=$(getProperties nacos_http_port)

prometheus_port=$(getProperties prometheus_port)
pushgateway_port=$(getProperties pushgateway_port)
Expand All @@ -78,6 +79,7 @@ echo "xdsv3_port=${xdsv3_port}"
echo "service_grpc_port=${service_grpc_port}"
echo "config_grpc_port=${config_grpc_port}"
echo "api_http_port=${api_http_port}"
echo "nacos_port=${nacos_port}"
echo ""
echo "polaris-limiter-server listen port info"
echo "polaris_limiter_http_port=${limiter_http_port}"
Expand Down Expand Up @@ -126,6 +128,8 @@ function installPolarisServer() {
sed -i "s/listenPort: 8093/listenPort: ${config_grpc_port}/g" conf/polaris-server.yaml
# 修改 polaris-server http-api 端口信息
sed -i "s/listenPort: 8090/listenPort: ${api_http_port}/g" conf/polaris-server.yaml
# 修改 polaris-server nacos 端口信息
sed -i "s/listenPort: 8848/listenPort: ${nacos_port}/g" conf/polaris-server.yaml

/bin/bash ./tool/start.sh
echo -e "install polaris server success"
Expand Down
2 changes: 2 additions & 0 deletions release/standalone/port.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ polaris_limiter_http_port=8100
polaris_limiter_grpc_port=8101
prometheus_port=9090
pushgateway_port=9091
nacos_http_port=8848
nacos_grpc_port=9848
1 change: 1 addition & 0 deletions release/tool/include
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function start() {
set +e
ulimit -n 409600
set -e
chmod +x $server_name
nohup $cmdline >>/dev/null 2>&1 &
}

Expand Down
2 changes: 2 additions & 0 deletions service/api_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ type ClientServer interface {
GetFaultDetectWithCache(ctx context.Context, req *apiservice.Service) *apiservice.DiscoverResponse
// UpdateInstance update one instance by client
UpdateInstance(ctx context.Context, req *apiservice.Instance) *apiservice.Response
// ReportServiceContract client report service_contract
ReportServiceContract(ctx context.Context, req *apiservice.ServiceContract) *apiservice.Response
}

// L5OperateServer L5 related operations
Expand Down
22 changes: 22 additions & 0 deletions service/client_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ func (s *Server) DeregisterInstance(ctx context.Context, req *apiservice.Instanc
return s.DeleteInstance(ctx, req)
}

// ReportServiceContract report client service interface info
func (s *Server) ReportServiceContract(ctx context.Context, req *apiservice.ServiceContract) *apiservice.Response {
ctx = context.WithValue(ctx, utils.ContextIsFromClient, true)
cacheData := s.caches.ServiceContract().Get(&model.ServiceContract{
Namespace: req.GetNamespace(),
Service: req.GetService(),
Name: req.GetName(),
Version: req.GetVersion(),
Protocol: req.GetProtocol(),
})
// 通过 Cache 模块减少无意义的 CreateServiceContract 逻辑
if cacheData == nil || cacheData.Content != req.GetContent() {
rsp := s.CreateServiceContract(ctx, req)
if rsp.GetCode().GetValue() != uint32(apimodel.Code_ExecuteSuccess) {
return rsp
}
}

rsp := s.CreateServiceContractInterfaces(ctx, req, apiservice.InterfaceDescriptor_Client)
return rsp
}

// ReportClient 客户端上报信息
func (s *Server) ReportClient(ctx context.Context, req *apiservice.Client) *apiservice.Response {
if s.caches == nil {
Expand Down
20 changes: 20 additions & 0 deletions service/interceptor/auth/client_v1_authability.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"

apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
"google.golang.org/protobuf/types/known/wrapperspb"

api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/common/model"
Expand Down Expand Up @@ -66,6 +67,25 @@ func (svr *ServerAuthAbility) ReportClient(ctx context.Context, req *apiservice.
return svr.targetServer.ReportClient(ctx, req)
}

// ReportServiceContract .
func (svr *ServerAuthAbility) ReportServiceContract(ctx context.Context, req *apiservice.ServiceContract) *apiservice.Response {
authCtx := svr.collectServiceAuthContext(
ctx, []*apiservice.Service{{
Name: wrapperspb.String(req.GetService()),
Namespace: wrapperspb.String(req.GetNamespace()),
}}, model.Create, "ReportServiceContract")

_, err := svr.strategyMgn.GetAuthChecker().CheckClientPermission(authCtx)
if err != nil {
resp := api.NewResponseWithMsg(convertToErrCode(err), err.Error())
return resp
}

ctx = authCtx.GetRequestContext()
ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
return svr.targetServer.ReportServiceContract(ctx, req)
}

// GetPrometheusTargets Used for client acquisition service information
func (svr *ServerAuthAbility) GetPrometheusTargets(ctx context.Context,
query map[string]string) *model.PrometheusDiscoveryResponse {
Expand Down
2 changes: 1 addition & 1 deletion service/service_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func serviceContractRecordEntry(ctx context.Context, req *apiservice.ServiceCont

entry := &model.RecordEntry{
ResourceType: model.RServiceContract,
ResourceName: data.GetKey(),
ResourceName: data.GetResourceName(),
Namespace: req.GetNamespace(),
OperationType: operationType,
Operator: utils.ParseOperator(ctx),
Expand Down
3 changes: 2 additions & 1 deletion service/service_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
"testing"

"github.com/golang/protobuf/ptypes"
"github.com/polarismesh/polaris/common/utils"
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
"github.com/polarismesh/specification/source/go/api/v1/service_manage"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/anypb"

"github.com/polarismesh/polaris/common/utils"
)

func TestServer_CreateServiceContracts(t *testing.T) {
Expand Down

0 comments on commit f4f35c6

Please sign in to comment.