From 9f1f90d203d12eb25c2201445e7ec6f3ae9f6e3e Mon Sep 17 00:00:00 2001 From: "chenlinfeng@step.ai" Date: Fri, 23 Feb 2024 16:12:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81overide=E5=8C=97=E6=9E=81?= =?UTF-8?q?=E6=98=9F=E5=90=8D=E5=AD=97=E5=92=8CNamespace=EF=BC=8C=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E5=A4=A7=E5=B0=8F=E5=86=99=E5=92=8C-=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/controller/apis.go | 23 +++++++++++----------- pkg/controller/service.go | 2 +- pkg/polarisapi/service_api.go | 37 +++++++++++++++++++---------------- pkg/util/controller_utils.go | 15 ++++++++++++++ pkg/util/types.go | 18 +++++++++-------- 5 files changed, 57 insertions(+), 38 deletions(-) diff --git a/pkg/controller/apis.go b/pkg/controller/apis.go index c9c1628a..7d196bdb 100644 --- a/pkg/controller/apis.go +++ b/pkg/controller/apis.go @@ -68,7 +68,7 @@ func (p *PolarisController) addInstances(service *v1.Service, addresses []addres if len(addresses) == 0 { return nil } - serviceMsg := fmt.Sprintf("[%s/%s]", service.GetNamespace(), service.GetName()) + serviceMsg := fmt.Sprintf("[%s/%s]", util.GetNamespace(service), util.GetServiceName(service)) // 处理健康检查 var healthCheck polarisapi.HealthCheck healthy := util.Bool(true) @@ -104,8 +104,8 @@ func (p *PolarisController) addInstances(service *v1.Service, addresses []addres *healthy = *healthy && addr.Healthy tmpInstance := polarisapi.Instance{ - Service: service.Name, - Namespace: service.Namespace, + Service: util.GetServiceName(service), + Namespace: util.GetNamespace(service), ServiceToken: globalToken, HealthCheck: &healthCheck, Host: addr.IP, @@ -129,7 +129,7 @@ func (p *PolarisController) deleteInstances(service *v1.Service, addresses []add if len(addresses) == 0 { return nil } - serviceMsg := fmt.Sprintf("[%s/%s]", service.GetNamespace(), service.GetName()) + serviceMsg := fmt.Sprintf("[%s/%s]", util.GetNamespace(service), util.GetServiceName(service)) startTime := time.Now() defer func() { log.SyncNamingScope().Infof("Finish to delete all instance %s (%v)", serviceMsg, time.Since(startTime)) @@ -139,8 +139,8 @@ func (p *PolarisController) deleteInstances(service *v1.Service, addresses []add for _, i := range addresses { tmpInstance := polarisapi.Instance{ - Service: service.Name, - Namespace: service.Namespace, + Service: util.GetServiceName(service), + Namespace: util.GetNamespace(service), ServiceToken: globalToken, Host: i.IP, Port: util.IntPtr(i.Port), @@ -155,7 +155,7 @@ func (p *PolarisController) updateInstances(service *v1.Service, addresses []add if len(addresses) == 0 { return nil } - serviceMsg := fmt.Sprintf("[%s/%s]", service.GetNamespace(), service.GetName()) + serviceMsg := fmt.Sprintf("[%s/%s]", util.GetNamespace(service), util.GetServiceName(service)) startTime := time.Now() defer func() { log.SyncNamingScope().Infof("Finish to update all %s (%v)", serviceMsg, time.Since(startTime)) @@ -189,8 +189,8 @@ func (p *PolarisController) updateInstances(service *v1.Service, addresses []add healthy := util.Bool(addr.Healthy) tmpInstance := polarisapi.Instance{ - Service: service.Name, - Namespace: service.Namespace, + Service: util.GetServiceName(service), + Namespace: util.GetNamespace(service), ServiceToken: globalToken, HealthCheck: &healthCheck, Host: addr.IP, @@ -212,9 +212,8 @@ func (p *PolarisController) getAllInstance(service *v1.Service) (instances []mod startTime := time.Now() getInstancesReq := &api.GetAllInstancesRequest{} getInstancesReq.FlowID = rand.Uint64() - getInstancesReq.Namespace = service.Namespace - getInstancesReq.Service = service.Name - + getInstancesReq.Namespace = util.GetNamespace(service) + getInstancesReq.Service = util.GetServiceName(service) registered, err := p.consumer.GetAllInstances(getInstancesReq) if err != nil { metrics.InstanceRequestSync.WithLabelValues("Get", "SDK", "Failed", "500"). diff --git a/pkg/controller/service.go b/pkg/controller/service.go index 5109accf..be0c44cc 100644 --- a/pkg/controller/service.go +++ b/pkg/controller/service.go @@ -240,7 +240,7 @@ func (p *PolarisController) processSyncNamespaceAndService(service *v1.Service) return nil } - createNsResponse, err := polarisapi.CreateNamespaces(service.Namespace) + createNsResponse, err := polarisapi.CreateNamespaces(util.GetNamespace(service)) if err != nil { log.SyncNamingScope().Errorf("Failed create namespaces in processSyncNamespaceAndService %s, err %s, resp %v", serviceMsg, err, createNsResponse) diff --git a/pkg/polarisapi/service_api.go b/pkg/polarisapi/service_api.go index bf3e8929..73241e7a 100644 --- a/pkg/polarisapi/service_api.go +++ b/pkg/polarisapi/service_api.go @@ -405,7 +405,8 @@ func dealUpdateInstanceResponse(response AddResponse, msg string, // GetService 查询服务接口 // GET /naming/v1/services?参数名=参数值 func GetService(service *v1.Service) (res GetServiceResponse, err error) { - serviceMsg := fmt.Sprintf("[%s/%s]", service.GetNamespace(), service.GetName()) + polarisName, polarisNamespace := getPolarisName(service) + serviceMsg := fmt.Sprintf("[%s/%s]", polarisNamespace, polarisName) log.SyncNamingScope().Infof("Start to get %s", serviceMsg) startTime := time.Now() @@ -415,17 +416,15 @@ func GetService(service *v1.Service) (res GetServiceResponse, err error) { var response GetServiceResponse requestID := uuid.New().String() - polarisNamespace := service.Namespace if polarisNamespace == "" { return response, fmt.Errorf("failed service is invalid, polarisNamespace is empty") } - polaris := service.Name - if polaris == "" { + if polarisName == "" { return response, fmt.Errorf("failed service is invalid, polarisService is empty") } url := fmt.Sprintf("%s%s?namespace=%s&name=%s&offset=%d&limit=%d", - PolarisHttpURL, getService, polarisNamespace, polaris, 0, 100) + PolarisHttpURL, getService, polarisNamespace, polarisName, 0, 100) statusCode, body, _, err := polarisHttpRequest(requestID, http.MethodGet, url, nil) @@ -487,7 +486,6 @@ func ListService(clusterID string) (res GetServiceResponse, err error) { } func getPolarisPorts(service *v1.Service) string { - var ports []string for _, port := range service.Spec.Ports { ports = append(ports, strconv.Itoa(int(port.Port))) @@ -498,10 +496,10 @@ func getPolarisPorts(service *v1.Service) string { // CreateService 创建北极星服务 func CreateService(service *v1.Service) (CreateServicesResponse, error) { + polarisName, polarisNamespace := getPolarisName(service) + serviceMsg := fmt.Sprintf("[%s/%s]", polarisNamespace, polarisName) - serviceMsg := fmt.Sprintf("[%s/%s]", service.GetNamespace(), service.GetName()) - - log.SyncNamingScope().Infof("Start to create service [%s][%s]", service.Namespace, service.Name) + log.SyncNamingScope().Infof("Start to create service [%s][%s]", polarisNamespace, polarisName) startTime := time.Now() defer func() { log.SyncNamingScope().Infof("Finish to update %s (%v)", serviceMsg, time.Since(startTime)) @@ -513,8 +511,8 @@ func CreateService(service *v1.Service) (CreateServicesResponse, error) { createRequest := []CreateServiceRequest{ { - Name: service.Name, - Namespace: service.Namespace, + Name: polarisName, + Namespace: polarisNamespace, Owners: Source, Ports: getPolarisPorts(service), Metadata: service.Labels, @@ -553,13 +551,12 @@ func CreateService(service *v1.Service) (CreateServicesResponse, error) { // CreateServiceAlias 创建北极星服务别名 func CreateServiceAlias(service *v1.Service) (CreateServiceAliasResponse, error) { - var response CreateServiceAliasResponse - alias := service.GetAnnotations()[util.PolarisAliasService] aliasNs := service.GetAnnotations()[util.PolarisAliasNamespace] + polarisName, polarisNamespace := getPolarisName(service) - serviceAliasMsg := fmt.Sprintf("[%s/%s], [%s/%s]", service.GetNamespace(), service.GetName(), aliasNs, alias) + serviceAliasMsg := fmt.Sprintf("[%s/%s], [%s/%s]", polarisNamespace, polarisName, aliasNs, alias) log.SyncNamingScope().Infof("Start to create service alias %s", serviceAliasMsg) startTime := time.Now() @@ -579,8 +576,8 @@ func CreateServiceAlias(service *v1.Service) (CreateServiceAliasResponse, error) url := fmt.Sprintf("%s%s", PolarisHttpURL, createServiceAlias) createRequest := &CreateServiceAliasRequest{ - Service: service.Name, - Namespace: service.Namespace, + Service: polarisName, + Namespace: polarisNamespace, Alias: alias, AliasNamespace: aliasNs, Owners: Source, @@ -619,7 +616,8 @@ func CreateServiceAlias(service *v1.Service) (CreateServiceAliasResponse, error) // UpdateService 更新服务字段 // PUT /naming/v1/services func UpdateService(service *v1.Service, request []Service) (int, PutServicesResponse, error) { - serviceMsg := fmt.Sprintf("[%s/%s]", service.GetNamespace(), service.GetName()) + polarisName, polarisNamespace := getPolarisName(service) + serviceMsg := fmt.Sprintf("[%s/%s]", polarisNamespace, polarisName) log.SyncNamingScope().Infof("Start to update %s", serviceMsg) startTime := time.Now() defer func() { @@ -821,3 +819,8 @@ func lookAccessToken() (string, error) { return userResp.User.AuthToken, nil } + +// getPolarisName, 解析北极星的Name +func getPolarisName(svr *v1.Service) (string, string) { + return util.GetServiceName(svr), util.GetNamespace(svr) +} diff --git a/pkg/util/controller_utils.go b/pkg/util/controller_utils.go index 7fb02d37..b07ad594 100644 --- a/pkg/util/controller_utils.go +++ b/pkg/util/controller_utils.go @@ -120,3 +120,18 @@ func IsConfigMapKey(key string) (string, bool) { } return key, false } + +func GetNamespace(svr *v1.Service) string { + if v, ok := svr.GetAnnotations()[PolarisOverideNamespace]; ok && v != "" { + return v + } + + return svr.GetNamespace() +} + +func GetServiceName(svr *v1.Service) string { + if v, ok := svr.GetAnnotations()[PolarisOverideService]; ok && v != "" { + return v + } + return svr.GetName() +} diff --git a/pkg/util/types.go b/pkg/util/types.go index cd6c8f65..d37915c3 100644 --- a/pkg/util/types.go +++ b/pkg/util/types.go @@ -20,14 +20,16 @@ const ( ) const ( - PolarisSync = "polarismesh.cn/sync" - PolarisEnableRegister = "polarismesh.cn/enableRegister" - PolarisAliasNamespace = "polarismesh.cn/aliasNamespace" - PolarisAliasService = "polarismesh.cn/aliasService" - PolarisSidecarMode = "polarismesh.cn/sidecar-mode" - PolarisMetadata = "polarismesh.cn/metadata" - PolarisWeight = "polarismesh.cn/weight" - PolarisHeartBeatTTL = "polarismesh.cn/ttl" + PolarisSync = "polarismesh.cn/sync" + PolarisEnableRegister = "polarismesh.cn/enableRegister" + PolarisAliasNamespace = "polarismesh.cn/aliasNamespace" + PolarisAliasService = "polarismesh.cn/aliasService" + PolarisOverideNamespace = "polarismesh.cn/overideNamespace" + PolarisOverideService = "polarismesh.cn/overideService" + PolarisSidecarMode = "polarismesh.cn/sidecar-mode" + PolarisMetadata = "polarismesh.cn/metadata" + PolarisWeight = "polarismesh.cn/weight" + PolarisHeartBeatTTL = "polarismesh.cn/ttl" WorkloadKind = "polarismesh.cn/workloadKind" PolarisCustomWeight = "polarismesh.cn/customWeight"