Skip to content

Commit

Permalink
支持overide北极星名字和Namespace,解决大小写和-问题
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Feb 23, 2024
1 parent bdabec5 commit 9f1f90d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
23 changes: 11 additions & 12 deletions pkg/controller/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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))
Expand All @@ -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),
Expand All @@ -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))
Expand Down Expand Up @@ -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,
Expand All @@ -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").
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 20 additions & 17 deletions pkg/polarisapi/service_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

Expand Down Expand Up @@ -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)))
Expand All @@ -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))
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
15 changes: 15 additions & 0 deletions pkg/util/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
18 changes: 10 additions & 8 deletions pkg/util/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 9f1f90d

Please sign in to comment.