diff options
author | Lvbo163 <lv.bo163@zte.com.cn> | 2018-08-06 15:45:19 +0800 |
---|---|---|
committer | Lvbo163 <lv.bo163@zte.com.cn> | 2018-08-06 15:45:19 +0800 |
commit | 9d7f63fdc1906c10618915ef5033832ac3501ae8 (patch) | |
tree | c0b2a9618a0fe8732d81278f3c68dce73a9b5071 /msb2pilot | |
parent | ce890da0e79c26ad6f31f86956f690efee0106da (diff) |
convert msb service to pilot default rule
Issue-ID: MSB-260
Change-Id: I0ae233102f293d2c50c2e4c4a509dd880d0aab15
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
Diffstat (limited to 'msb2pilot')
-rw-r--r-- | msb2pilot/src/msb2pilot/models/config.go | 1 | ||||
-rw-r--r-- | msb2pilot/src/msb2pilot/pilot/msb.go | 108 |
2 files changed, 109 insertions, 0 deletions
diff --git a/msb2pilot/src/msb2pilot/models/config.go b/msb2pilot/src/msb2pilot/models/config.go index 5773cf4..b8111a5 100644 --- a/msb2pilot/src/msb2pilot/models/config.go +++ b/msb2pilot/src/msb2pilot/models/config.go @@ -15,4 +15,5 @@ const ( EnvConsulAddress = "ConsulAddress" //http://localhost:8500 EnvK8sAddress = "K8sAddress" EnvMsbAddress = "MsbAddress" + EnvApiGatewayName = "MsbApiGatewayName" // default value "apigateway" ) diff --git a/msb2pilot/src/msb2pilot/pilot/msb.go b/msb2pilot/src/msb2pilot/pilot/msb.go index c0a2dc9..9b6a977 100644 --- a/msb2pilot/src/msb2pilot/pilot/msb.go +++ b/msb2pilot/src/msb2pilot/pilot/msb.go @@ -11,10 +11,26 @@ */ package pilot +import ( + "bytes" + "msb2pilot/log" + "msb2pilot/models" + "msb2pilot/msb" + "os" + "regexp" + "strings" + + istioModel "istio.io/istio/pilot/pkg/model" +) + var ( cachedServices []*models.MsbService ) +const ( + routerulePrefix = "msbcustom." +) + func SyncMsbData(newServices []*models.MsbService) { log.Log.Debug("sync msb rewrite rule to pilot") createServices, updateServices, deleteServices := compareServices(cachedServices, newServices) @@ -58,3 +74,95 @@ func toServiceMap(services []*models.MsbService) map[string]*models.MsbService { return serviceMap } + +func parseServiceToConfig(services []*models.MsbService) ([]istioModel.Config, error) { + publishServices := getPublishServiceMap() + apiGateway := os.Getenv(models.EnvApiGatewayName) + var buf bytes.Buffer + for _, service := range services { + if publishService, exist := publishServices[getPublishServiceKey(service)]; exist { + + if service.ConsulLabels.BaseInfo != nil { + rule := createRouteRule(apiGateway, publishService.PublishUrl, service.ServiceName, service.ConsulLabels.BaseInfo.Url) + buf.WriteString(rule) + } + } + } + return ParseParam(buf.String()) +} + +func getPublishServiceKey(svc *models.MsbService) string { + res := svc.ServiceName + + if svc.ConsulLabels.BaseInfo != nil { + res += svc.ConsulLabels.BaseInfo.Version + } + + if svc.ConsulLabels.NameSpace != nil { + res += svc.ConsulLabels.NameSpace.NameSpace + } + + return res +} + +func getPublishServiceMap() map[string]*models.PublishService { + publishServices := msb.GetAllPublishServices() + + res := make(map[string]*models.PublishService) + + for _, svc := range publishServices { + key := svc.ServiceName + svc.Version + svc.NameSpace + res[key] = svc + } + + return res +} + +func createRouteRule(sourceService, sourcePath, targetService, targetPath string) string { + if sourcePath == "" { + sourcePath = "/" + } + if targetPath == "" { + targetPath = "/" + } + // rule name must consist of lower case alphanuberic charactoers, '-' or '.'. and must start and end with an alphanumberic charactore + r := regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*") + strs := r.FindAllString(targetService, -1) + name := routerulePrefix + strings.Join(strs, "") + name = strings.ToLower(name) + + rule := `{ +"apiVersion": "config.istio.io/v1alpha2", +"kind": "RouteRule", +"metadata": { + "name": "` + name + `" +}, +"spec": { + "destination":{ + "name":"` + sourceService + `" + }, + "match":{ + "request":{ + "headers": { + "uri": { + "prefix": "` + sourcePath + `" + } + } + } + }, + "rewrite": { + "uri": "` + targetPath + `" + }, + "route":[ + { + "destination":{ + "name":"` + targetService + `" + } + } + ] +} +} + +` + return rule +} |